-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructBinaryTree.java
More file actions
257 lines (243 loc) · 5.29 KB
/
Copy pathConstructBinaryTree.java
File metadata and controls
257 lines (243 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package com.wg.offerAlgorithm;
import java.util.Stack;
/*
* 重建二叉树
* 二叉树的创建,遍历
*
*/
public class ConstructBinaryTree {
private Node root;
private class Node{
private int data;
private Node left;
private Node right;
public Node(int data){
this.data=data;
this.left=null;
this.right=null;
}
}
public ConstructBinaryTree(){
root=null;
}
//递归构建树
public void buildTree(Node node,int data){
if( root==null){
root=new Node(data);
}else{
if(data<node.data){
if(node.left==null){
node.left=new Node(data);
}else{
buildTree(node.left,data);
}
}if(data>node.data){
if(node.right==null){
node.right=new Node(data);
}else{
buildTree(node.right,data);
}
}
}
}
/**
* 前序遍历
* @param node
*/
public void preOrder(Node node){
if(node != null){
System.out.print(node.data+" ");
preOrder(node.left);
preOrder(node.right);
}
}
/**
* 非递归二叉树前序遍历
* 利用栈实现
* @param node
*/
public void preOrder_(Node node){
Stack<Node> stack = new Stack<Node>();
if(node!=null){
stack.push(node);
while(!stack.empty()){
node=stack.pop();
System.out.print(node.data+" ");
if(node.right!=null){
stack.push(node.right);
}
if(node.left!=null){
stack.push(node.left);
}
}
}
}
/**
* 中序遍历
* @param node
*/
public void inOrder(Node node){
if(node != null){
inOrder(node.left);
System.out.print(node.data+" ");
inOrder(node.right);
}
}
/**
* 中序非递归遍历
* @param node
*/
public void inOrder_(Node node){
Stack<Node> stack = new Stack<Node>();
while(node!=null ||stack.size()>0){
while(node!=null){
stack.push(node);
node=node.left;
}
if(stack.size()>0){
node=stack.pop();
System.out.print(node.data+" ");
node = node.right;
}
}
}
public void in_Order(Node node){
Stack<Node> stack = new Stack<Node>();
while(node!=null||stack.size()>0){
while(node!=null){
//根元素压栈
stack.push(node);
node=node.left;
}
if(stack.size()>0){
//出栈
node=stack.pop();
System.out.println(node.data);
node=node.right;
}
}
}
/**
* 后序遍历
* @param node
*/
public void postOrder(Node node){
if(node != null){
postOrder(node.left);
postOrder(node.right);
System.out.print(node.data+" ");
}
}
/**
* 后续非递归遍历
* @param node
*/
public void postOrder_(Node node){
Stack<Node> stack = new Stack<Node>();
Node prve=node;
while(node!=null||stack.size()>0){
while(node!=null){
stack.push(node);
node = node.left;
}
if(stack.size()>0){
Node temp = stack.peek().right;
if(temp==null||temp==prve){
node = stack.pop();
System.out.print(node.data+" ");
prve =node;
node=null;
}else{
node = temp;
}
}
}
}
/**
* 二叉树插入节点
* @param data
*/
public void insert(int data){
Node newNode = new Node(data);
if(root==null){
root = newNode;
}
else{
Node current=root;
Node parent;
while(true){
parent=current;
//左子树
if(current.data>data){
current=current.left;
if(current==null){
parent.left=newNode;
return;
}
}
else{
current =current.right;
if(current==null){
parent.right=newNode;
return;
}
}
System.out.println(parent.data);
}
}
}
/**
* 查找最小值
* @param root
* @return
*/
public Node minimum(Node root){
Node last=null;
Node current= root;
while(current!=null){
last=current;
current = current.left;
}
return last;
}
/**
* 查找最大值
* @param root
* @return
*/
public Node maxmum(Node root){
Node last=null;
Node current=root;
while(current!=null){
last=current;
current=current.right;
}
return last;
}
/**
* 删除节点
* @param node
*/
public void deleteNode(Node node){
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// int[] a = {2,4,12,45,21,6,111};
int[] a={12,76,35,22,16,48,90,46,9,40};
ConstructBinaryTree bTree = new ConstructBinaryTree();
for (int i = 0; i < a.length; i++) {
bTree.buildTree(bTree.root, a[i]);
}
// bTree.insert(20);
// Node last=bTree.minimum(bTree.root);
// System.out.println(last.data);
// bTree.preOrder_(bTree.root);
// System.out.println();
bTree.in_Order(bTree.root);
System.out.println();
// bTree.inOrder(bTree.root);
// System.out.println();
// bTree.postOrder_(bTree.root);
}
}
//