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

What is difference between Array and Binary search tree in efficiency?

I want know what is the best : Array OR Binary search tree in ( insert , delete , find max and min ) and how can I Improve both of them ?
12
votes
1 answer

What is a coarse and fine grid search?

I was reading this answer Efficient (and well explained) implementation of a Quadtree for 2D collision detection and encountered this paragraph All right, so actually quadtrees are not my favorite data structure for this purpose. I tend to prefer a…
jokoon
  • 6,207
  • 11
  • 48
  • 85
12
votes
2 answers

What are the advantages of T-trees over B+/-trees?

I have explored the definitions of T-trees and B-/B+ trees. From papers on the web I understand that B-trees perform better in hierarchical memory, such as disk drives and cached memory. What I can not understand is why T-trees were/are used even…
simeonz
  • 412
  • 5
  • 14
12
votes
2 answers

Using smart pointers for C++ binary search tree

I have implemented a binary search tree in c++. Instead of using bare pointers to point to a nodes children I used the std::shared_ptr. The nodes of the tree are implemented as follows struct treeNode; typedef std::shared_ptr
dayman
  • 123
  • 1
  • 5
12
votes
2 answers

Algorithm to exchange the roles of two randomly chosen nodes from a tree moving pointers

I have created an algorithm whose purpose should be of, given two nodes A and B in a BST, it switches the roles (or positions in the tree) of the two by simply moving pointers. In my representation of a BST, I am using a double linked connection…
nbro
  • 15,395
  • 32
  • 113
  • 196
12
votes
3 answers

Is a node in a tree considered its own ancestor?

I'm wondering what the consensus is on the definition of "ancestor" in a computer science context. I only ask because in Introduction to Algorithms, Second Edition, p. 259 there is a description of the algorithm Tree-Successor(x) that seems odd. In…
kostmo
  • 6,222
  • 4
  • 40
  • 51
12
votes
2 answers

What is the standard binary search tree structure to use in Scala?

What is the standard balanced binary search tree implementation one should use in Scala 2.10.x? I am looking around and it seems that AVLTree was removed and RedBlack is deprecated with a message (Since version 2.10.0) use TreeMap or TreeSet…
jbx
  • 21,365
  • 18
  • 90
  • 144
12
votes
1 answer

What is the difference between std::set and std::map

I'm relatively new to c++ programming and was wondering if someone could help clarify a few questions for me. http://www.cplusplus.com/reference/set/set/ http://www.cplusplus.com/reference/map/map/ I've been reading on how to implement STL binary…
Valrok
  • 1,514
  • 7
  • 30
  • 49
11
votes
4 answers

Does Cocoa Touch have a search tree data structure?

I've been looking into this on Google and read the Collections entry in the SDK documentation, and turned up nothing. Is there a BST (any of its variants) implementation available out of the box with the iOS SDK? It seems odd that something so…
schematic
  • 1,964
  • 1
  • 16
  • 20
11
votes
4 answers

Best self-balancing BST for quick insertion of a large number of nodes

I've been able to find details on several self-balancing BSTs through several sources, but I haven't found any good descriptions detailing which one is best to use in different situations (or if it really doesn't matter). I want a BST that is…
Jonesinator
  • 4,186
  • 2
  • 24
  • 18
11
votes
1 answer

Printing to a file in C

How do I print to an empty .txt file I already have created? I already print the results to the console, and now I want to print to a file named "Output.txt". I've tried a couple of things that haven't worked, but I think it was easier to create a…
Silent Phantom
  • 113
  • 1
  • 1
  • 6
11
votes
5 answers

Binary Tree Insert Algorithm

I recently finished implementing a Binary search tree for a project I was working on. It went well and I learned a lot. However, now I need to implement a regular Binary Tree... which for some reason has me stumped. I'm looking for a way to do my…
Taylor
  • 123
  • 1
  • 1
  • 7
11
votes
2 answers

Store the largest 5000 numbers from a stream of numbers

Given the following problem: "Store the largest 5000 numbers from a stream of numbers" The solution which springs to mind is a binary search tree maintaining a count of the number of nodes in the tree and a reference to the smallest node once the…
Rich
  • 3,781
  • 5
  • 34
  • 56
10
votes
2 answers

Implementing a balanced binary search tree?

I have implemented a binary search tree and I want to add more functionality in its insertion function to make it a self-balancing tree. I am coding in C#. Can anybody please suggest me good tutorials or links on this? I did some searches and…
Lost
  • 12,007
  • 32
  • 121
  • 193
10
votes
2 answers

Why do we sort via Heaps instead of Binary Search Trees?

A heap can be constructed from a list in O(n logn) time, because inserting an element into a heap takes O(logn) time and there are n elements. Similarly, a binary search tree can be constructed from a list in O(n logn) time, because inserting an…
Shuklaswag
  • 1,003
  • 1
  • 10
  • 27