美文网首页前端编程
二叉搜索树的构建

二叉搜索树的构建

作者: vinson_sheep | 来源:发表于2017-04-07 20:11 被阅读0次

文章来源https://www.nczonline.net/blog/2009/06/09/computer-science-in-javascript-binary-search-tree-part-1/

基本界面

function BinarySearchTree() { this._root = null; } BinarySearchTree.prototype = //restore constructor constructor: BinarySearchTree, add: function (value){ }, contains: function(value){ }, remove: function(value){ }, size: function(){ }, toArray: function(){ }, toString: function(){ } };//这里size,toArray,toString是文章作者添加的新函数

contians

BinarySearchTree.prototype = { //more code contains: function(value){ var found = false, current = this._root //make sure there's a node to search while(!found && current){ //if the value is less than the current node's, go left if (value < current.value){ current = current.left; //if the value is greater than the current node's, go right } else if (value > current.value){ current = current.right; //values are equal, found it! } else { found = true; } } //only proceed if the node was found return found; }, //more code };

add

`BinarySearchTree.prototype = {
//more code
add: function(value){
//create a new item object, place data in
var node = {
value: value,

            left: null,
            right: null
        },
        //used to traverse the structure
        current;
    //special case: no items in the tree yet
    if (this._root === null){
        this._root = node;
    } else {
        current = this._root;
        while(true){
            //if the new value is less than this node's value, go left
            if (value < current.value){
                //if there's no left, then the new node belongs there
                if (current.left === null){
                    current.left = node;
                    break;
                } else {
                    current = current.left;
                }
            //if the new value is greater than this node's value, go right
            } else if (value > current.value){
                //if there's no right, then the new node belongs there
                if (current.right === null){
                    current.right = node;
                    break;
                } else {
                    current = current.right;
                }       
            //if the new value is equal to the current one, just ignore
            } else {
                break;
            }
        }
    }
},
//more code

};`

traverse遍历递归

BinarySearchTree.prototype = { //more code traverse: function(process){ //helper function function inOrder(node){ if (node){ //traverse the left subtree if (node.left !== null){ inOrder(node.left); } //call the process method on this node process.call(this, node); //traverse the right subtree if (node.right !== null){ inOrder(node.right); } } } //start with the root inOrder(this._root); }, //more code };

size/toArray/toString

BinarySearchTree.prototype = { //more code size: function(){ var length = 0; this.traverse(function(node){ length++; }); return length; }, toArray: function(){ var result = []; this.traverse(function(node){ result.push(node.value); }); return result; }, toString: function(){ return this.toArray().toString(); }, //more code };

相关文章

  • 数据结构与算法之二叉搜索树(八)

    目录 二叉搜索树概念二叉搜索树的接口设计,包括增,删,改,查平衡二叉搜索树 一 二叉搜索树 二叉搜索树是二叉树的一...

  • LeetCode 96——不同的二叉搜索树

    1. 题目 2. 解答 以 构建二叉搜索树,其中,任意数字都可以作为根节点来构建二叉搜索树。当我们将某一个数字作...

  • LeetCode 95——不同的二叉搜索树 II

    1. 题目 2. 解答 以 构建二叉搜索树,其中,任意数字都可以作为根节点来构建二叉搜索树。当我们将某一个数字作...

  • Algorithm小白入门 -- 二叉搜索树

    二叉搜索树二叉搜索树 BSTBST 的基本操作计算合法的 BST 1. 二叉搜索树 BST 二叉搜索树(Binar...

  • 二叉搜索树

    二叉搜索树 图解二叉树搜索算法图解:二叉搜索树算法二叉查找树(Binary Search Tree),(又:二叉搜...

  • 23-红黑树

    1.二叉搜索树(BST)继承二叉树(BinaryTree) 2.平衡二叉搜索树(BBST)继承二叉搜索树(BST)...

  • 二叉搜索树(Binary Search Tree)

    1. 定义 二叉搜索树(BST)又叫二叉查找树,二叉排序树。二叉搜索树就是一棵二叉树,但是它又具有搜索树的特征: ...

  • 二叉树基础

    二叉树的分类 完全二叉树与满二叉树 二叉搜索树BST 平衡二叉搜索树BBST因为二叉搜索树有可能退化为链表,降低查...

  • Swift 验证二叉搜索树- LeetCode

    题目: 验证二叉搜索树 验证二叉搜索树给定一个二叉树,判断其是否是一个有效的二叉搜索树。 假设一个二叉搜索树具有...

  • 数据结构 经典算法复习

    二叉搜索树, 平衡二叉树(AVL) 红黑树 B树(平衡多路搜索树) B+树(在B树上改造) 二叉搜索树...

网友评论

    本文标题:二叉搜索树的构建

    本文链接:https://www.haomeiwen.com/subject/wlqoottx.html