-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
180 lines (158 loc) · 3.67 KB
/
BinaryTree.java
File metadata and controls
180 lines (158 loc) · 3.67 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
package ds;
import java.util.ArrayList;
import java.util.Date;
/*
* �������Ҫ��--20160317
* һ������������������
* 1��������
* 2������
* 3������+������
* 4�������ĸ߶ȣ���ȣ�ÿһ��Ŀ��
*
*/
public class BinaryTree {
private Node root;
static char ch[];
static int i = 0;
static int[] a = { 2, 1, 12, 45, 21, 6, 111 };
public BinaryTree() {
root = null;
}
private class Node {
private Node left;
private Node right;
private int data;
/* ���캯�� */
public Node(int data) {
this.left = null;
this.right = null;
this.data = data;
}
public Node(char data){
this.left = null;
this.right = null;
this.data = data;
}
}
/* ������ --����һ���� */
public Node Create() {
Node node = null;
if (i < ch.length - 1) {
if (ch[i++] == '#') {
}
node = new Node(ch[i++] - '0');
node.left = Create();
node.right = Create();
}
return node;
}
/* ����ط�������*/
public void create(Node node) {
if (i < ch.length) {
if (ch[i] == '#') {
node.left = null;
i++;
}
if (root == null) {
root = new Node(ch[i] - '0');
i++;
node = root;
}
node.left = new Node(ch[i] - '0');
i++;
create(node.left);
node.right = new Node(ch[i] - '0');
i++;
create(node.right);
}
}
/* ���ķ�ת */
public void InvertTree(Node node) {
if (node == null) {
return;
} else {
Node tmp = node.right;
node.right = node.left;
node.left = tmp;
InvertTree(node.left);
InvertTree(node.right);
}
}
/**
* ǰ�����
*
* @param node
*/
public void preOrder(Node node) {
if (node != null) {
System.out.println(node.data);
preOrder(node.left);
preOrder(node.right);
}
}
/**
* �������
*
* @param node
*/
public void inOrder(Node node) {
if (node != null) {
inOrder(node.left);
System.out.println(node.data);
inOrder(node.right);
}
}
/**
* �������
*
* @param node
*/
public void postOrder(Node node) {
if (node != null) {
postOrder(node.left);
postOrder(node.right);
System.out.println(node.data);
}
}
/*�Ⲣ���Ƕ���������-->��������˸ij��˶����������� */
public void BinarySortTree(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 {
BinarySortTree(node.left, data);
}
} else {
if (node.right == null) {
node.right = new Node(data);
} else {
BinarySortTree(node.right, data);
}
}
}
}
public static void main(String[] args) {
BinaryTree bt = new BinaryTree();
bt.ch = "12#3##45###".toCharArray();
System.out.println(ch);
System.out.println("����������");
/* ����ǵݹ��������������ķ���������Node ���͵� */
bt.root = bt.Create();
bt.preOrder(bt.root);
System.out.println("��һ�ֽ����������ķ���");
/* ������ͼ����һ��û�з���ֵ�ģ��� */
/*
* bt.root = null; bt.create(bt.root); bt.preOrder(bt.root);
*/
System.out.println("��������������");
/* �����ǽ���һ�Ŷ��������� */
bt.root = null;
for (int i = 0; i < a.length; i++) {
bt.BinarySortTree(bt.root, a[i]);
}
bt.inOrder(bt.root);
}
}