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
9
votes
3 answers

For a complete binary tree with n nodes, how many nodes are leaf nodes?

One of the answers in our powerpoint says it is n/2 leaves, but I am seeing another answer which says (n+1)/2. I was wondering which one is correct if any, and why?
Amit Jain
  • 337
  • 2
  • 3
  • 7
9
votes
2 answers

Create a complete binary search tree from list

I am trying to make an algorithm that creates a complete binary search tree given a list of values. Complete, in that all levels are full except for maybe the last level, which needs to have all elements shifted as far left as possible. I've…
computmaxer
  • 1,677
  • 17
  • 28
9
votes
4 answers

BST with duplicates

I know that, BST does not allow duplicates. For example, if I have a word "RABSAB". The Binary search tree for the above string is: R /\ A S \ B What if we wanted to include the duplicates in the tree. How the tree gonna…
user
  • 291
  • 1
  • 3
  • 16
9
votes
5 answers

How to save the memory when storing color information in Red-Black Trees?

I've bumped into this question at one of Coursera algorithms course and realized that I have no idea how to do that. But still, I have some thoughts about it. The first thing that comes into my mind was using optimized bit set (like Java's BitSet)…
9
votes
5 answers

Why storing data only in the leaf nodes of a balanced binary-search tree?

I have bought a nice little book about computational geometry. While reading it here and there, I often stumbled over the use of this special kind of binary search tree. These trees are balanced and should store the data only in the leaf nodes,…
philipp
  • 15,947
  • 15
  • 61
  • 106
9
votes
1 answer

Converting a heap to a BST in O(n) time?

I think that I know the answer and the minimum complexity is O(nlogn). But is there any way that I can make an binary search tree from a heap in O(n) complexity?
9
votes
4 answers

Equality of two binary search trees constructed from unordered arrays

Given two unsorted arrays of size N each, we are to determine if the Binary Search Tree constructed from them will be equal or not. So, the elements of the array are picked and inserted into a basic (no balancing, no red-black, nothing) binary…
Kanishk
  • 167
  • 7
8
votes
1 answer

Are there real-world reasons to employ a Binary Search Tree over a Binary Search of semi-contiguous list?

I'm watching university lectures on algorithms and it seems so many of them rely almost entirely binary search trees of some particular sort for querying/database/search tasks. I don't understand this obsession with Binary Search Trees. It seems…
8
votes
3 answers

Can a std::map be efficiently split into two std::maps at an iterator?

Let's say I have a std::map myMap containing the data 1. Red 2. Blue 3. Green 5. Fuchsia 6. Mauve 9. Gamboge 10. Vermillion and also an std::map::iterator it pointing at the element 5. Fuchsia I would like to do…
Daniel McLaury
  • 4,047
  • 1
  • 15
  • 37
8
votes
1 answer

Binary Search Tree Traversal - PreOrder

I m trying to implement Tree Traversal PreOrder using yield return which returns an IEnumerable private IEnumerable Preorder(Node node) { while(node != null) { yield return node.Data; yield return node.LeftChild.Data; …
DarthVader
  • 52,984
  • 76
  • 209
  • 300
8
votes
10 answers

Finding the common ancestor in a binary tree

This question was asked to me in an interview: I have a binary tree and I have to find the common ancestor (parent) given two random nodes of that tree. I am also given a pointer to the root node. My answer is: Traverse the tree separately for…
Vijay
  • 65,327
  • 90
  • 227
  • 319
8
votes
2 answers

Dynamic order statistic: get k-th element in constant time?

So, I'm trying to implement a Data Structure to handle Dynamic Order Statistic. The Data Structure has following operations: add(x): inserts a new element with value x get(k): returns the k-th smallest element: k = ceiling(n/a), where n = amount of…
G.M
  • 530
  • 4
  • 20
8
votes
1 answer

Count total child node on left and right grouped by ranks

I am working on a project that rewards people base on referrals (MLM) I have been able to count the total number of child nodes on both left and right side but now I need to be able to update the ranks of users when they have a certain number of…
8
votes
1 answer

Find median in binary search tree

Write the implementation of the function T ComputeMedian() const that computes the median value in the tree in O(n) time. Assume that the tree is a BST but is not necessarily balanced. Recall that the median of n numbers is defined as follows: If n…
Nat
  • 107
  • 1
  • 2
  • 10
8
votes
2 answers

Dynamic prefix sum

Is there any data structure which is able to return the prefix sum [1] of array, update an element, and insert/remove elements to the array, all in O(log n)? [1] "prefix sum" is the sum of all elements from the first one up to given index For…
Ecir Hana
  • 10,864
  • 13
  • 67
  • 117