【问题标题】:Is this the efficient BST Insertion?这是高效的 BST 插入吗?
【发布时间】:2020-09-22 05:04:17
【问题描述】:

我只是根据逻辑自己实现了二叉搜索树插入方法。 那么,任何人都可以在插入和搜索时验证代码是否正常工作(使用您自己的搜索方法,如 inorder、preorder、postorder)?

并且还要求代码的时间复杂度。

public void insert(int data) {
    Node node = new Node(data);
    if (root == null) {
        root = node;
        size++;
    } else {
        Node n = root;
        if (data > n.data) {
            while (n.right != null) {
                n = n.right;
            }
            if (data < n.data) {
                while (n.left != null) {
                    n = n.left;
                }
                if (data != n.data) {
                    n.left = node;
                    size++;
                }
            } else {
                if (data != n.data) {
                    n.right = node;
                    size++;
                }
            }
        } else if (data < n.data) {
            while (n.left != null) {
                n = n.left;
            }
            if (data > n.data) {
                while (n.right != null) {
                    n = n.right;
                }
                if (data != n.data) {
                    n.right = node;
                    size++;
                }
            } else {
                if (data != n.data) {
                    n.left = node;
                    size++;
                }
            }
        }

    }
}

编辑:- 我在插入这些数字时发现了一个问题:-

    bst.insert(10);
    bst.insert(11);
    bst.insert(90);
    bst.insert(13);
    bst.insert(12);
    bst.insert(70);
    bst.insert(80);

它会这样打印(按顺序):- 10 11 80 70 12 13 90

【问题讨论】:

    标签: java data-structures binary-search-tree


    【解决方案1】:

    您的代码似乎存在一些问题:

    if (data > n.data) {
        while (n.right != null) {
                    n = n.right;
                    .
                    .
                    }
               }
    

    表示给定一棵树,如:

      10
     /  \
    8    15
        / \
       12  23
             \
              26
             /
           20
    

    并要求插入例如 21,给定的算法将尝试将其插入 26 的左子节点。但是该节点已被占用,因此您的算法有缺陷。 我建议您从每个节点递归调用插入,例如:

    // in class tree
    public void insert(int data) {
        if (root == null) {
            root = new Node(data);
        } 
        else {
            root.insert(data);
        }
        size++;
    }
    //in class Node
    public void insert(int data) {
        if(data>=this.data){
             if(right==null) right=new Node(data)
             else right.insert(data);
        }
        else{
             if(left==null) left=new Node(data)
             else left.insert(data);
        }
    }
    

    由于每个节点都负责在插入之前检查其子节点是否为空,因此此问题不会重复。

    在复杂性方面: 你可以得到一棵树像

    1
     \
      2
       \
        3
         \
          ...
            \
             n
    

    如果插入大于 n 的值会导致通过 n 个节点,因此时间复杂度为 O(n)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多