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

本文介绍了如何使用Java来创建和遍历二叉树,包括递归方式实现的先序、中序和后序遍历,以及非递归方式实现的先序和中序遍历。同时提供了插入节点的方法和计算树的大小。
摘要由CSDN通过智能技术生成

1 packagetest.tree;2

3 importjava.util.ArrayList;4 importjava.util.Stack;5

6 public classBinaryTree {7 privateTreeNode root;8 private int size = 0;9

10 publicBinaryTree() {11 }12

13 public BinaryTree(intvalue) {14 this.root = newTreeNode(value);15 this.size = 1;16 }17

18 publicTreeNode getRoot() {19 returnroot;20 }21

22

23 /**

24 * 先序遍历递归实现25 *@paramnode26 *@paramcontainer27 */

28 public void preOrderTreeWalk(TreeNode node, ArrayListcontainer) {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 *@paramroot39 *@paramcontainer40 */

41 public void prePrint2(TreeNode root, ArrayListcontainer) {42 Stack stack = new Stack();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 *@paramnode59 *@paramcontainer60 */

61 public void midOrderTreeWalk(TreeNode node, ArrayListcontainer) {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 *@paramroot72 *@paramcontainer73 */

74 public void inPrint2(TreeNode root, ArrayListcontainer) {75 Stack stack = new Stack();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 *@paramnode91 *@paramcontainer92 */

93 public void postOrderTreeWalk(TreeNode node, ArrayListcontainer) {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 *@paramnode104 *@paramcontainer105 */

106 public void postPrintTree(TreeNode node, ArrayListcontainer) {107

108 Stack stack = new Stack();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 (inti : data) {134 insert(i);135 }136 }137 }138

139 public void insert(intvalue) {140 if (root == null) { //当前没有根节点,则数组中的第一个数是根节点

141 root = newTreeNode(value);142 } else{143 TreeNode curNode = root; //根节点

144 TreeNode parentNode;145 while (true) {146 parentNode = curNode; //curNode:root

147 if (value

149 if (curNode == null) {150 parentNode.leftChildNode = newTreeNode(value);151 //parentNode.leftChildNode.leftOrRight = -1;

152 break;153 }154 } else{155 curNode =curNode.rightChildNode;156 if (curNode == null) {157 parentNode.rightChildNode = newTreeNode(value);158 //parentNode.rightChildNode.leftOrRight = 1;

159 break;160 }161 }162 }163 }164 ++size;165 }166

167

168 public intsize() {169 return this.size;170 }171

172 public booleanisEmpty() {173 return size == 0;174 }175 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值