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

Insert sorted array into binary search tree

I want to implement an algorithm that inserts sorted arrays into binary search trees but I don't want ending up with a tree that only grows to one side. Do you have any ideas? Thanks.
Ege
  • 941
  • 4
  • 17
  • 36
8
votes
1 answer

Confusion in the differences between hashmap and hashtable

I have a confusion: I have read in many posts that Hash-maps are implemented as binary search trees which makes the various operations time complexity of logarithmic order. Hashtables on the other hand provide constant time fetching. But, as I read…
Sankalp
  • 2,796
  • 3
  • 30
  • 43
8
votes
2 answers

Red Black Tree inserting: why make nodes red when inserted?

In Red Black Tree Properties doesn't contain any rule for insert node color, how ever always we are inserting red color nodes. If we insert black color node does it violate any Properties( any rule out of 4)?
8
votes
2 answers

Order of insertion for worst case black height of a red black tree

Lets say we are dealing with the keys 1-15. To get the worst case performance of a regular BST, you would insert the keys in ascending or descending order as follows: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 Then the BST would essentially…
Jordan
  • 4,928
  • 4
  • 26
  • 39
8
votes
2 answers

Is it always possible to turn one BST into another using tree rotations?

Given a set of values, it's possible for there to be many different possible binary search trees that can be formed from those values. For example, for the values 1, 2, and 3, there are five BSTs we can make from those values: 1 1 2 …
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
8
votes
3 answers

Perfect Balanced Binary Search Tree

I have an theoretical question about Balanced BST. I would like to build Perfect Balanced Tree that has 2^k - 1 nodes, from a regular unbalanced BST. The easiest solution I can think of is to use a sorted Array/Linked list and recursively divide the…
OlejkaKL
  • 521
  • 1
  • 9
  • 23
8
votes
4 answers

Find whether given sum exists over a path in a BST

The question is to find whether a given sum exists over any path in a BST. The question is damn easy if a path means root to leaf, or easy if the path means a portion of a path from root to leaf that may not include the root or the leaf. But it…
SexyBeast
  • 7,913
  • 28
  • 108
  • 196
7
votes
2 answers

Prove the efficiency of repeated calls to successor() in binary trees?

I need a hint for this exercise from the CLRS Algorithms book: Prove that no matter what node we start at in a height-h binary search tree, k successive calls to Tree-Successor take O(k+h) time.
ssierral
  • 8,537
  • 6
  • 26
  • 44
7
votes
6 answers

2 binary trees are equal or not

Possible Duplicate: Determine if two binary trees are equal Got an interview yesterday, a question got me, here it is: Description There are 2 binary trees, check if they are equal. They are equal if and only if tree1->child == tree2->child, and…
Alcott
  • 17,905
  • 32
  • 116
  • 173
7
votes
2 answers

deletion in a binary search tree

I have been given two binary search trees. For example, A and B. Next, I was asked to delete the tree B from the tree A. By deletion, I mean delete all the nodes present in B from A. Note: B is not necessarily a subtree of A. eg: A: …
letsc
  • 2,075
  • 5
  • 24
  • 43
7
votes
4 answers

How can I check if a BST is valid?

How can I check if a BST is a valid one, given its definition and using a generalized version of fold for BST? data(Ord a, Show a, Read a) => BST a = Void | Node { val :: a, left, right :: BST a } deriving (Eq, Ord, Read, Show) fold ::…
gremo
  • 47,186
  • 75
  • 257
  • 421
7
votes
4 answers

Inorder Traversal of Tree in Python Returning a List

I learnt implementing inorder traversal of a binary search tree: def inorder(root): # root has val, left and right fields if root==None: return inorder(root.left) print(root.val) inorder(root.right) Now, the problem is I do…
mourinho
  • 763
  • 6
  • 13
  • 24
7
votes
3 answers

Generating an optimal binary search tree (Cormen)

I'm reading Cormen et al., Introduction to Algorithms (3rd ed.) (PDF), section 15.4 on optimal binary search trees, but am having some trouble implementing the pseudocode for the optimal_bst function in Python. Here is the example I'm trying to…
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
7
votes
3 answers

Split a binary search Tree

Given a BST tree, we have to break the tree depending on the input(N), into two subtrees, where subtree1 consists of all the nodes, which are less than or equal to N and subtree2 consists of all the nodes which are greater than N. 50 …
amrender singh
  • 7,949
  • 3
  • 22
  • 28
7
votes
4 answers

What are the advantages of binary search trees with parent pointers?

Up until this point, I've been implementing Binary Search Trees with left and right pointers, like: template struct BSTNode{ BSTNode* left; BSTNode* right; T data; } I came across implementations where nodes also had…
happy_sisyphus
  • 1,693
  • 1
  • 18
  • 27