-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.js
154 lines (149 loc) · 4.05 KB
/
tree.js
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
function Node(data, left, right) {
this.data = data;
this.left = left;
this.right = right;
}
Node.prototype.show = function () {
return this.data;
}
function BST() {
this.root = null;
}
//插入元素
BST.prototype.insert = function (data) {
var n = new Node(data, null, null);
if(this.root == null) {
this.root = n;
}else {
var current = this.root,
parent = current;
while(true) {
parent = 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;
}
}
}
}
}
//中序遍历
BST.prototype.inOrder = function (node) {
if(node !== null) {
this.inOrder(node.left);
console.log(node.show());
this.inOrder(node.right);
}
}
//先序遍历
BST.prototype.preOrder = function (node) {
if(node !== null) {
console.log(node.show());
this.preOrder(node.left);
this.preOrder(node.right);
}
}
//后序遍历
BST.prototype.postOrder = function (node) {
if(node !== null) {
this.postOrder(node.left);
this.postOrder(node.right);
console.log(node.show());
}
}
//查找最小值
BST.prototype.getMin = function () {
var current = this.root;
while(current.left !== null) {
current = current.left;
}
return current.data;
}
//查找最大值
BST.prototype.getMax = function () {
var current = this.root;
while(current.right !== null) {
current = current.right;
}
return current.data;
}
//查找给定值
BST.prototype.find = function (data) {
var current = this.root;
while(current !== null) {
if(data < current.data) {
current = current.left;
}else if(data > current.data) {
current = current.right;
}else {
return current;
}
}
return null;
}
//删除节点
BST.prototype.remove = function (data) {
this.root = this.removeNode(this.root, data);
}
BST.prototype.removeNode = function (node, data) {
if(node == null) {
return null;
}
if(node.data == data) {
//以下语句第一个判断必须作为第一个判断,否为会出现逻辑判断错误
if(node.left == null && node.right == null) {//没有子节点
return null;
}else if(node.left == null) {//没有左子节点
return node.right;
}else if(node.right == null) {//没有右子节点
return node.left;
}else {//有两个子节点
var tempNode = getSmallest(node.right);
node.data = tempNode.data;
node.right = this.removeNode(node.right, tempNode.data);
return node;
}
}else if(data < node.data) {
node.left = this.removeNode(node.left, data);
return node;
}else {
node.right = this.removeNode(node.right, data);
return node;
}
}
function getSmallest(node) {
var current = node;
while(current.left !== null) {
current = current.left;
}
return current;
}
var nums = new BST();
nums.insert(23);
nums.insert(45);
nums.insert(16);
nums.insert(37);
nums.insert(3);
nums.insert(99);
nums.insert(22);
console.log("inOrder traversal:");
nums.inOrder(nums.root);
console.log('preOrder traversal:');
nums.preOrder(nums.root);
console.log('postOrder traversal:');
nums.postOrder(nums.root);
console.log('The minimum value of the BST is: ' + nums.getMin());
console.log('The maximum value of the BST is: ' + nums.getMax());
console.log('search for 16 in the BST is: ', nums.find(16));
console.log('search for 100 in the BST is: ', nums.find(100));
console.log('remove the data 45, inOrder traversal: ');
nums.remove(45);
nums.inOrder(nums.root);