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

What does the AVL stand for in AVL tree?

An AVL tree is the same as a self-balancing binary search tree. What does AVL stand for? Does it have to do with the name of the inventor?
Jing Ma
  • 183
  • 2
  • 12
7
votes
2 answers

Most efficient way to insert an element into sorted array and find its index

I need to insert an element into sorted range, but I also need to know its index (number of elements in range that are less then the element). I want to do this in O(logN) time. Can I do this with basic C++ containers? I was thinking to use…
7
votes
3 answers

Advantage of Binary Search Tree over vector in C++

What is the use of data structure Binary Search Tree, if vector (in sorted order) can support insert,delete and search in log(n) time (using binary search)??
7
votes
3 answers

Hash table - implementing with Binary Search Tree

From Cracking the Coding Interview, page 71: Alternatively, we can implement hash table with a BST. We can then guarantee an O(log n) lookup time, since we can keep the tree balanced. Additionally we may use less space, since a large array no …
Divyanshu Jimmy
  • 2,542
  • 5
  • 32
  • 48
7
votes
4 answers

How does this inorder traversal algorithm work?

I don't have much experience with recursion, so I'm having a hard time determining exactly how this algorithm works: public static void inorder(Node n) { if (n != null) { inorder(n.getLeft()); System.out.print(n.data + " "); …
Haque1
  • 575
  • 1
  • 11
  • 21
7
votes
2 answers

What's an idiomatic way to implement a red-black tree in Go?

I'm new to Go and have implemented a binary search tree. The tree can store any value (specifically, anything that implements interface{}). I'd like to build upon this implementation to create a self-balancing red-black tree. In an object-oriented…
Brian Gesiak
  • 6,648
  • 4
  • 35
  • 50
7
votes
1 answer

implementing binary search tree insertion in Haskell

I'm implementing the insert function of a BST, below is my code: data Tree a = Empty | Branch a (Tree a) (Tree a) deriving (Show, Eq) tinsert :: Tree a -> a -> Tree a tinsert Empty a = Branch a Empty Empty tinsert (Branch…
J Freebird
  • 3,664
  • 7
  • 46
  • 81
7
votes
1 answer

Balancing a binary search tree

Ok, I am trying to get a binary search tree to balance, and I know why it's not working, but I don't know how to fix it. This is what I have for my balancing methods. public void balance(){ if(isEmpty()){ System.out.println("Empty…
Neonjoe
  • 85
  • 2
  • 2
  • 7
7
votes
8 answers

Iterative BST insertion in C++

I am trying to understand BSTs and how to insert elements into it iteratively. My node structure implementation looks like so: struct Node{ Node *left; Node *right; T data; //template class }; And my insertion implementation looks…
Samuel
  • 231
  • 3
  • 5
  • 13
7
votes
2 answers

Recursion: Returning a list in order traversal

I have a basic function that does an in order traversal in C++: void inorder(Node *root) { if(root != NULL) { inorder(root->left); cout<data<right); } } However, I would like to return a…
Ali
  • 5,338
  • 12
  • 55
  • 78
7
votes
2 answers

recursive delete on a binary tree

I am trying to understand how the recursive method of deletion of a binary search tree works. The code that I came across in many places looks as follows: void destroy_tree(struct node *leaf) { if( leaf != 0 ) { destroy_tree(leaf->left); …
Simon Righley
  • 4,538
  • 6
  • 26
  • 33
7
votes
5 answers

How many permutations of a given array result in BST's of height 2?

A BST is generated (by successive insertion of nodes) from each permutation of keys from the set {1,2,3,4,5,6,7}. How many permutations determine trees of height two? I been stuck on this simple question for quite some time. Any hints anyone. By…
7
votes
2 answers

Complexity of inserting n numbers into a binary search tree

I have got a question, and it says "calculate the tight time complexity for the process of inserting n numbers into a binary search tree". It does not denote whether this is a balanced tree or not. So, what answer can be given to such a question? If…
7
votes
2 answers

How to display a binary search tree using CSS, HTML and a bit of Javascript?

I want to "paint" the tree on screen using CSS and HTML and not represent it in any way or data structure ...
mythbu
  • 657
  • 2
  • 7
  • 16
7
votes
5 answers

Recursive Binary Search Tree Insert

So this is my first java program, but I've done c++ for a few years. I wrote what I think should work, but in fact it does not. So I had a stipulation of having to write a method for this call: tree.insertNode(value); where value is an int. I…
Nick Sinklier
  • 221
  • 1
  • 3
  • 9