Questions tagged [binary-search-tree]

A binary search tree is a data structure which consists of a root node with left and right child nodes. The left node and all of its descendants have smaller values than the root node, while the right node and all of its descendants have larger values than the root node. The children of the root node follow this same pattern. This gives us a tree consisting of ordered elements.

In computer science, a binary search tree (BST), sometimes also called an ordered or sorted binary tree, is a node-based binary tree data structure where each node has a comparable key (and an associated value) and satisfies the restriction that the key in any node is larger than the keys in all nodes in that node's left subtree and smaller than the keys in all nodes in that node's right sub-tree. Each node has no more than two child nodes. Each child must either be a leaf node or the root of another binary search tree. The left sub-tree contains only nodes with keys less than the parent node; the right sub-tree contains only nodes with keys greater than the parent node. BSTs are also dynamic data structures, and the size of a BST is only limited by the amount of free memory in the operating system. The main advantage of binary search trees is that it remains ordered, which provides quicker search times than many other data structures. The common properties of binary search trees are as follows:

  1. The left subtree of a node contains only nodes with keys less than the node's key.

  2. The right subtree of a node contains only nodes with keys greater than the node's key.

  3. The left and right subtree each must also be a binary search tree.

  4. Each node can have up to two successor nodes.

  5. There must be no duplicate nodes.

  6. A unique path exists from the root to every other node.

Generally, the information represented by each node is a record rather than a single data element. However, for sequencing purposes, nodes are compared according to their keys rather than any part of their associated records.

6319 questions
33
votes
4 answers

What is the difference between breadth first searching and level order traversal?

I don't need code, just an explanation. My textbook says level order: each node at level i is processed before any node at level i+1 My understanding of breadth first searching is that you explore nodes nearest the root first, starting from the…
33
votes
7 answers

Finding if a Binary Tree is a Binary Search Tree

Today I had an interview where I was asked to write a program which takes a Binary Tree and returns true if it is also a Binary Search Tree otherwise false. My Approach1: Perform an in-order traversal and store the elements in O(n) time. Now scan…
dharam
  • 7,882
  • 15
  • 65
  • 93
32
votes
5 answers

Why lookup in a Binary Search Tree is O(log(n))?

I can see how, when looking up a value in a BST we leave half the tree everytime we compare a node with the value we are looking for. However I fail to see why the time complexity is O(log(n)). So, my question is: If we have a tree of N elements,…
Hommer Smith
  • 26,772
  • 56
  • 167
  • 296
31
votes
1 answer

How do you know where to perform rotations in an AVL tree?

So I'm self teaching AVL trees and I understand the basic idea behind it, but I just want to make sure my intuition of actually implementing it is valid: I'll examine it with the left rotation- So, the following situation is simple: 8 /…
Skorpius
  • 2,135
  • 2
  • 26
  • 31
29
votes
1 answer

Inserting an equal value element

I am currently studying binary search trees, and I was wondering what do you do if you try to insert an element that is the same value as the root? Where does it go?
Programatt
  • 786
  • 4
  • 11
  • 23
29
votes
19 answers

In Order Successor in Binary Search Tree

Given a node in a BST, how does one find the next higher key?
shreyasva
  • 13,126
  • 25
  • 78
  • 101
28
votes
5 answers

Is there any technical reason why std::lower_bound is not specialized for red-black tree iterators?

I have always assumed that std::lower_bound() runs in logarithmic time if I pass a pair of red-black tree iterators (set::iterator or map::iterator) to it. I had to burn myself twice to notice that std::lower_bound() runs in O(n) time in this case,…
Ali
  • 56,466
  • 29
  • 168
  • 265
27
votes
6 answers

How to merge two BST's efficiently?

How to merge two binary search trees maintaining the property of BST? If we decide to take each element from a tree and insert it into the other, the complexity of this method would be O(n1 * log(n2)), where n1 is the number of nodes of the tree…
gowth08
  • 297
  • 1
  • 3
  • 7
26
votes
2 answers

Advantage of B+ trees over BSTs?

I'm learning about B+ trees in a class about databases and I was wondering what concrete advantages B+ trees would give over Binary Search Trees? It seems like they both have O(logN) average complexity for most operations of note but B+ trees also…
riggspc
  • 612
  • 3
  • 7
  • 15
26
votes
5 answers

Proof that the height of a balanced binary-search tree is log(n)

The binary-search algorithm takes log(n) time, because of the fact that the height of the tree (with n nodes) would be log(n). How would you prove this?
Igor L.
  • 3,159
  • 7
  • 40
  • 61
22
votes
11 answers

Pre-order to post-order traversal

If the pre-order traversal of a binary search tree is 6, 2, 1, 4, 3, 7, 10, 9, 11, how to get the post-order traversal?
Bobj-C
  • 5,276
  • 9
  • 47
  • 83
22
votes
2 answers

How many ways can you insert a series of values into a BST to form a specific tree?

This earlier question asked how many ways there were to insert the values 1 - 7 into a binary search tree that would result in the following tree: 4 / \ 2 6 / \ / \ 1 3 5 7 (The answer is 80, by the way). Suppose…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
22
votes
3 answers

Why do we need a separate datastructure like B-Tree for database and file system?

I am reading up on B Trees and looks like they achieve the dynamic set operations in O(lg n) time . The Red Black Tree( TreeMap in java ) also achieves the same operation in asymptotically the same time frame . So I would like to know what makes B…
Geek
  • 26,489
  • 43
  • 149
  • 227
21
votes
9 answers

Given a BST and its root, print all sequences of nodes which give rise to the same bst

Given a BST, find all sequences of nodes starting from root that will essentially give the same binary search tree. Given a bst, say 3 / \ 1 5 the answer should be 3,1,5 and 3,5,1. another example 5 / \ 4 7 / /…
user2892884
  • 329
  • 1
  • 2
  • 5
21
votes
2 answers

How many traversals need to be known to construct a BST

I am very confused by a number of articles at different sites regarding constructing a Binary Search Tree from any one traversal (pre,post or in-order), or a combination of any two of them. For example, at this page, it says that given the pre,post…
SexyBeast
  • 7,913
  • 28
  • 108
  • 196