forked from zhoutk/js-data-struct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSTree.js
146 lines (135 loc) · 4.14 KB
/
BSTree.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
/***************************************************************************
> File Name : BSTree.js
> Author : zhoutk
> Mail : zhoutk@189.cn
> Create Time : 2015-09-29 18:38
***************************************************************************/
(function(){
"use strict";
var Node = require("./lib/BSTNode");
function BSTree(){
this.root = null;
}
BSTree.prototype.remove = function(data){
if(this.root == null)
return false;
var currNode = this.root;
var parent = null;
//注意边界值,如果被删除的是根结点,循环是不进入的,parent为null
while(currNode != null && currNode.data != data) {
parent = currNode;
if(data < currNode.data){
currNode = currNode.left;
}else{
currNode = currNode.right;
}
}
if(currNode == null){
return false;
}
if(currNode.left == null || currNode.right == null){
if(parent == null){ //处理边界值,但左右子树同时存在时,不会出问题
this.root = currNode.left == null ? currNode.right : currNode.left;
}
else if(parent.left == currNode){
parent.left = currNode.left == null ? currNode.right : currNode.left;
}
else{
parent.right = currNode.left == null ? currNode.right : currNode.left;
}
}else{
var mid = currNode.right;
parent = currNode;
while(mid.left != null){
parent = mid;
mid = mid.left;
}
currNode.data = mid.data;
if(parent.left == mid){
parent.left = mid.right;
}
else{
parent.right = mid.right;
}
}
return true;
};
BSTree.prototype.find = function(data){
var currNode = this.root;
while(currNode != null){
if(currNode.data == data){
return currNode;
}
else if(data < currNode.data){
currNode = currNode.left;
}
else{
currNode = currNode.right;
}
}
return null;
};
BSTree.prototype.getMin = function(){
var currNode = this.root;
while(currNode.left != null){
currNode = currNode.left;
}
return currNode.data;
};
BSTree.prototype.getMax = function(){
var currNode = this.root;
while(currNode.right != null){
currNode = currNode.right;
}
return currNode.data;
};
BSTree.prototype.postOrder = function(node){
if(node != null){
this.postOrder(node.left);
this.postOrder(node.right);
console.log(node.data);
}
};
BSTree.prototype.preOrder = function(node){
if(node != null){
console.log(node.data);
this.preOrder(node.left);
this.preOrder(node.right);
}
};
BSTree.prototype.inOrder = function(node){
if(node != null){
this.inOrder(node.left);
console.log(node.data);
this.inOrder(node.right);
}
};
BSTree.prototype.insert = function(data){
var node = new Node(data, null, null);
if(this.root == null){
this.root = node;
}
else{
var currNode = this.root;
var parent;
while(true){
parent = currNode;
if(data < currNode.data){
currNode = currNode.left;
if(currNode == null){
parent.left = node;
break;
}
}
else{
currNode = currNode.right;
if(currNode == null){
parent.right = node;
break;
}
}
}
}
};
module.exports = BSTree;
})();