Js二叉树(数据结构)的前序遍历、中序遍历、后序遍历、移除节点

 

树节点属性  Node

  •    data:节点值
  •    parent :指向节点的父节点
  •    left:指向节点的左节点
  •    right:指向节点的右节点

在移除节点的时候,要考虑到这几种情况:

1:移除忽地节点不存在

2:移除的是根节点

3:移除的不是根节点

      3.1:这个节点比根节点大就要去根结点的右节点找

             3.1.1节点的右节点存不存

      3.2:这个节点比根节点小就要去根结点的左节点找

               3.2.1节点的右节点存不存

最后就是将删除的节点的子节点的最小节点和它的左节点连接

    这是我个人对这个节点删除的解决方式,如果各位有什么更好的方法,请指教!!!!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
<script>
    /*this.insert=insert//插入节点(insert node)
    this.find=find//查找元素(find node in Binary tree)
    this.getMin=getMin//查找最小值(get the min node in Binary tree)
     this.getMax=getMax//查找最大值(get the max node in Binary tree)
     this.remove =remove //移除节点(Remove node from binary tree)
     this.InSort =InSort //中序遍历(In-order traversal)
     this.PreSort =PreSort //前序遍历(Pre-order traversal)
     this.PostSort=PostSort  //后序遍历(post-order traversal)
                      
     */
    function Node(element,left,right,parent){
        this.element=element;
        this.left=left;
        this.right=right;
        this.parent=parent;
        this.show=function () {
            return this.element;
        }
    }


    function BST() {
        this.root=null;

        //插入节点(insert node)
        this.insert=function (element) {
            var node=new Node(element,null,null);
            if(this.root==null){
                this.root=node;
            }else{
                var buffer=this.root;
                var parent;
                while(true){
                    parent=buffer;
                    if(element>buffer.element){
                        buffer=buffer.right;
                        if(buffer==null){
                            node.parent=parent;
                            parent.right=node;
                            break;
                        }

                    }else {
                        buffer=buffer.left;
                        if(buffer==null){
                            node.parent=parent;
                            parent.left=node;
                            break;
                        }
                    }
                }
            }
        }

        //查找元素(find node in Binary tree)
        this.find=function (element) {
            var node = this.root;
            while(true){
                if(node==null||node.element==element){
                    return node;
                }else if(node.element<element){
                    node=node.right;
                }else if(node.element>element){
                    node=node.left;
                }
            }

        }

        //查找最小值(get the min node in Binary tree)
        this.getMin=function (element) {
            var node=this.find(element);
            while(node.left!=null){
                node=node.left;
            }
            return node;
        }


        //查找最大值(get the max node in Binary tree)
        this.getMax=function (element) {
            var node=this.find(element);
            while(node.right!=null){
                node=node.right;
            }
            return node;
        }

        //移除节点(Remove node from binary tree)
        this.remove=function (element) {
            var node=this.find(element);
            //要移除的节点为空(The node to be removed is null)
            if(node==null)
                return;
            //移除的为根节点(Removed as root node)
            if(node.parent==null){
                //节点的右节点不为空(The right node of the node is not null)
                if(node.right!=null){
                    this.root=node.right;
                    this.root.parent=null;
                    var minNode=this.find(node.right.element);
                    minNode.left=node.left;
                }else{//节点的右节点为空(The right node of the node is  null)
                    this.root=node.left;
                    this.root.parent=null;
                }
            }else{//移除的不为根节点(Removed is not the root node)
                var parent=node.parent;
                if(parent.element<node.element){
                    if(node.right!=null){
                        var minNode=this.geMin(node.right.element);
                        minNode.left=node.left;
                        node.left.parent=minNode;
                        parent.right=node.right;
                        node.right.parent=parent;
                    }else {
                        parent.right=node.left;
                        if(node.left!=null)
                            node.left.parent=parent;
                    }
                }else{
                    if(node.right!=null){
                        var minNode=this.geMin(node.right.element);
                        minNode.left=node.left;
                        node.left.parent=minNode;
                        parent.left=node.right;
                        node.right.parent=parent;
                    }else {
                        parent.left=node.left;
                        if(node.left!=null)
                            node.left.parent=parent;
                    }
                }
            }


        }


        //中序遍历(In-order traversal)
        this.InSort=function (node) {
            if(node!=null){
                this.insort(node.left);
                console.log(node.show())
                this.insort(node.right);
            }
        }


        //前序遍历(Pre-order traversal)
        this.PreSort=function (node) {
            if(node!=null){
                console.log(node.show())
                this.insort(node.left);
                this.insort(node.right);
            }
        }


        //后序遍历(post-order traversal)
        this.PostSort=function (node) {
            if(node!=null){
                this.insort(node.left);
                this.insort(node.right);
                console.log(node.show())
            }
        }
    }
    var bst=new BST();
    bst.insert(23);
    bst.insert(45);
    bst.insert(16);
    bst.insert(37);
    bst.insert(3);
    bst.insert(99);
    bst.insert(22);
    //中序遍历
   // bst.InSort(bst.root)
    //前序遍历
    //bst.PreSort(bst.root)
    //后序遍历
    bst.PostSort(bst.root);
</script>
</html>

数据结构,二叉树,前序遍历,后序遍历,中序遍历,移除节点

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值