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
365
votes
14 answers

Difference between binary tree and binary search tree

Can anyone please explain the difference between binary tree and binary search tree with an example?
Neel
  • 9,913
  • 16
  • 52
  • 74
226
votes
6 answers

Why is std::map implemented as a red-black tree?

Why is std::map implemented as a red-black tree? There are several balanced binary search trees (BSTs) out there. What were design trade-offs in choosing a red-black tree?
Denis Gorodetskiy
  • 2,884
  • 3
  • 21
  • 23
212
votes
8 answers

Heap vs Binary Search Tree (BST)

What is the difference between a heap and BST? When to use a heap and when to use a BST? If you want to get the elements in a sorted fashion, is BST better over heap?
kc3
  • 4,281
  • 7
  • 20
  • 16
126
votes
18 answers

Advantages of Binary Search Trees over Hash Tables

What are the advantages of binary search trees over hash tables? Hash tables can look up any element in Theta(1) time and it is just as easy to add an element....but I'm not sure of the advantages going the other way around.
Devoted
  • 177,705
  • 43
  • 90
  • 110
77
votes
26 answers

Finding height in Binary Search Tree

I was wondering if anybody could help me rework this method to find the height of a binary search tree. So far, my code looks like this. However, the answer I'm getting is larger than the actual height by 1. But when I remove the +1 from my return…
mike
  • 3,339
  • 6
  • 30
  • 34
69
votes
33 answers

How do you validate a binary search tree?

I read on here of an exercise in interviews known as validating a binary search tree. How exactly does this work? What would one be looking for in validating a binary search tree? I have written a basic search tree, but never heard of this concept.
GurdeepS
  • 65,107
  • 109
  • 251
  • 387
55
votes
2 answers

Difference between AVL trees and splay trees

I am studying about various trees, and came across AVL trees and splay trees. I want to know What is difference between AVL trees and splay trees? On what basis do we select these tress? What are positive's and negative's of these trees? What are…
venkysmarty
  • 11,099
  • 25
  • 101
  • 184
51
votes
18 answers

How to implement a binary search tree in Python?

This is what I've got so far but it is not working: class Node: rChild,lChild,data = None,None,None def __init__(self,key): self.rChild = None self.lChild = None self.data = key class Tree: root,size = None,0 …
chochim
  • 1,710
  • 5
  • 17
  • 30
46
votes
4 answers

Rebalancing an arbitrary BST?

Reference: I was asked this question @MS SDE interview, 3rd round. And it's not a homework problem. I also gave it a thought and mentioning my approach below. Question: Modify a BST so that it becomes as balanced as possible. Needless to mention,…
ADJ
  • 1,182
  • 2
  • 14
  • 26
43
votes
13 answers

Difference between a LinkedList and a Binary Search Tree

What are the main differences between a Linked List and a BinarySearchTree? Is BST just a way of maintaining a LinkedList? My instructor talked about LinkedList and then BST but did't compare them or didn't say when to prefer one over another. This…
39
votes
4 answers

Difference between binary search and binary search tree?

What is the difference between binary search and binary search tree? Are they the same? Reading the internet it seems the second is only for trees (up to 2 children nodes) and binary search doesn't follow this rule. I didn't quite get it.
RollRoll
  • 8,133
  • 20
  • 76
  • 135
39
votes
2 answers

Built-in binary search tree in Python?

Are there any self-balancing binary search tree (RED-BLACK, AVL or others) built-in types in Python 2.7 or Python 3.x? I am looking for something equivalent to Java's TreeMap or TreeSet. If there are no such built-ins, why have they been ommited? Is…
Alexandru Chirila
  • 2,274
  • 5
  • 29
  • 40
35
votes
8 answers

Implementing an iterator over a binary search tree

I've been coding up a bunch of different binary search tree implementations recently (AVL, splay, treap) and am curious if there's a particularly "good" way to write an iterator to traverse these structures. The solution I've used right now is to…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
33
votes
4 answers

PHP daylight saving time detection

I need to send an email to users based wherever in the world at 9:00 am local time. The server is in the UK. What I can do is set up a time difference between each user and the server's time, which would then perfectly work if DST didn't…
Nicolas
  • 2,754
  • 6
  • 26
  • 41
33
votes
11 answers

How to Serialize Binary Tree

I went to an interview today where I was asked to serialize a binary tree. I implemented an array-based approach where the children of node i (numbering in level-order traversal) were at the 2*i index for the left child and 2*i + 1 for the right…
worker1138
  • 2,071
  • 5
  • 29
  • 36
1
2 3
99 100