二叉树的简单实现 JS

3 篇文章 0 订阅
1 篇文章 0 订阅

今天学习了一下 二叉树的实现,在此记录一下

简单的二叉树实现,并且实现升序和降序排序输出

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.inOrderDesc = inOrderDesc;//中序遍历(降序)
    this.preOrder = preOrder;//先序遍历
    this.postOrder = postOrder;//后续遍历
    this.getMin = getMin;//最大值
    this.getMax = getMax;//最小值
    this.find =  find;//查找值
    this.remove = remove;//删除节点
    this.count = count;//获取节点数量
    function insert(data){
        //创建一个新的节点
        var newNode  = new Node(data,null,null);
        //判断是否存在根节点,没有将新节点存入
        if(this.root == null){
            this.root = newNode;
        }else{
            //获取根节点
            var current = this.root;
            var parent;
            while(true){
                //将当前节点保存为父节点
                parent = current;
                //将小的数据放在左节点
                if(data < current.data){
                    //获取当前节点的左节点
                    //判断当前节点下的左节点是否有数据
                    current = current.left;
                    if(current == null){
                        //如果没有数据将新节点存入当前节点下的左节点
                        parent.left = newNode;
                        break;
                    }
                }else{
                    current = current.right;
                    if(current == null){
                        parent.right = newNode;
                        break;
                    }
                }
            }    
        }

    }
    function inOrder(node){
        var data = [];
        _inOrder(node,data);
        return data;
    }
    function inOrderDesc(node){
        var data = [];
        _inOrderDesc(node,data);
        return data;
    }

    function preOrder(node){
        var data = [];
        _preOrder(node,data);
        return data;
    }

    function postOrder(node){
        var data = [];
        _postOrder(node,data);
        return data;
    }

    function _inOrder(node,data){

        if(!(node == null)){
            _inOrder(node.left,data);
            data.push(node.show());
            _inOrder(node.right,data);
        }
    }
    function _inOrderDesc(node,data){
        debugger;
        if(!(node == null)){
            _inOrderDesc(node.right,data);
            data.push(node.show());
            _inOrderDesc(node.left,data);
        }
    }
    function _preOrder(node,data){
        if(!(node == null)){
            data.push(node.show());
            _preOrder(node.left,data);
            _preOrder(node.right,data);
        }
    }

    function _postOrder(node,data){
        if(!(node == null)){
            _postOrder(node.left,data);
            _postOrder(node.right,data);
            data.push(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(data == current.data){
                return current;
            }else if(data < current.data){
                current = current.left;
            }else{
                current = current.right;
            }
        }
        return null;
    }
    function getSmallest(node){
        var current = node;
        while(!(current.left == null)){
            current = current.left;
        }
        return current;
    }
    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){
                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;
        }
    }

    function count(){
        var counts = 0;
        var current = this.root;
        if(current == null){
            return counts;
        }
        return _count(current,counts);
    }
    function _count(node,counts){
        debugger;
        if(!(node == null)){
            counts ++;
            counts = _count(node.left,counts);;
            counts = _count(node.right,counts);
        }
        return counts;
    }
}


二叉树的 JavaScript 实现可以使用对象来表示每个节点,每个节点包含一个值和两个指向左右子节点的指针。以下是一个简单实现: ``` class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } class BinaryTree { constructor() { this.root = null; } insert(value) { const node = new Node(value); if (!this.root) { this.root = node; return; } let current = this.root; while (true) { if (value < current.value) { if (!current.left) { current.left = node; break; } current = current.left; } else { if (!current.right) { current.right = node; break; } current = current.right; } } } search(value) { let current = this.root; while (current) { if (value === current.value) { return current; } else if (value < current.value) { current = current.left; } else { current = current.right; } } return null; } } ``` 你可以使用 `insert` 方法来插入一个值,使用 `search` 方法来查找一个值。例如: ``` const tree = new BinaryTree(); tree.insert(5); tree.insert(3); tree.insert(7); tree.insert(1); tree.insert(4); tree.insert(6); tree.insert(8); console.log(tree.search(4)); // Node { value: 4, left: Node { value: 1, left: null, right: null }, right: Node { value: 5, left: Node { value: 3, left: null, right: [Node] }, right: Node { value: 7, left: Node { value: 6, left: null, right: null }, right: Node { value: 8, left: null, right: null } } } } ``` 这将输出包含值为 4 的节点及其子节点的对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值