Java创建、遍历(递归+非递归)二叉树


输入:一个先序的数组,将数组中的元素构建成为二叉树

  1 package test.tree;
  2 
  3 import java.util.ArrayList;
  4 import java.util.Stack;
  5 
  6 public class BinaryTree {
  7     private TreeNode root;
  8     private int size = 0;
  9 
 10     public BinaryTree() {
 11     }
 12 
 13     public BinaryTree(int value) {
 14         this.root = new TreeNode(value);
 15         this.size = 1;
 16     }
 17 
 18     public TreeNode getRoot() {
 19         return root;
 20     }
 21 
 22 
 23     /**
 24      * 先序遍历递归实现
 25      * @param node
 26      * @param container
 27      */
 28     public void preOrderTreeWalk(TreeNode node, ArrayList<Integer> container) {
 29         if (node != null) {
 30             container.add(node.value);
 31             preOrderTreeWalk(node.leftChildNode, container);
 32             preOrderTreeWalk(node.rightChildNode, container);
 33         }
 34     }
 35 
 36     /**
 37      * 非递归实现先序遍历
 38      * @param root
 39      * @param container
 40      */
 41     public void prePrint2(TreeNode root, ArrayList<Integer> container) {
 42         Stack<TreeNode> stack = new Stack<TreeNode>();
 43         while(root != null || !stack.isEmpty()) {
 44             if(root != null) {
 45                 container.add(root.value);
 46                 stack.push(root);
 47                 root = root.leftChildNode;
 48             }
 49             else if(!stack.isEmpty()) { //pNode == null, !stack.isEmpty
 50                 TreeNode node = stack.pop();
 51                 root = node.rightChildNode;
 52             }
 53         }
 54     }
 55 
 56     /**
 57      * 递归实现中序遍历
 58      * @param node
 59      * @param container
 60      */
 61     public void midOrderTreeWalk(TreeNode node, ArrayList<Integer> container) {
 62         if (node != null) {
 63             midOrderTreeWalk(node.leftChildNode, container);
 64             container.add(node.value);
 65             midOrderTreeWalk(node.rightChildNode, container);
 66         }
 67     }
 68 
 69     /**
 70      * 非递归实现中序遍历
 71      * @param root
 72      * @param container
 73      */
 74     public void inPrint2(TreeNode root, ArrayList<Integer> container) {
 75         Stack<TreeNode> stack = new Stack<TreeNode>();
 76         while(root != null || !stack.isEmpty()) {
 77             if(root != null) {
 78                 stack.push(root);
 79                 root = root.leftChildNode;
 80             } else {
 81                 TreeNode node = stack.pop();
 82                 container.add(node.value);
 83                 root = node.rightChildNode;
 84             }
 85         }
 86     }
 87 
 88     /**
 89      * 递归实现后序遍历
 90      * @param node
 91      * @param container
 92      */
 93     public void postOrderTreeWalk(TreeNode node, ArrayList<Integer> container) {
 94         if (node != null) {
 95             postOrderTreeWalk(node.leftChildNode, container);
 96             postOrderTreeWalk(node.rightChildNode, container);
 97             container.add(node.value);
 98         }
 99     }
100 
101     /**
102      * 非递归实现后序遍历
103      * @param node
104      * @param container
105      */
106     public void postPrintTree(TreeNode node, ArrayList<Integer> container) {
107 
108         Stack<TreeNode> stack = new Stack<TreeNode>();
109         TreeNode p = node, prev = node;
110         while(p != null || !stack.isEmpty()) {
111             while(p != null) {
112                 stack.push(p);
113                 p = p.leftChildNode;
114             }
115 
116             if(!stack.isEmpty()) {
117                 TreeNode tmp = stack.peek().rightChildNode;
118                 if(tmp == null || tmp == prev) {
119                     p = stack.pop();
120                     container.add(p.value);
121                     prev = p;
122                     p = null;
123                 } else {
124                     p = tmp;
125                 }
126             }
127         }
128 
129     }
130 
131     public void createBinaryTree(int[] data) {
132         if (data != null) {
133             for (int i : data) {
134                 insert(i);
135             }
136         }
137     }
138 
139     public void insert(int value) {
140         if (root == null) { //当前没有根节点,则数组中的第一个数是根节点
141             root = new TreeNode(value);
142         } else {
143             TreeNode curNode = root; //根节点
144             TreeNode parentNode;
145             while (true) {
146                 parentNode = curNode; //curNode:root
147                 if (value < curNode.value) {
148                     curNode = curNode.leftChildNode; //小于根节点,就在是root的左孩子结点
149                     if (curNode == null) {
150                         parentNode.leftChildNode = new TreeNode(value);
151 //                        parentNode.leftChildNode.leftOrRight = -1;
152                         break;
153                     }
154                 } else {
155                     curNode = curNode.rightChildNode;
156                     if (curNode == null) {
157                         parentNode.rightChildNode = new TreeNode(value);
158 //                        parentNode.rightChildNode.leftOrRight = 1;
159                         break;
160                     }
161                 }
162             }
163         }
164         ++size;
165     }
166 
167 
168     public int size() {
169         return this.size;
170     }
171 
172     public boolean isEmpty() {
173         return size == 0;
174     }
175 }

测试

 1 package test.tree;
 2 
 3 import java.util.ArrayList;
 4 
 5 public class TestBinTree {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         int[] data = { 21, 25, 16, 32, 22, 19, 13, 20};
12         BinaryTree bTree = new BinaryTree();
13         bTree.createBinaryTree(data);
14         ArrayList<Integer> container = new ArrayList<Integer>();
15         TreeNode root = bTree.getRoot();
16         container.clear();
17         bTree.preOrderTreeWalk(root, container);
18         System.out.println("递归先序遍历:  " + container);
19         container.clear();
20         bTree.prePrint2(root, container);
21         System.out.println("非递归先序遍历:"+container);
22         container.clear();
23         System.out.println("----------------------------------------------");
24         bTree.midOrderTreeWalk(root, container);
25         System.out.println("递归中序遍历:  " + container);
26         container.clear();
27         bTree.inPrint2(root, container);
28         System.out.println("非递归中序遍历:"+container);
29         container.clear();
30         System.out.println("----------------------------------------------");
31         bTree.postOrderTreeWalk(root, container);
32         System.out.println("递归后续遍历:  " + container);
33         container.clear();
34         bTree.postPrintTree(root, container);
35         System.out.println("非递归后序遍历:" + container);
36 
37     }
38 }

结果

1 递归先序遍历:  [21, 16, 13, 19, 20, 25, 22, 32]
2 非递归先序遍历:[21, 16, 13, 19, 20, 25, 22, 32]
3 ----------------------------------------------
4 递归中序遍历:  [13, 16, 19, 20, 21, 22, 25, 32]
5 非递归中序遍历:[13, 16, 19, 20, 21, 22, 25, 32]
6 ----------------------------------------------
7 递归后续遍历:  [13, 20, 19, 16, 22, 32, 25, 21]
8 非递归后序遍历:[13, 20, 19, 16, 22, 32, 25, 21]

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM