-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTrees.html
More file actions
160 lines (153 loc) · 3.29 KB
/
BinaryTrees.html
File metadata and controls
160 lines (153 loc) · 3.29 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
<script>
function Node(data, left, right){
this.data = data;
this.left = left;
this.right = right;
}
Node.prototype.show = function(){
return this.data;
}
function BinaryTrees(){
this.root = null;
}
BinaryTrees.prototype = {
insert: function(data){
var n = new Node(data, null, null);
if (this.root == null) {
this.root = n;
}
else {
var current = this.root;
var parent;
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;
}
}
}
}
},
inOrder: function(node) {
if (!(node == null)) {
this.inOrder(node.left);
document.writeln(node.show() + " ");
this.inOrder(node.right);
}
},
preOrder: function(node) {
if (!(node == null)) {
document.writeln(node.show() + " ");
this.preOrder(node.left);
this.preOrder(node.right);
}
},
Postorder: function(node){
if (!(node == null)) {
this.preOrder(node.left);
this.preOrder(node.right);
document.writeln(node.show() + " ");
}
},
getMin: function(){
var current = this.root;
while(!(current.left == null)){
current = current.left;
}
return current.data;
},
getMax: function(){
var current = this.root;
while (!(current.right == null)) {
current = current.right;
}
return current.data;
},
find: function(data){
var current = this.root;
while(current != null){
if(current.data == data){
return current;
} else if(data < current.data){
current = current.left;
} else {
current = current.right;
}
}
return null;
},
remove : function(data) {
this.root = removeNode(this.root, data);
},
removeNode: function(node, data) {
if (node == null) {
return null;
}
if (data == node.data) {
// 没有子节点的节点
if (node.left == null && node.right == null) {
return null;
}
// 没有左子节点的节点
if (node.left == null) {
return node.right;
}
// 没有右子节点的节点
if (node.right == null) {
return node.left;
}
// 有两个子节点的节点
var tempNode = getSmallest(node.right);
node.data = tempNode.data;
node.right = removeNode(node.right, tempNode.data);
return node;
} else if (data < node.data) {
node.left = removeNode(node.left, data);
return node;
} else {
node.right = removeNode(node.right, data);
return node;
}
}
}
var nums = new BinaryTrees();
nums.insert(23);
nums.insert(45);
nums.insert(16);
nums.insert(37);
nums.insert(3);
nums.insert(99);
nums.insert(22);
nums.removeNode
document.writeln("Inorder traversal: ");
nums.inOrder(nums.root);
document.writeln("</br>")
document.writeln("Preorder traversal: ");
nums.preOrder(nums.root);
document.writeln("</br>")
document.writeln("Postorder traversal: ");
nums.Postorder(nums.root);
document.writeln("</br>")
var min = nums.getMin();
document.writeln("The minimun value: " + min);
document.writeln("</br>")
var max = nums.getMax();
document.writeln("The maximun value: " + max);
document.writeln("</br>")
var num = 1;
var found = nums.find(num);
if(found != null){
document.writeln(num + " in tree")
} else {
document.writeln(num + " not found in tree")
}
</script>