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
6
votes
1 answer

Trie implementation with wildcard values

I'm implementing an algorithm to do directory matching. So I'm given a set of valid paths that can include wildcards (denoted by "X"). Then when I pass in an input I need to know if that input matches with one of the paths in my valid set. I'm…
6
votes
3 answers

Creating a Binary Search Tree from a sorted array

I am currently checking about coding an algorithms. If we have the following case: Given a sorted (increasing order) array with unique integer elements, wrote an algorithm to create a binary search tree with minimal height. The following code is…
Tk421
  • 6,196
  • 6
  • 38
  • 47
6
votes
4 answers

Why Binary Search Trees?

I was reading binary search tree and was thinking that why do we need BST at all? All the things as far as I know can also be achieve using simple sorted arrays. For e.g. - In order to build a BST having n elements, we requires n*O(log n) time i.e.…
Ravi Gupta
  • 6,258
  • 17
  • 56
  • 79
6
votes
2 answers

Tree sort: time complexity

Why is the average case time complexity of tree sort O(n log n)? From Wikipedia: Adding one item to a binary search tree is on average an O(log n) process (in big O notation), so adding n items is an O(n log n) process But we don't each time…
rapt
  • 11,810
  • 35
  • 103
  • 145
6
votes
3 answers

My hash table is slower than binary search

I've implemented binary search, linear search and a hash table to compare each time complexity. The problem is that somehow, my hash table is much slower than the binary search when I measure time to find prime numbers. Below is my code: // Make the…
6
votes
4 answers

Checking if a Binary Search Tree is Valid javascript

I came across this problem online and I found the following function to check if a BST is valid. However, what I don't fully understand is how max/min change from null to values that you can compare against. so in the following function: //Give the…
devdropper87
  • 4,025
  • 11
  • 44
  • 70
6
votes
1 answer

delete node in binary search tree python

The code below is my implement for my binary search tree, and I want to implement delete method to remove the node. Below is my implementation, but when I perform bst =…
Alexander
  • 282
  • 1
  • 4
  • 13
6
votes
4 answers

Why B-Tree for file systems?

I know this is a common question and I saw a few threads in Stack Overflow but still couldn't get it. Here is an accepted answer from Stack overflow: " Disk seeks are expensive. B-Tree structure is designed specifically to avoid disk seeks as…
6
votes
1 answer

represent binary search trees in python

how do i represent binary search trees in python?
Bunny Rabbit
  • 8,213
  • 16
  • 66
  • 106
6
votes
3 answers

Java : setting object to null within a method has no effect (Reusing code)

I am trying to write a method to delete a Node from a Binary Search Tree. Here is my method to delete a node. public void delete(int deletionNodeValue) { Node nodeToBeDeleted = getNode(deletionNodeValue); if(nodeToBeDeleted == null)…
Nash Vail
  • 848
  • 2
  • 11
  • 27
6
votes
1 answer

Shortest path in a custom binary search tree

This a question from a coding competition The original question can be found here http://www.olympiad.org.za/olympiad/wp-content/uploads/2014/03/2013-PO-Question-Paper.pdf Question 5 SHORTEST PATH THROUGH THE HALL [by Alan Smithee of Hulsbos…
6
votes
1 answer

Using generators to perform an inorder tree traversal on a BST

So given the following: def inorder(t): if t: inorder(t.left) yield t.key inorder(t.right) x = [ n for n in inorder(r) ] x only contains the root node, why? Here's the full code; note that the BST implementation is…
laurids
  • 931
  • 9
  • 24
6
votes
3 answers

Binary Tree static methods in Java

I have these instance methods in my Java implementation of Binary Tree and Search Binary Tree: getSize(), getHeight(), getDepth(), getPreOrder(), getInOrder(), getPostOrder() and getLevelOrder(). These methods use the root of the tree in others…
RobinHood
  • 377
  • 4
  • 20
6
votes
2 answers

Storing a binary tree in an array

Suppose that there's a binary tree like the one below that needs to be stored in an array. 7 / \ 1 10 /\ 9 11 And I found that the formula for storing the nodes in the array begins with storing the root node at position 0, and…
VCODE
  • 535
  • 5
  • 19
6
votes
2 answers

Binary Search Tree in C

I'm a Python guy. Learning C language and I've been trying to implement Binary Search Tree in C. I wrote down the code, and I've been trying from few hours but, not able to get the output as expected. Please help! Please correct…
heapzero
  • 1,413
  • 2
  • 13
  • 18