Java实现二叉树的递归和非递归遍历

- 日理万妓 2023-07-14 08:21 91阅读 0赞

点击进入尚硅谷数据结构和算法Java代码导航

1.1 递归前序遍历

  1. //递归前序遍历
  2. public void preOrderRec(Node temp) {
  3. if(temp == null) {
  4. System.out.println("BinaryTree is empty.");
  5. }else {
  6. System.out.print(temp.data + " ");
  7. if(temp.left != null) {
  8. preOrderRec(temp.left);
  9. }
  10. if(temp.right != null) {
  11. preOrderRec(temp.right);
  12. }
  13. }
  14. }

1.2 非递归前序遍历算法一

算法思路:先遍历到二叉树的最左边的子节点,过程中同时打印数据被遍历的子节点并将其压栈,到末尾时依次出栈遍历右子节点。该方法将子二叉树的根节点压栈,利用根节点寻找它的右子节点。

  1. //非递归前序遍历算法一,压栈的是每个二叉子树的根节点,利用这个根节点来找右子节点
  2. public void preOrderNotRec2() {
  3. Node temp = root;
  4. Stack<Node> stack = new Stack<Node>();
  5. while(!stack.isEmpty() || temp != null) {
  6. if(temp != null) {
  7. System.out.print( temp.data + " ");
  8. stack.push(temp);
  9. temp = temp.left;
  10. }else {
  11. temp = stack.pop();
  12. temp = temp.right;
  13. }
  14. }
  15. }

1.3 非递归前序遍历算法二

算法思路:
1,将根节点压栈。
2,从栈顶弹出节点,记为temp,打印temp数据,然后再将temp的右子节点和左子节点分别入栈。
3,重复步骤2直至栈空。
该算法与算法一不同之处是栈里存储的是每个子二叉树的右子节点,而不是根节点。

  1. //非递归前序遍历算法一,压栈的是右子节点,
  2. public void preOrderNotRec1() {
  3. Node temp = root;
  4. Stack<Node> stack = new Stack<Node>();
  5. stack.push(temp);
  6. while(!stack.isEmpty()) {
  7. temp = stack.pop();
  8. System.out.print( temp.data + " ");
  9. if(temp.right != null) {
  10. stack.push(temp.right);
  11. }
  12. if(temp.left != null) {
  13. stack.push(temp.left);
  14. }
  15. }
  16. }

2.1 递归中序遍历

  1. //递归中序遍历
  2. public void inOrderRec(Node temp) {
  3. if(temp == null) {
  4. System.out.println("BinaryTree is empty.");
  5. }else {
  6. if(temp.left != null) {
  7. inOrderRec(temp.left);
  8. }
  9. System.out.print(temp.data + " ");
  10. if(temp.right != null) {
  11. inOrderRec(temp.right);
  12. }
  13. }
  14. }

2.2 非递归中序遍历

和非递归前序遍历算法一类似,只是输出数据的位置不同。

  1. //非递归中序遍历
  2. public void inOrderNotRec() {
  3. Stack<Node> stack = new Stack<Node>();
  4. Node temp = root;
  5. while(!stack.isEmpty() || temp != null) {
  6. if(temp != null) {
  7. stack.push(temp);
  8. temp = temp.left;
  9. }else {
  10. temp = stack.pop();
  11. System.out.print(temp.data + " ");
  12. temp = temp.right;
  13. }
  14. }
  15. }

3.1 递归后序遍历

  1. //递归后序遍历
  2. public void postOrderRec(Node temp) {
  3. if(temp == null) {
  4. System.out.println("BinaryTree is empty.");
  5. }else {
  6. if(temp.left != null) {
  7. postOrderRec(temp.left);
  8. }
  9. if(temp.right != null) {
  10. postOrderRec(temp.right);
  11. }
  12. System.out.print(temp.data + " ");
  13. }
  14. }

3.2 非递归后序遍历算法一

算法思路:后序遍历的难点在于需要判断是从左子树返回还是从右子树返回。若从左子树返回则先遍历右子树,若从右子树返回直接输出。因此我们用里一个栈记录是从左子树返回还是从右子树返回。

具体思路参考:https://www.bilibili.com/video/av23885544

  1. //非递归后序遍历算法一
  2. public void postOrderNotRec2() {
  3. Stack<Node> s1 = new Stack<Node>();
  4. //用另一个栈记录是从左子树返回还是从右子树返回
  5. Stack<Boolean> s2 = new Stack<Boolean>();
  6. Node temp = root;
  7. boolean flag;
  8. while(!s1.isEmpty() || temp != null) {
  9. if(temp != null) {
  10. s1.push(temp);
  11. s2.push(false);
  12. temp = temp.left;
  13. }else {
  14. temp = s1.pop();
  15. flag = s2.pop();
  16. //false代表从左子树返回,此时需要先到右子树,并将tag改为true
  17. if(!flag) {
  18. s1.push(temp);
  19. s2.push(true);
  20. temp = temp.right;
  21. }else {
  22. //从右子树返回说明该节点的左右子树的遍历过了,直接输出
  23. System.out.print(temp.data + " ");
  24. //此时temp位于子二叉树的根节点,不为空,将其手动置空,否则出现死循环
  25. temp = null;
  26. }
  27. }
  28. }
  29. }

3.3 非递归后序遍历算法二

非常巧妙,也是用两个栈。
算法思路:
1,将根节点压入栈s1。
2,从s1中弹出节点记为temp,并将temp压入栈s2。然后将temp的左子节点和右子节点分别入栈。
3,重复步骤2直至栈s1为空。
4,从栈s2中取出数据并打印,打印的顺序就是后序遍历的顺序。

  1. //非递归后序遍历算法二
  2. public void postOrderNotRec1() {
  3. Stack<Node> s1 = new Stack<Node>();
  4. Stack<Node> s2 = new Stack<Node>();
  5. Node temp = root;
  6. s1.push(temp);
  7. while(!s1.isEmpty()) {
  8. temp = s1.pop();
  9. s2.push(temp);
  10. if(temp.left != null) {
  11. s1.push(temp.left);
  12. }
  13. if(temp.right != null) {
  14. s1.push(temp.right);
  15. }
  16. }
  17. while(!s2.isEmpty()) {
  18. System.out.print(s2.pop().data + " ");
  19. }
  20. }

4 层次遍历

  1. //层次遍历
  2. public void levelOrder() {
  3. ArrayDeque<Node> quene = new ArrayDeque<Node>();
  4. Node temp = this.root;
  5. quene.add(temp);
  6. while(!quene.isEmpty()) {
  7. temp = quene.poll();
  8. System.out.print(temp.data + " ");
  9. if(temp.left != null) {
  10. quene.add(temp.left);
  11. }
  12. if(temp.right != null) {
  13. quene.add(temp.right);
  14. }
  15. }
  16. }

发表评论

表情:
评论列表 (有 0 条评论,91人围观)

还没有评论,来说两句吧...

相关阅读