博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二叉查找树(查找最大值、最小值、给定值、删除给定值的实现)
阅读量:5362 次
发布时间:2019-06-15

本文共 3962 字,大约阅读时间需要 13 分钟。

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.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 (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;                    }                }            }        }    }    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;    }    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();    document.write("The minimum value of the BST is: " + min);    document.write("
"); var max = nums.getMax(); document.write("The maximum value of the BST is: " + max); document.write("
"); 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; } var s = 99; var found = nums.find(s); if (found != null) { document.write("Found " + s + " in the BST."); } else { document.write(s + " was not found in the BST."); } document.write("
"); function getSmallest(node) {
//查找最小节点 while (node.left != null) { node = node.left; } return node; } function remove(data) { root = removeNode(this.root, data); } 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; } 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; } } nums.remove(99); function inOrder(node) { if (node != null) { inOrder(node.left); document.write(node.show() + " "); inOrder(node.right); } } document.write("Inorder traversal: " + "
"); inOrder(nums.root); /*上述程序运行结果如下: The minimum value of the BST is: 3 The maximum value of the BST is: 99 Found 99 in the BST. Inorder traversal: 3 16 22 23 37 45 */

 

转载于:https://www.cnblogs.com/feile/p/5391767.html

你可能感兴趣的文章
C# 语句 分支语句 switch----case----.
查看>>
lseek函数
查看>>
反射获取 obj类 的属性 与对应值
查看>>
表单中的readonly与disable的区别(zhuan)
查看>>
win10下安装配置mysql-8.0.13--实战可用
查看>>
周记2018.8.27~9.2
查看>>
MySQL中 1305-FUNCTION liangshanhero2.getdate does not exit 问题解决
查看>>
Ctrl+Alt+Down/Up 按键冲突
查看>>
python序列化和json
查看>>
mongodb
查看>>
网格与无网格
查看>>
2018年3月份
查看>>
SSH-struts2的异常处理
查看>>
《30天自制操作系统》学习笔记--第14天
查看>>
LGPL协议的理解
查看>>
1、Python基础
查看>>
Unity The Tag Attribute Matching Rule
查看>>
试着理解下kvm
查看>>
WebService学习总结(二)--使用JDK开发WebService
查看>>
Tizen参考手机RD-210和RD-PQ
查看>>