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

Best Data Structure for storing license plates and searching if a given license plate exists

I am attempting to write a program that determines whether a specific license plate is one of 10,000 that I have stored. I want to write a fast response algorithm first and foremost, with memory usage as a secondary objective. Would a balanced…
5
votes
2 answers

Best practise for creating BST : python

So I am implementing a Binary Search Tree but am confused as to whether I should have a Node class as well as a Bst class or just one class. The reason why is because I know a BST is made up of nodes but at any point you can take a node and all the…
5
votes
3 answers

how to determine a balanced or perfectly balanced Binary search tree ( just from the picture )

I am not sure how to determine if a tree is balanced, perfectly balanced, or neither if I have it as a picture not a code For example if I have this tree How can I check if it's balanced, perfectly balanced, or unbalanced? and can someone give me an…
rullzing
  • 622
  • 2
  • 10
  • 22
5
votes
6 answers

Remove method binary search tree

I am trying to implement a remove method for the BST structure that I have been working on. Here is the code with find, insert, and remove methods: public class BST { BSTNode root = new BSTNode("root"); public void insert(BSTNode root,…
dawich77
  • 92
  • 1
  • 3
  • 11
5
votes
2 answers

In Haskell, how to generate a perfectly balanced binary search tree?

The function should takes a list xs and constructs a balanced binary search tree consisting of exactly the same set of elements as xs. The result should be like this: (if the list is [1,2,3,4,5,6,7,8]) Node (Node (Node (Node Empty 1 Empty) 2 Empty)…
Zip
  • 809
  • 2
  • 14
  • 30
5
votes
5 answers

maximum length of a descending path in a tree which always goes left|right

I'm prepearing for tech interview, so basically learning algorithms from very beginning :) we are given a BST. I need to find the max length of a desc path in it, which always goes left or right In other words, an example tree's descending path is…
tania
  • 1,086
  • 2
  • 12
  • 31
5
votes
1 answer

Why does the AVL tree in Map of OCaml use balance factor (height diff) 2 instead of 1?

According to AVL tree wiki The balance factor is calculated as follows: balanceFactor = height(left-subtree) - height(right-subtree). For each node checked, if the balance factor remains −1, 0, or +1 then no rotations are necessary. However, in…
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
5
votes
3 answers

Relationship between number of nodes and height

I am reading The Algorithm Design Manual. The author states that the height of a tree is: h = log n, where h is height n = number of leaf nodes log is log to base d, where d is the maximum number of children allowed per node. He then goes on to…
Quest Monger
  • 8,252
  • 11
  • 37
  • 43
5
votes
1 answer

Understanding Binary Search Tree construction

I am having a difficult time understanding how binary search trees are built. Do they require the initial data to be sorted in order to find the top most root?
George L
  • 1,673
  • 2
  • 26
  • 39
5
votes
2 answers

Quickly finding if there are 2 or more equal numbers

I have an array of N different numbers that change frequently. After every change there is a chance that two or more numbers have become equal, and I don't want that. The number N can be as big as the maximum possible integer. Knowing that changes…
5
votes
1 answer

Is it possible to have multiple valid BSTs for a given set of data?

Given a set of data in a binary search tree, like the numbers 1 to 10, is it possible for multiple balanced binary search trees to exist? Or is there just one, unique balanced BST for that set of data? Thanks
5
votes
4 answers

Binary Search Tree C implementation

I recently wrote a fairly simple piece of code attempting to implement a Binary Search Tree in C with insertion, search, deletion and display operations. Unfortunately, the code does not seem to work. #include #include struct…
Pastafarian
  • 2,080
  • 4
  • 18
  • 22
5
votes
5 answers

Finding elements in a BST that sum up to a provided value

I'm trying to get an approach to solve the problem Find two elements in balanced BST which sums to a given a value. Constraints Time O(n) and space O(logn). I was wondering whether the following algorithm would be valid. int[] findSum(Node root,…
seeker
  • 6,841
  • 24
  • 64
  • 100
5
votes
4 answers

Turning a Binary Tree to a sorted array

Is there a way to turn a Binary to a sorted array without having to traverse the tree for every array index? Node root; Node runner; int current_smallest; void findsmallest(Node root){ //Pre-order traversal if(root == null){ …
MosesA
  • 925
  • 5
  • 27
  • 53
5
votes
2 answers

Binary String Search - minimum bin width?

I happen to be building the binary search in Python, but the question has more to do with binary search structure in general. Let's assume I have about one thousand eligible candidates I am searching through using binary search, doing the classic…
DeaconDesperado
  • 9,977
  • 9
  • 47
  • 77