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
15
votes
2 answers

Difference between the time complexity required to build Binary search tree and AVL tree?

While I was learning Binary search tree(Balanced and unbalanced), I come up with questions which I need to resolve: If I construct a Binary search tree(Not necessary to be balanced) , using n elements then what is the total time complexity for tree…
15
votes
3 answers

Why red-black tree based implementation for java TreeMap?

The third paragraph of wikipedia's article on AVL trees says: "Because AVL trees are more rigidly balanced, they are faster than red-black trees for lookup-intensive applications." So, shouldn't TreeMap be implemented using AVL trees instead of…
Nikunj Banka
  • 11,117
  • 16
  • 74
  • 112
14
votes
2 answers

C++ : Running time of next() and prev() in a multiset iterator?

What is the time complexity of applying the next() and prev() function on an multiset::iterator type object where the corresponding multiset contains the N elements? I understand that in STL, a multiset is implemented as a balanced binary…
Banach Tarski
  • 1,821
  • 17
  • 36
14
votes
2 answers

std::map and binary search tree

I have read that std map is implemented using binary search tree data structure. BST is a sequential data structure (like elements in an array) which stores elements in a BST node and maintains elements in their order. For e.g. if element is less…
user982740
  • 193
  • 2
  • 3
  • 9
13
votes
5 answers

In Big-O notation for tree structures: Why do some sources refer to O(logN) and some to O(h)?

In researching complexity for any algorithm that traverses a binary search tree, I see two different ways to express the same thing: Version #1: The traversal algorithm at worst case compares once per height of the tree; therefore complexity is…
Stephen Gross
  • 5,274
  • 12
  • 41
  • 59
13
votes
3 answers

Create a balanced binary search tree from a stream of integers

I have just finished a job interview and I was struggling with this question, which seems to me as a very hard question for giving on a 15 minutes interview. The question was: Write a function, which given a stream of integers (unordered), builds a…
Dave
  • 131
  • 1
  • 3
13
votes
1 answer

What is a zip tree, and how does it work?

I've heard of a new balanced BST data structure called a zip tree. What is the zip tree? How does it work?
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
13
votes
4 answers

What are the advantages of storing all elements in the leaf nodes?

I'm reading Advanced Data Structures by Peter Brass. In the beginning of the chapter on search trees, he stated that there is two models of search trees - one where nodes contain the actual object (the value if the tree is used as a dictionary), and…
fokenrute
  • 739
  • 6
  • 17
13
votes
7 answers

For a given binary tree find maximum binary search sub-tree

For a given binary tree, find the largest subtree which is also binary search tree? Example: Input: 10 / \ 50 150 / \ / \ 25 75 200 20 …
gtikok
  • 1,119
  • 8
  • 18
13
votes
4 answers

Finding the minimum and maximum height in a AVL tree, given a number of nodes?

Is there a formula to calculate what the maximum and minimum height for an AVL tree, given a certain number of nodes? For example: Textbook question: What is the maximum/minimum height for an AVL tree of 3 nodes, 5 nodes, and 7 nodes? Textbook…
darkserith
  • 133
  • 1
  • 1
  • 5
13
votes
8 answers

To print the boundary of Binary Tree

I have been asked in an interviews to print the boundary of the Binary Tree. For example. 1 / \ 2 3 / \ / \ 4 5 6 7 / \ \ 8 9 10 Answer will be : 1, 2, 4, 8, 9, 10, 7, 3 I have given the following…
13
votes
11 answers

How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space

How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space.
bit-question
  • 3,733
  • 17
  • 50
  • 63
13
votes
2 answers

Dynamic Programming: Why Knuth's improvement to Optimal Binary Search Tree O(n^2)?

This is Exercise 15.5-4 of Introduction to Algorithms, 3rd edition, which is about Knuth's improvement to the DP approach to Optimal Binary Search Tree. The DP algorithm of Optimal Binary Search Tree is: OPTIMAL_BST(p, q, n) let e[1..n+1, 0..n],…
Guibao Wang
  • 405
  • 4
  • 15
13
votes
5 answers

How to construct BST given post-order traversal

I know there are ways to construct a tree from pre-order traversal (as an array). The more common question is to construct it, given the inorder and pre-order traversals. In this case, although the inorder traversal is redundant, it definitely makes…
SexyBeast
  • 7,913
  • 28
  • 108
  • 196
13
votes
4 answers

Binary Tree in Objective-C

I am learning algorithms and data structures and to train I am trying to design and implement a binary tree using objective-c. So far I have the following Classes: main - for testing Node - node of tree BinaryTree - for all methods related to the…
Janusz Chudzynski
  • 2,700
  • 3
  • 33
  • 46