-
Notifications
You must be signed in to change notification settings - Fork 0
/
二叉查找树的建立,遍历.java
93 lines (85 loc) · 2.67 KB
/
二叉查找树的建立,遍历.java
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
public class class3 {
static class Node {
int value;
Node leftChild = null;
Node rightChild = null;
Node(int value) {
this.value = value;
}
public void display() {
System.out.print(this.value + "\t");
}
@Override
public String toString() {
// TODO Auto-generated method stub
return String.valueOf(value);
}
}
static class Tree {
private Node root = null;
public Node find(int key) {
if (root == null) {
System.out.println("The tree is empty!");
return null;
}
Node current = root;
while (current.value != key) {
if (key > current.value)
current = current.rightChild;
else
current = current.leftChild;
if (current == null)
return null;
}
return current;
}
public boolean insert(Node node) {
if (root == null) {
root = node;
return true;
}
//树中不允许插入重复的数据项
if (this.find(node.value) != null) {
System.out.println("Node with id '" +
node.value + "' has already existed!");
return false;
}
Node current = root;
while (current != null) {
if (node.value > current.value) {
if (current.rightChild == null) {
current.rightChild = node;
return true;
}
current = current.rightChild;
} else {
if (current.leftChild == null) {
current.leftChild = node;
return true;
}
current = current.leftChild;
}
}
return false;
}
//前序遍历,根左右
public void preorderIterator(Node node) {
System.out.println(node.value);
if(node.leftChild != null){
preorderIterator(node.leftChild);
}
if(node.rightChild != null){
preorderIterator(node.rightChild);
}
}
}
public static void main(String[] args){
Tree tree = new Tree();
int[] ints = {6,2,8,1,4,3};
for(int i=0;i<ints.length;i++){
Node news = new Node(ints[i]);
tree.insert(news);
}
tree.preorderIterator(tree.root);
}
}