javascript数据结构与算法---二叉树(删除节点)

function Node(data,left,right) {
this.data = data;
this.left = left;
this.right = right;
this.show = show;
} function show() {
return this.data;
} function BST() {
this.root = null;
this.insert = insert;
this.inOrder = inOrder;
this.getMin = getMin;
this.getMax = getMax;
this.find = find;
this.remove = remove;
} 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;
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;
}
}
}
}
}
// 中序遍历
function inOrder(node) {
if(!(node == null)) {
inOrder(node.left);
console.log(node.show());
inOrder(node.right);
}
} // 先序遍历
function preOrder(node) {
if(!(node == null)) {
console.log(node.show());
preOrder(node.left);
preOrder(node.right);
}
} // 后序遍历
function postOrder(node) {
if(!(node == null)) {
postOrder(node.left);
postOrder(node.right);
console.log("后序遍历"+node.show());
}
} // 二叉树查找最小值
function getMin(){
var current = this.root;
while(!(current.left == null)) {
current = current.left;
}
return current.data;
} // 二叉树上查找最大值
function getMax() {
var current = this.root;
while(!(current.right == null)) {
current = current.right;
}
return current.data;
} // 查找给定值
function find(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;
} function remove(data) {
root = removeNode(this.root,data);
}
function getSmallest(node) {
if (node.left == null) {
return node;
}
else {
return getSmallest(node.left);
}
}
function removeNode(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;
}
// 有2个子节点的节点
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 BST();
nums.insert(23);
nums.insert(45);
nums.insert(16);
nums.insert(37);
nums.insert(3);
nums.insert(99);
nums.insert(22);
var min = nums.getMin();
console.log(min);
var max = nums.getMax();
console.log(max);
var value = nums.find("45");
console.log(value);
nums.remove(23);

最新文章

  1. autocomplete的使用
  2. Java线程池的那些事
  3. JAVA生产者消费者的实现
  4. redis实现spring-redis-data的入门实例
  5. Educational Codeforces Round 6 C. Pearls in a Row
  6. 转:memcpy的用法总结
  7. mklink修改Chrome缓存目录
  8. commons-lang 包常用方法
  9. Sping--Id, Name
  10. NHibernate教程(18)--对象状态
  11. 如何在README.md文件中添加图片
  12. 【福大软工】 W班级总成绩排名2
  13. 洛谷P1209-最大公约数与最小公倍数问题
  14. JavaScript判断对象是否是NULL
  15. poj 2955 Brackets (区间dp 括号匹配)
  16. zlib简单使用说明(转)
  17. Linux/Unix命令行安装weblogic软件
  18. 通过url获取参数信息
  19. 如何使用tapd?
  20. Java实现文件上传到服务器(FTP方式)

热门文章

  1. MFC连接MySQL
  2. js setInterval详解
  3. JavaScript常用定义和方法
  4. Vue上传文件:ElementUI中的upload实现
  5. VS2013利用ajax访问不了json文件——VS2013配置webconfig识别json文件
  6. android 首字母迷糊查询 拼音查询 中英文混排查询
  7. 【并查集的另一个思考方向】POJ1456
  8. 使用PinYin4j.jar将汉字转换为拼音
  9. 两数据库Dblink数据抽取blob
  10. 修改vsftpd的默认根目录