1

Can there exist a balanced binary tree that is not a balanced binary search tree? If so, what would the time complexity be to search a node in such a tree.

My understanding is this:

  1. Binary Tree: any node has two maximum leaf nodes. Searching in a binary tree, using DFS or BFS is O|V+E|
  2. Binary Search Tree: A BST is a tree of ordered nodes. Searching in a binary search tree, using DFS is O|log n|
  3. Balanced Tree(assuming height-balanced): Maximum number of levels below the root is kept to the minimum. Does being balanced have any effect to the time complexity of search?

So, essentially, can I create a binary tree that is height-balanced but not ordered. Would the search time of this tree be O|V+E| or will it be better?

aarti
  • 2,815
  • 1
  • 23
  • 31
  • Maybe http://cs.stackexchange.com/ would be a better place for your question – Rod Jul 28 '15 at 17:30
  • It's not really DFS (or BFS) when searching an ordered tree. The "F" stands for "First", implying there's at least one other option, but there's no choices to be made with ordered trees. You know exactly the path to take. – ikegami Jul 28 '15 at 17:44
  • @ikegami hmm, so you are saying you don't have to pick an algorithm to search, queue based or stack based(the first part), since you are traversing based on the order. – aarti Jul 28 '15 at 17:49
  • 2
    Right. For an ordered tree (balanced or not), you'd use: `n = root; while (n) { if (n.value == target) return n; n = n.value < target ? n.left : n.right; } return NULL;`. No need for a stack (DFS) or queue (BFS) for backtracking since there's no backtracking. – ikegami Jul 28 '15 at 17:52
  • 1
    Balancing the tree limits the maximum depth of the tree, which limits the maximum number of iterations of the while loop. – ikegami Jul 28 '15 at 17:57
  • Got it, Balancing reduces operation in the while loop, but since you are backtracking, you still have O(N) operations, correct? – aarti Jul 28 '15 at 17:59
  • 2
    No, there's no backtracking with ordered trees. It's O(log N) for balanced and O(N) for unbalanced (since you could effectively have a linked list). With unordered trees, you have to visit every node, so it's O(N). The depth of the tree has no effect on the number of nodes to visit, so balancing doesn't help. – ikegami Jul 28 '15 at 18:16
  • According to this question http://stackoverflow.com/questions/9134839/in-big-o-notation-for-tree-structures-why-do-some-sources-refer-to-ologn-and O(h) = O(n) if the BST is unbalanced, just like you mentioned. – aarti Jul 28 '15 at 18:24

1 Answers1

5

Searching an unordered binary tree requires visiting every node, so it's O(N) whether it's balanced

          50
       __/  \__
      /        \
    25          26
   /  \        /  \
 49    46    48    47

or not

          50
       __/  \__
      /        \
    25          26
   /  \
 49    46
      /  \
     5    6

There's really no point in balancing an unordered tree.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • 3
    Note that O(|V|+|E|) = O(|V|) because |E| = |V|-1 in the case of a tree – Niklas B. Jul 28 '15 at 17:32
  • (… if talking "internal edges" (non-nil references/pointers) or _binary_ trees (trees of limited degree)) – greybeard Jul 28 '15 at 17:51
  • @NiklasB. |E|=|V|-1 because a tree is a connected graph, then O(2|V|-1) approximates to O(V) correct. http://math.stackexchange.com/questions/457042/prove-that-a-connected-graph-with-n-vertices-has-at-least-n-1-edges – aarti Jul 28 '15 at 18:39