二叉树的遍历(递归与非递归)java实现

本文详细介绍了二叉树的先序、中序和后序遍历算法,包括递归和非递归(栈)两种实现方式,并提供了完整的Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

三种遍历的递归实现:
[java]  view plain  copy
  1. public class Node {//二叉树节点  
  2.       
  3.     private int data;  
  4.     private Node leftNode;  
  5.     private Node rightNode;  
  6.       
  7.     public Node(int data, Node leftNode, Node rightNode){  
  8.         this.data = data;  
  9.         this.leftNode = leftNode;  
  10.         this.rightNode = rightNode;  
  11.     }  
  12.       
  13.     public int getData(){  
  14.         return data;  
  15.     }  
  16.       
  17.     public void setData(int data){  
  18.         this.data = data;  
  19.     }  
  20.       
  21.     public Node getLeftNode(){  
  22.         return leftNode;  
  23.     }  
  24.       
  25.     public void setLeftNode(Node leftNode){  
  26.         this.leftNode = leftNode;  
  27.     }  
  28.       
  29.     public Node getRightNode(){  
  30.         return rightNode;  
  31.     }  
  32.       
  33.     public void setRightNode(Node rightNode){  
  34.         this.rightNode = rightNode;  
  35.     }  
  36.       
  37. }  

三种遍历的递归实现:
[java]  view plain  copy
  1. public class BinaryTree_DiGui {  
  2.       
  3.     /* 
  4.      * 二叉树先序中序后序排序 
  5.      * 方式:递归。 
  6.      */  
  7.       
  8.     //注意必须逆序简历,先建立子节点,再逆序往上建立,  
  9.     //因为非叶子节点会使用到下面的节点,而初始化是按顺序初始化得,不逆序建立会报错  
  10.     public static Node init(){  
  11.         Node J = new Node(8nullnull);  
  12.         Node H = new Node(4nullnull);  
  13.         Node G = new Node(2nullnull);  
  14.         Node F = new Node(7null, J);  
  15.         Node E = new Node(5, H, null);  
  16.         Node D = new Node(1null, G);  
  17.         Node C = new Node(9, F, null);  
  18.         Node B = new Node(3, D, E);  
  19.         Node A = new Node(6, B, C);  
  20.         return A;  //返回根节点  
  21.     }  
  22.       
  23.     //打印节点数值  
  24.     public static void printNode(Node node){  
  25.         System.out.print(node.getData());  
  26.     }  
  27.       
  28.       
  29.     //先序遍历  
  30.     public static void preOrder(Node root){  
  31.           
  32.         printNode(root);//打印根节点  
  33.           
  34.         if(root.getLeftNode() != null){//使用递归遍历左孩子  
  35.             preOrder(root.getLeftNode());  
  36.         }  
  37.         if(root.getRightNode() != null){//使用递归遍历右孩子  
  38.             preOrder(root.getRightNode());  
  39.         }  
  40.     }  
  41.       
  42.       
  43.     //中序遍历  
  44.     public static void inOrder(Node root){  
  45.           
  46.         if(root.getLeftNode() != null){//使用递归遍历左孩子  
  47.             inOrder(root.getLeftNode());  
  48.         }  
  49.           
  50.         printNode(root);//打印根节点  
  51.           
  52.         if(root.getRightNode() != null){//使用递归遍历右孩子  
  53.             inOrder(root.getRightNode());  
  54.         }  
  55.     }  
  56.       
  57.       
  58.     //后续遍历  
  59.     public static void postOrder(Node root){  
  60.           
  61.         if(root.getLeftNode() != null){//使用递归遍历左孩子  
  62.             postOrder(root.getLeftNode());  
  63.         }  
  64.           
  65.         if(root.getRightNode() != null){//使用递归遍历右孩子  
  66.             postOrder(root.getRightNode());  
  67.         }  
  68.           
  69.         printNode(root);//打印根节点  
  70.     }  
  71.       
  72.       
  73.     public static void main(String[] args){  
  74. //      BinaryTree tree = new BinaryTree();//注释掉本行后类中方法需变为static  
  75.         Node root = init();  
  76.           
  77.         System.out.println("先序遍历");  
  78.         preOrder(root);  
  79.         System.out.println("");  
  80.           
  81.         System.out.println("中序遍历");  
  82.         inOrder(root);  
  83.         System.out.println("");  
  84.           
  85.         System.out.println("后序遍历");  
  86.         postOrder(root);  
  87.         System.out.println("");  
  88.           
  89.     }  
  90.       
  91. }  



通过栈实现三种遍历(非递归):
[java]  view plain  copy
  1. public class BinaryTree_Zhan {  
  2.       
  3.     /* 
  4.      *  
  5.      * 二叉树先序中序后序排序 
  6.      * 方式:采用非递归方式。 
  7.      */  
  8.       
  9.     //注意必须逆序简历,先建立子节点,再逆序往上建立,  
  10.     //因为非叶子节点会使用到下面的节点,而初始化是按顺序初始化得,不逆序建立会报错  
  11.     public static Node init(){  
  12.         Node J = new Node(8nullnull);  
  13.         Node H = new Node(4nullnull);  
  14.         Node G = new Node(2nullnull);  
  15.         Node F = new Node(7null, J);  
  16.         Node E = new Node(5, H, null);  
  17.         Node D = new Node(1null, G);  
  18.         Node C = new Node(9, F, null);  
  19.         Node B = new Node(3, D, E);  
  20.         Node A = new Node(6, B, C);  
  21.         return A;  //返回根节点  
  22.     }  
  23.       
  24.     //打印节点数值  
  25.     public static void printNode(Node node){  
  26.         System.out.print(node.getData());  
  27.     }  
  28.       
  29.       
  30.     public static void preOrder_stack(Node root){//先序遍历  
  31.           
  32.         Stack<Node> stack = new Stack<Node>();  
  33.         Node node = root;  
  34.           
  35.         while(node != null || stack.size()>0){//将所有左孩子压栈  
  36.             if(node != null){//压栈之前先访问  
  37.                 printNode(node);  
  38.                 stack.push(node);  
  39.                 node = node.getLeftNode();  
  40.                   
  41.             }else{  
  42.                 node = stack.pop();  
  43.                 node = node.getRightNode();  
  44.             }  
  45.         }  
  46.     }  
  47.       
  48.       
  49.     public static void inOrder_Stack(Node root){//中序遍历  
  50.           
  51.         Stack<Node> stack = new Stack<Node>();  
  52.         Node node = root;  
  53.           
  54.         while(node != null || stack.size()>0){  
  55.             if(node != null){  
  56.                 stack.push(node);//直接压栈  
  57.                 node = node.getLeftNode();  
  58.             }else{  
  59.                 node = stack.pop();//出栈并访问  
  60.                 printNode(node);  
  61.                 node = node.getRightNode();  
  62.             }  
  63.         }  
  64.     }  
  65.       
  66.       
  67.     public static void postOrder_Stack(Node root){//后续遍历  
  68.           
  69.         Stack<Node> stack = new Stack<Node>();  
  70.         Stack<Node> output = new Stack<Node>();//构造一个中间栈来存储逆后续遍历的结果  
  71.         Node node = root;  
  72.         while(node != null || stack.size()>0){  
  73.             if(node != null){  
  74.                 output.push(node);  
  75.                 stack.push(node);  
  76.                 node = node.getRightNode();  
  77.             }else{  
  78.                 node = stack.pop();  
  79.                 node = node.getLeftNode();  
  80.             }  
  81.         }  
  82.           
  83.         while(output.size()>0){  
  84.             printNode(output.pop());  
  85.         }  
  86.           
  87.     }  
  88.       
  89.       
  90.     public static void main(String[] args){  
  91.           
  92.         Node root = init();  
  93.           
  94.         System.out.println("先序遍历");  
  95.         preOrder_stack(root);  
  96.         System.out.println("");  
  97.           
  98.         System.out.println("中序遍历");  
  99.         inOrder_Stack(root);   
  100.         System.out.println("");  
  101.           
  102.         System.out.println("后序遍历");  
  103.         postOrder_Stack(root);  
  104.         System.out.println("");  
  105.     }  
  106.   
  107. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值