简体   繁体   English

为什么要添加不平衡二叉搜索树O(n)?

[英]Why is add in unbalanced Binary Search Tree O(n)?

This is the implementation of add in Binary Search Tree from BST Add 这是BST Add中Binary Search Tree中add的实现

private IntTreeNode add(IntTreeNode root, int value) {
        if (root == null) {
            root = new IntTreeNode(value);
        } else if (value <= root.data) {
            root.left = add(root.left, value);
        } else {
            root.right = add(root.right, value);
        }

        return root;
    }

I understand why this runs in O(log n). 我知道为什么它在O(log n)中运行。 Here's how I analyze it. 这就是我的分析方式。 We have a tree size of n. 我们的树大小为n。 How many cuts of 2, or half cut, will reduce this tree down to a size of 1. So we have the expression n(1/2)^x = 1 where the 1/2 represents each half cut. 多少个2的切口或半切口将把这棵树缩小到1的大小。因此,我们有n(1/2)^ x = 1的表达式,其中1/2表示每个半切口。 Solving this for x, we have log2(x) so the logn comes from search. 为x解决这个问题,我们有log2(x),所以logn来自搜索。
Here is a lecture slide from Heap that discusses runtime for an unbalanced binary search. 这是Heap的演讲幻灯片,讨论了不平衡二进制搜索的运行时。 在此处输入图片说明

My question is even if the binary search tree is unbalanced, wouldn't the same strategy work for analyzing the runtime of add? 我的问题是,即使二进制搜索树不平衡,使用相同的策略来分析添加的运行时间也行不通吗? How many cuts you have to make. 您必须进行多少次切割。 Wouldn't the runtime still be O(log n), not O(n)? 运行时是否仍为O(log n),而不是O(n)? If so, can someone show the math of why it would be O(n)? 如果是这样,有人可以说明为什么它是O(n)的数学方法吗?

With an unbalanced tree: 对于不平衡的树:

1
 \
  2
   \
    3
     \
      4
       \
        5
         \
          ...

Your intuition of cutting the tree in half with each operation no longer applies. 您每次操作将树切成两半的直觉不再适用。 This unbalanced tree is the worst case of an unbalanced binary search tree. 该不平衡树是不平衡二进制搜索树的最坏情况。 To search for 10 at the bottom of the list, you must make 10 operations, one for each element in the tree. 要在列表底部搜索10 ,必须执行10操作,每个操作针对树中的每个元素。 That is why a search operation for an unbalanced binary search tree is O (n) - this unbalanced binary search tree is equivalent to a linked list. 这就是为什么不平衡的二进制搜索树的搜索操作为O (n)的原因-该不平衡的二进制搜索树等效于链接列表。 Each operation doesn't cut off half the tree -- just the one node you've already visited. 每个操作都不会砍掉一半的树-只是您已经访问过的一个节点。

That is why specialized versions of binary search trees, such as red-black trees and AVL trees are important: they maintain trees that are balanced well enough so that all operations - search, insert, delete -- are still O (log n). 这就是为什么二进制搜索树(例如红黑树和AVL树)的特殊版本很重要的原因:它们维护的树要平衡得足够好,以便所有操作(搜索,插入,删除)仍为O (log n)。

The O(n) situation in a BST happens when you have either the minimum or the maximum at the top, effectively turning your BST into a linked list. BST的O(n)情况发生在顶部有最小值或最大值时,实际上将BST变成了链表。 Suppose you added elements as: 1, 2, 3, 4, 5 , generating your BST, which will be a linked list due to every element having only a right child . 假设您添加的元素为: 1, 2, 3, 4, 5生成您的BST,由于每个元素只有一个right child ,所以它将成为一个链表。 Adding 6 would have to go down right on every single node, going through all the elements, hence making the asymptotic complexity of add O(n) 加法6必须在每个节点上向下遍历所有元素,因此使加法O(n)的渐近复杂度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么二叉搜索树趋向于变得不平衡? - Why does binary search tree tend to become unbalanced to the right? 如何创建不平衡的二叉搜索树 - How do I create a unbalanced binary search tree 为什么这个 function 检查二叉树平衡的时间复杂度是 O(n log n)? - Why is the time complexity of this function to check the balance of a Binary Tree O(n log n)? 以 O(log n) 获取二叉树的大小 - Get size of a binary tree in O(log n) 使用常量空间和O(n)运行时编写二进制搜索树的非递归遍历 - Write a non-recursive traversal of a Binary Search Tree using constant space and O(n) run time 你如何以 O(log n) 倍的复杂度计算平衡二叉搜索树的高度? - How do you calculate the height of balanced Binary Search Tree in O(log n) times complexity? 算法 - O(n)中二进制搜索树的每两个节点之间的距离之和? - Algorithm- Sum of distances between every two nodes of a Binary Search Tree in O(n)? 二叉树O(n)的InOrder树遍历的时间复杂度? - Time Complexity of InOrder Tree Traversal of Binary Tree O(n)? 二进制搜索树递归添加 - Binary Search Tree recursive add 为什么这段代码在多次重新计算深度时检查二叉树是否平衡需要时间 O(n log n) ? - Why does this code to check if a binary tree is balanced take time O(n log n) when it recomputes depths multiple times?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM