-
Notifications
You must be signed in to change notification settings - Fork 1
/
二叉树js.html
64 lines (54 loc) · 1.28 KB
/
二叉树js.html
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
function Node(data,left,right) {
this.data = data;
this.left = left;
this.right = right;
}
function BST() {
this.root = null;
this.insert = insert;
// this.inOrder = inOrder;
}
function insert(data) {
var n = new Node(data,null,null);
if(this.root == null) {
this.root = n;
}else {
var current = this.root;
var parent;
while(current) {
parent = current;
parent.Father = current;
if(data < current.data) {
current = current.left;
if(current == null) {
parent.left = n;
break;
}
}else {
current = current.right;
if(current == null) {
parent.right = n;
break;
}
}
}
}
}
var nums = new BST();
nums.insert(23);
nums.insert(45);
nums.insert(16);
nums.insert(37);
nums.insert(3);
nums.insert(99);
nums.insert(122);
nums.insert(86);
nums.insert(20);
nums.insert(5);
nums.insert(19);
nums.insert(43);
nums.insert(454)
// 23
// 16 45
// 3 20 37 99
// 5 19 43 86 122