# 二叉树
function Node(value) {
this.value = value;
this.left =this.left=null;
this.show = show
}
function show () {
return this.value;
}
function BST () {
this.root = null;
this.insert = insert;
this.inOrder = inOrder;
this.getSmall = getSmall;
this.getMax = getMax;
this.find = find;
this.remove = remove;
}
function insert (value) {
let n = new Node(value);
if(this.root==null) {
this.root = n;
}else {
let current = this.root;
let parent ;
while(true){
parent = current;
if(value<current.value){
current = current.left;
if(current==null) {
parent.left = n;
break;
}
}else {
current = current.right;
if(current==null) {
parent.right = n;
break;
}
}
}
}
}
function inOrder (node) {
if(node!=null) {
inOrder(node.left)
console.log(node.value)
inOrder(node.right)
}
}
function levelOrder(node,depth) {
if (root !== null) {
if (!res[depth]) {
res[depth] = []
}
levelOrder(root.left, depth + 1)
res[depth].push(root.val)
levelOrder(root.right, depth + 1)
}
}
function getSmall (root) {
let current = this.root || root;
while(current.left!=null) {
current = current.left;
}
return current
}
function getMax (root) {
let current = this.root || root;
while(current.right!=null) {
current = current.right;
}
return current
}
function find (value) {
let current = this.root
while(current!=null) {
if(current.value === value) {
return current;
}else if(value<current.value){
current = current.left;
}else {
current = current.right;
}
}
}
function remove (value) {
removeNode(this.root,value);
}
function removeNode (node,value) {
if (node==null) return null;
if (value==node.value) {
if(node.left==null && node.right==null) return null;
if(node.left==null) return node.right;
if(node.right==null) return node.left;
let tempNode = getSmall(node.right)
node.value = tempNode.value;
node.right = removeNode(node.right,tempNode.value);
return node;
}else if(value<current.value) {
node.left = removeNode(node.left,value);
return node;
}else {
node.right = removeNode(node.right,value);
return node;
}
}
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
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