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

Time complexity of finding k successors in BST

Given a binary search tree (BST) of height h, it would take O(k+h) time to apply the BST InOrder Successor algorithm k times in succession, starting in any node, applying each next call on the node that was returned by the previous call. Pseudo…
GameOfChess
  • 95
  • 2
  • 8
5
votes
3 answers

Cannot borrow node as mutable more than once while implementing a binary search tree

I'm trying to implement a binary search tree in Rust and I am running into problems with inserting an element. What is an idiomatic way of doing this in Rust? Here is my implementation: use std::cmp::Ordering; pub struct BinarySearchTree { …
brodie
  • 61
  • 5
5
votes
5 answers

Binary Search Trees

This is some code found on wikipedia regarding BST : # 'node' refers to the parent-node in this case def search_binary_tree(node, key): if node is None: return None # key not found if key < node.key: return…
Martin
  • 905
  • 9
  • 19
5
votes
4 answers

Sorted list to complete BST array representation

I'm wondering whether there is a mapping between a sorted array (e.g., [1, 2, 3, 4, 5, 6]) and the representation that one obtains when one constructs a complete binary search tree from this sorted array, and expresses said binary search tree as an…
sga001
  • 158
  • 1
  • 9
5
votes
4 answers

C# Most efficient data structure to insert and to remove lower half

Imagine I have a big sorted list of integers (>1000 items). I need to be able to do two operations on this list: remove the lower half and fill the list again to its original size by inserting random integers. Because I do these operations about a…
Safron
  • 802
  • 1
  • 11
  • 23
5
votes
1 answer

How can I traverse binary search tree without recursion?

I can traverse the binary search tree using recursion easily, but I don't have an idea about traverse without recursion so please anyone explain,.....
Meena Patel
  • 71
  • 1
  • 3
5
votes
4 answers

Find all subtrees in a BST whose keys lie in a given range

I was given this question during a recent interview: Given a BST whose nodes contains an Integer as value, find all subtrees whose nodes fall between integers X (min) and Y (max), where X
Quest Monger
  • 8,252
  • 11
  • 37
  • 43
5
votes
5 answers

Find the parent node of a node in binary search tree

So I want to find the parent node of a Node in a binary tree. Suppose that I input 30,15,17,45,69,80,7 in the tree through a text file. The tree should be 30 15 45 7 17 69 …
Hòa Ngô
  • 53
  • 1
  • 1
  • 3
5
votes
5 answers

Using wrapper class instead of static variables

This is my first question ever at StackOverFlow: I am studying to interviews with the help of "Cracking the code interview" (5th Edition) book, and I was solving the next problem: Implement a function to check if a binary tree is a binary search…
vigdora
  • 319
  • 4
  • 11
5
votes
3 answers

What's the complexity of map/set :: insert if one has provided a correct iterator hint?

Is it O(1) or O(logN) but with a smaller coefficient? If this is unspecified, I'd at least like to know the answer based on a reasonable supposition that the map/set is implemented using a red-black or AVL tree. The general algorithm to insert an…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
5
votes
3 answers

Computing rank of a node in a binary search tree

If each node in a binary search tree stores its weight (number of nodes in its subtree), what would be an efficient method to compute a rank of a given node (its index in the sorted list) as I search for it in the tree?
dissem
  • 199
  • 1
  • 1
  • 5
5
votes
2 answers

Using unique_ptr instead of shared_ptr in BST

I am trying to implement BST with unique_ptr. I got a working program for shared_ptr. How do I go about using unique_ptr instead to enforce the single ownership semantics of the BinarySearchTree? When I replace shared_ptr with unique_ptr, I get…
Ian McGrath
  • 982
  • 2
  • 10
  • 23
5
votes
2 answers

TreeMap removing all keys greater than a certain key

In a project I need to remove all objects having key value greater than a certain key (key type is Date, if it matters). As far as I know TreeMap implemented in Java is a red-black tree which is a binary search tree. So I should get O(n) when…
Hongxiang Qiu
  • 63
  • 1
  • 3
5
votes
1 answer

Red Black Tree - max number of rotations needed for K insertions and K deletions?

What's the maximum number of rotations required after K insertions and K deletions in a Red Black tree? I'm thinking its 3K as in the worst case scenario for insertion we perform 2 rotations for every insertion and 1 rotation for every deletion. Am…
Ned
  • 355
  • 2
  • 9
  • 24
5
votes
3 answers

Building a BST from a depth-first preorder list in Haskell more idiomatically

This submission to Programming Praxis gives an O(n) function that "undoes" a preorder traversal of a binary search tree, converting a list back into a tree. Supplying the missing data declaration: data Tree a = Leaf | Branch {value::a, left::Tree…
dfeuer
  • 48,079
  • 5
  • 63
  • 167