美文网首页
二叉排序树中插入节点

二叉排序树中插入节点

作者: mrjunwang | 来源:发表于2018-07-19 15:05 被阅读0次

1已知有一颗二叉排序树,向树里面插入节点,如果该节点已存在(节点值相等),将节点中的count字段加一;如果不存在,将节点插入树中,并将节点的count值置为1。自行设计数据结构,插入算法并且分析算法的复杂度

public class TreeNode<T extends Comparable<T>> {
    private T value;
    private TreeNode<T> left;
    private TreeNode<T> right;
    private int count=0;
    public int compare(TreeNode<T> n){
        if(value.compareTo(n.getValue())>0){
            return 1;
        }
        else if(value.compareTo(n.getValue())<0){
            return -1;
        }
        else{
            return 0;
        }
    }
    public T getValue() {
        return value;
    }
    public void setValue(T value) {
        this.value = value;
    }
    public TreeNode<T> getLeft() {
        return left;
    }
    public void setLeft(TreeNode<T> left) {
        this.left = left;
    }
    public TreeNode<T> getRight() {
        return right;
    }
    public void setRight(TreeNode<T> right) {
        this.right = right;
    }
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
    public TreeNode<T> insert(TreeNode<T>  root,TreeNode<T>  node){
        node.count=1;
        if(root==null){
            return node;
        }
        
        if(root.compare(node)==0){
                root.count=root.count+1;
                return root;
            }
        else if(root.compare(node)>0){
                if(root.left==null){
                    root.left=node;
                }
                else{
                    insert(root.left,node);
                }
            return root;    
            }
        else {
                if(root.right==null){
                    root.right=node;
                }
                else{
                    insert(root.right,node);
                }
                return root;    
            }
    }
    
    public static void main(String args[]){
        TreeNode<Integer> root=new TreeNode();
        TreeNode<Integer> root1=new TreeNode();
        root1.setValue(5);
        TreeNode<Integer> node=new TreeNode();
        node.setValue(2);
        
        root1.setLeft(node);
        TreeNode<Integer> node2=new TreeNode();
        node2.setValue(1);
        node.setLeft(node2);
        TreeNode<Integer> node3=new TreeNode();
        node3.setValue(3);
        TreeNode head=root.insert(root1, node3);
        Queue<TreeNode> que=new LinkedList();
        que.add(head);
        while(!que.isEmpty()){
            TreeNode cur=que.poll();
            System.out.println(cur.value +","+cur.count);
            if(cur.left!=null){
                que.add(cur.left);
            }
            if(cur.right!=null){
                que.add(cur.right);
            }
        }
    }

}

假设树中有N个节点,最多遍历树的高度。时间复杂度O(logN)

相关文章

  • 二叉平衡树(AVL)

    一种二叉排序树,其中每个节点的左子树和右子树的高度差至多等于1基本思想:在构建二叉排序树的过程中,每当插入一个节点...

  • 平衡树(AVL)的插入

    平衡树,遵循了二叉排序树的原则,小的往左插,也可以认为是最优二叉排序树,因为它在插入的过程中通过每个节点的平衡因子...

  • 二叉排序树中插入节点

    1已知有一颗二叉排序树,向树里面插入节点,如果该节点已存在(节点值相等),将节点中的count字段加一;如果不存在...

  • 二叉搜索树的定义

    二叉搜索树(二叉排序树)的定义:根节点比左子树的所有节点都大,比右节点的所有节点的值都小插入有序节点,退化成单支树...

  • 二叉排序树BST

    二叉排序树/二叉查找树/二叉搜索树BST set和map的实现基础查找 插入 不使用引用C中没有引用对父节点的le...

  • 二叉排序树:左节点 < 根节点 < 右节点(节点不相等),左右子树亦是二叉排序树。 红黑树:自平衡二叉查找树。避免...

  • 二叉树排序树(搜索树)的理解

    二叉排序树,也叫搜索树,顾名思义 是一种有顺序的二叉树; 数值的插入 保证插入的数值满足:节点的值大于左子树上的所...

  • 2018-04-08JQ中DOM的操作

    回顾原生DOM操作 *获取节点 *创建节点 *遍历节点 *替换节点 *删除节点 *插入节点 *复制节点 JQ中的...

  • 基本算法

    预热姿势: 什么是二叉查找树 (二叉排序树) 1.红黑树 规则: 插入删除节点时的方法: 2. B-树 (也叫B树...

  • 5.2. 在循环单链表的末尾插入节点

    在循环单链表的末尾插入节点有两种情况。 第一种情况:将节点插入空链表中,第一种情况:将节点插入非空链表中。首先,使...

网友评论

      本文标题:二叉排序树中插入节点

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