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

binary search tree use in real world programs?

Possible Duplicate: Where is binary search used in practice? What are the applications of binary trees? I have done various exercises involving adding,deleting,ordering and so on. However i am having a hard time visualizing the use of binary…
Win Coder
  • 6,628
  • 11
  • 54
  • 81
5
votes
2 answers

Dynamic Programming: Optimal Binary Search Tree

Allright, I'm hoping someone can explain this to me. I'm studying for finals and I can't quite figure something out. The problem is dynamic programming; constructing an optimal binary search tree (OBST). I understand dynamic programming in general…
rpmartz
  • 3,759
  • 2
  • 26
  • 36
5
votes
1 answer

Deletion in AVL Tree

As you know how avl should be balanced after deletion of a node, I'll get to point. For starting, Im considering deleting a node with no children. For Example a Tree: 10 / \ 5 17 / \ / \ 2 9 12 20 \ …
InamTaj
  • 276
  • 2
  • 7
  • 15
5
votes
1 answer

String Representation of Binary Search Tree

I've been trying to write a recursive string method for a binary search tree that returns a multiple line representation of a tree with preorder path info. Each node should be prefaced by a series of < and > characters showing the path that leads…
me0w0r
  • 53
  • 1
  • 3
5
votes
2 answers

Time complexity of BST inorder traversal if implemented this way

Well normally if using depth-first traversal, we get O(n) time. However, if we find the minimum element first then call the successor() method n times, what time complexity will it be? I think it may be O(n log n) because successor is O(log n) but…
hanlindev
  • 109
  • 2
  • 7
5
votes
3 answers

Find median in O(1) in binary tree

Suppose I have a balanced BST (binary search tree). Each tree node contains a special field count, which counts all descendants of that node + the node itself. They call this data structure order statistics binary tree. This data structure supports…
Michael
  • 41,026
  • 70
  • 193
  • 341
4
votes
6 answers

Bad Operand Types for Binary Operator ">"?

I am writing a BST Program. I get the error: "Bad Operand Types for Binary Operator ">" first type: java.lang.Object second type: java.lang.Object" This is the method where it gives me the error: public void placeNodeInTree(TreeNode current,…
svsav
  • 828
  • 3
  • 12
  • 29
4
votes
2 answers

Duplicate Entries in Binary Search Tree

I have a very simple question regarding BSTs. I have seen multiple definitions of BSTs regarding duplicate entries. Some define BSTs as not allowing duplicate entries, others that node's left child is <= to the nodes value and the right child is…
Tareq
  • 788
  • 6
  • 8
4
votes
2 answers

How to find out deepest node in the BST?

private int Depth(TreeNode node) { if (node == null) { return -1; } else { return 1 + Math.Max(Depth(node.LeftNode),Depth(node.RightNode)); } } I wrote method to find deepest node, but I am not sure about…
Desire
  • 563
  • 4
  • 13
  • 28
4
votes
5 answers

Stackoverflow exception when traversing BST

I have implement a link-based BST (binary search tree) in C++ for one of my assignment. I have written my whole class and everything works good, but my assignment asks me to plot the run-times for: a. A sorted list of 50000, 75000, and 100000…
Saad Imran.
  • 4,480
  • 2
  • 23
  • 33
4
votes
2 answers

Pseudocode for non-recursive implementation of tree height and isBST

I am in the process of converting recursive function for a BST to non recursive to help prepare for an interview. So far I figured out preorder, inorder, postorder, search, delete, insert, and converting the BST to a circular linked list. I am…
Aaron
  • 4,380
  • 19
  • 85
  • 141
4
votes
3 answers

Given a modified binary search tree, find k'th smallest element

Suppose in a given binary tree if each node contains number of child elements, then what is the optimal way to find k'th smallest element in the tree ? Please note this is not regular BST. Each node is containing number of child element under it.
Ajeet Ganga
  • 8,353
  • 10
  • 56
  • 79
4
votes
3 answers

What can a binary heap do that a binary search tree cannot?

This is something I do not quite understand. When I read literature on heaps, it always says that the big advantage of a heap is that you have the top (max if max heap) element immediately available. But couldn't you just use a BST and store a…
4
votes
1 answer

Difference between BST , Hashing , Tries and map

I read some blog and tutorial on Tries , hashing, Map(stl) and BST. I am very confused in which one is better to use and where. I know that to make such difference between them are nonsense because they are all implementation dependent. Would you…
Amit Pal
  • 10,604
  • 26
  • 80
  • 160
4
votes
1 answer

Binary Search Tree select method implementation

I'm currently going over Robert Sedgewick's Algorithms book. In the book I'm trying to understand the implementation of the select method in a Binary Search Tree. The author uses a BST to implement a symbol table. The author describes the select…
Steven Aguilar
  • 3,107
  • 5
  • 39
  • 89