当前位置:   article > 正文

Java 二叉搜索树 实现六种遍历方法_java二叉搜索树遍历

java二叉搜索树遍历

目录

 创建二叉搜索树对象

         让T泛型继承Comparable接口

         创建内部类

实现遍历方法

        前序遍历

                递归实现 

                非递归实现  

        中序遍历

                递归实现  

                非递归实现 

        后序遍历 

                递归实现  

                非递归实现  


 创建二叉搜索树对象

         让T泛型继承Comparable接口

  1. public class BinarySearchTree<T extends Comparable<T>, V> {
  2. // 根节点
  3. private TreeNode<T, V> root;
  4. }

         创建内部类

  1. private static class TreeNode<T, V> {
  2. public T key;
  3. public V value;
  4. public TreeNode<T, V> left;
  5. public TreeNode<T, V> right;
  6. public TreeNode(T key) {
  7. this.key = key;
  8. }
  9. public TreeNode(T key, V value) {
  10. this.key = key;
  11. this.value = value;
  12. }
  13. @Override
  14. public String toString() {
  15. return "{" +
  16. "key=" + key +
  17. ", value=" + value +
  18. '}';
  19. }
  20. public TreeNode(T key, V value, TreeNode<T, V> left, TreeNode<T, V> right) {
  21. this.key = key;
  22. this.value = value;
  23. this.left = left;
  24. this.right = right;
  25. }
  26. }

实现遍历方法

        前序遍历

                递归实现 

  1. /**
  2. * 前序遍历
  3. */
  4. public void preOrder() {
  5. preOrder(root);
  6. }
  7. /**
  8. * 前序遍历
  9. *
  10. * @param root -根结点
  11. */
  12. private void preOrder(TreeNode<T, V> root) {
  13. if (root == null) {
  14. return;
  15. }
  16. System.out.println(root.toString());
  17. preOrder(root.left);
  18. preOrder(root.right);
  19. }

                非递归实现  

  1. public List<Integer> preorderTraversal(TreeNode root) {
  2. List<Integer> list = new LinkedList<>();
  3. if (root == null) {
  4. return list;
  5. }
  6. Stack<TreeNode> stack = new Stack<TreeNode>();
  7. TreeNode node = root;
  8. TreeNode pop = null;
  9. while (!stack.isEmpty() || node != null) {
  10. if (node != null) {
  11. // 待处理左子数
  12. // 将节点压入栈
  13. stack.push(node);
  14. list.add(node.val);
  15. node = node.left;
  16. } else {
  17. //处理右子数
  18. // 记录栈顶元素
  19. TreeNode peek = stack.peek();
  20. // 没有右子数
  21. if (peek.right == null) {
  22. pop = stack.pop();
  23. }
  24. // 右子数处理完成
  25. else if (peek.right == pop) {
  26. pop = stack.pop();
  27. }
  28. // 待处理右子数
  29. else {
  30. node = peek.right;
  31. }
  32. }
  33. }
  34. return list;
  35. }

        中序遍历

                递归实现  

  1. /**
  2. * 中序遍历
  3. */
  4. public void inOrder() {
  5. inOrder(root);
  6. }
  7. /**
  8. * 中序遍历
  9. *
  10. * @param root -根结点
  11. */
  12. private void inOrder(TreeNode<T, V> root) {
  13. if (root == null) {
  14. return;
  15. }
  16. preOrder(root.left);
  17. System.out.println(root.toString());
  18. preOrder(root.right);
  19. }

                非递归实现 

  1. public List<Integer> inorderTraversal(TreeNode root) {
  2. List<Integer> list = new ArrayList<>();
  3. Stack<TreeNode> stack = new Stack<TreeNode>();
  4. TreeNode node = root;
  5. TreeNode pop = null;
  6. while (!stack.isEmpty() || node != null) {
  7. if (node != null) {
  8. stack.push(node);
  9. node = node.left;
  10. } else {
  11. TreeNode peek = stack.peek();
  12. if (peek.right == null) {
  13. list.add(peek.val);
  14. pop = stack.pop();
  15. } else if (peek.right == pop) {
  16. pop = stack.pop();
  17. } else {
  18. list.add(peek.val);
  19. node = peek.right;
  20. }
  21. }
  22. }
  23. return list;
  24. }

后序遍历 

                递归实现  

  1. /**
  2. * 后序遍历
  3. */
  4. public void postOrder() {
  5. postOrder(root);
  6. }
  7. /**
  8. * 后序遍历
  9. *
  10. * @param root -根结点
  11. */
  12. private void postOrder(TreeNode<T, V> root) {
  13. if (root == null) {
  14. return;
  15. }
  16. preOrder(root.left);
  17. preOrder(root.right);
  18. System.out.println(root.toString());
  19. }

                非递归实现  

  1. public List<Integer> postorderTraversal(TreeNode root) {
  2. List<Integer> list = new ArrayList<>();
  3. if (root == null) {
  4. return list;
  5. }
  6. Stack<TreeNode> stack = new Stack<TreeNode>();
  7. TreeNode node = root;
  8. TreeNode pop = null;
  9. while (!stack.isEmpty() || node != null) {
  10. if (node != null) {
  11. stack.push(node);
  12. node = node.left;
  13. } else {
  14. TreeNode peek = stack.peek();
  15. if (peek.right == null) {
  16. pop = stack.pop();
  17. list.add(pop.val);
  18. } else if (peek.right == pop) {
  19. pop = stack.pop();
  20. list.add(pop.val);
  21. } else {
  22. node = peek.right;
  23. }
  24. }
  25. }
  26. return list;
  27. }

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/528286
推荐阅读
相关标签
  

闽ICP备14008679号