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

Second max in BST

This is an interview question. Find the second max in BST. The max element is the rightmost leaf in the BST. The second max is either its parent or its left child. So the solution is to traverse the BST to find the rightmost leaf and check its…
Michael
  • 41,026
  • 70
  • 193
  • 341
20
votes
1 answer

GraphViz binary tree left and right child

i'm trying to draw binary tree using GraphViz but i have problems about left child and right child. There is a way to force a node to be right or left child? This is my sample code: digraph G{ 5 -> 3; 5 -> 8; 3 -> 1; 3 -> 4; 8 -> 6; 8 -> 12; }
Alberto
  • 2,881
  • 7
  • 35
  • 66
20
votes
5 answers

To find largest element smaller than K in a BST

Given a binary search tree and an integer K, i would like to find the largest element less than K. In the below tree, for K = 13, result = 12 for K = 10, result = 8 for K = 1 (or) 2, result = -1 10 5 12 2 8 11 14 I tried the…
josh
  • 13,793
  • 12
  • 49
  • 58
20
votes
4 answers

Why implement a Hashtable with a Binary Search Tree?

When implementing a Hashtable using an array, we inherit the constant time indexing of the array. What are the reasons for implementing a Hashtable with a Binary Search Tree since it offers search with O(logn)? Why not just use a Binary Search Tree…
Michael
  • 791
  • 2
  • 12
  • 32
20
votes
4 answers

Why are Haskell Maps implemented as balanced binary trees instead of traditional hashtables?

From my limited knowledge of Haskell, it seems that Maps (from Data.Map) are supposed to be used much like a dictionary or hashtable in other languages, and yet are implemented as self-balancing binary search trees. Why is this? Using a binary tree…
reem
  • 7,186
  • 4
  • 20
  • 30
19
votes
11 answers

How to find the closest element to a given key value in a binary search tree?

Given a bst with integer values as keys how do I find the closest node to that key in a bst ? The BST is represented using a object of nodes (Java). Closest will be for eg 4,5,9 and if the key is 6 it will return 5 ..
phoenix
  • 3,531
  • 9
  • 30
  • 31
18
votes
6 answers

Node search in Binary Tree overflows stack

I use the following method to traverse* a binary tree of 300 000 levels: Node* find(int v){ if(value==v) return this; else if(right && valuefind(v); else if(left && value>v) return…
18
votes
8 answers

How to do in-order traversal of a BST without recursion or stack but using parent pointers?

Is it possible to do an iterative in-order-traversal on a BST whose node has a parent pointer (the parent of the root is null) without using a visited flag or a stack? I googled and didn't find a reply. The point is, how can I know - at a certain…
OmarOthman
  • 1,718
  • 2
  • 19
  • 36
17
votes
10 answers

How to calculate the depth of a binary search tree

I would like to calculate the summation of the depths of each node of a Binary Search Tree. The individual depths of the elements are not already stored.
Jon
  • 187
  • 1
  • 2
  • 5
16
votes
6 answers

C# Binary Trees and Dictionaries

I'm struggling with the concept of when to use binary search trees and when to use dictionaries. In my application I did a little experiment which used the C5 library TreeDictionary (which I believe is a red-black binary search tree), and the C#…
Projectile Fish
  • 935
  • 3
  • 9
  • 26
16
votes
3 answers

Number of binary search trees over n distinct elements

How many binary search trees can be constructed from n distinct elements? And how can we find a mathematically proved formula for it? Example: If we have 3 distinct elements, say 1, 2, 3, there are 5 binary search trees.
siddstuff
  • 1,215
  • 4
  • 16
  • 37
15
votes
17 answers

Real world examples of tree structures

I'm looking for some examples of tree structures that are used in commercial/free software projects, modern or old. I can see examples on wikipedia, but I am looking for more concrete examples and how they're used. For example primary keys in…
Arec Barrwin
  • 61,343
  • 9
  • 29
  • 25
15
votes
2 answers

Fastest way to perform complex search on pandas dataframe

I am trying to figure out the fastest way to perform search and sort on a pandas dataframe. Below are before and after dataframes of what I am trying to accomplish. Before: flightTo flightFrom toNum fromNum toCode fromCode ABC DEF …
MaxB
  • 428
  • 1
  • 8
  • 24
15
votes
5 answers

Concrete examples of using binary search trees?

I understand how binary search trees are implemented, but I am not sure what are the advantages of using it over the hash tables that most programming languages have built into their standard libraries. Could someone please provide examples of…
Samnang
  • 5,536
  • 6
  • 35
  • 47
15
votes
1 answer

Remove recursively from a binary search tree

This is homework; please don't just give me code I have two methods: remove(T data) and removeRec(Node node, T data). In its current state, it seems my code only removes the root node of the BST. @Override public T remove(T data) { if (data…
anon