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

Balancing a Binary Search Tree (BST)

I'm trying to make a balance_bst(bstNode root) function, but i'm struggling with the implementation. I'm implementing the function as a template function, since my bstNode class is a template class. Here is (some of) my code: template
MRT89
  • 299
  • 3
  • 7
  • 16
5
votes
8 answers

How to delete a node with 2 children nodes in a binary search tree?

How to delete a node with 2 children nodes in a binary tree? Is there any kind of method to remove it? I googled it. But didn't get clear idea about it. Anybody explain it with diagrammatic representation? How to delete the node '5' from the this…
Dinesh
  • 16,014
  • 23
  • 80
  • 122
5
votes
3 answers

Exam question about inserting to a empty binary search tree

I'm having trouble interpreting a certain question about inserting elements to a binary search tree. I'm familiar with preorder, postorder, and inorder traversals, but I'm unfamiliar with the following question: Suppose that we insert the elements…
Jigglypuff
  • 1,433
  • 6
  • 24
  • 38
5
votes
3 answers

Implement a PriorityQueue using a BinarySearchTree : Java

I need to "create a priority queue implemented by a binary search tree (BST)" for my algorithms II class. However, I'm not sure exactly how you would use a binary search tree as a priority queue. Could someone clarify what it is that the assignment…
Chris V.
  • 1,123
  • 5
  • 22
  • 50
5
votes
1 answer

Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with…
user11138349
5
votes
2 answers

Is Valid Generic Binary Search Tree with Recursion

To check the validity of a Binary Search Tree, I use a method. But the method always returns false, when tested against a valid Binary Search Tree. Online Demo Here The code public class Program { public static void Main() { …
Bayu
  • 437
  • 2
  • 10
  • 19
5
votes
2 answers

Non-balanced binary tree

I enjoy reading engaging book "Programming in Haskell" (Second Edition) by Graham Hutton. In Chapter "8 Declaring Types and classes", section "8.4 Recursive types", page 97 bottom I find definition of binary tree: data Tree a = Leaf a | Node (Tree…
Mun Dong
  • 53
  • 4
5
votes
2 answers

.NET equivalent of Java's TreeSet.floor & TreeSet.ceiling

As an example, there's a Binary Search Tree which holds a range of values. Before adding a new value, I need to check if it already contains it's 'almost duplicate'. I have Java solution which simply performs floor and ceiling and further condition…
user2376997
  • 501
  • 6
  • 22
5
votes
3 answers

How to find the rank of a node in an AVL tree?

I need to implement two rank queries [rank(k) and select(r)]. But before I can start on this, I need to figure out how the two functions work. As far as I know, rank(k) returns the rank of a given key k, and select(r) returns the key of a given…
tvguide1234
  • 197
  • 1
  • 3
  • 8
5
votes
1 answer

C# Binary Search Tree giving Incorrect Majority Element

The main reason I'm using the BST is to get the Majority Element, being the Value > Array.Length / 2. So, if we have an array of 5 elements, there must be a minimum of at least 3 to be considered the majority. Now the problem I am facing at the…
user7943
  • 803
  • 7
  • 20
5
votes
1 answer

C++ treeset implementation with templates

I have to write a template for a treeset. The size of a leaf is 0. When the create_empty_set() is called it should make a leaf, when you add T data the leaf should become a branch and its value should be put in the left or right. No duplicates are…
Kelvijn
  • 179
  • 4
  • 17
5
votes
1 answer

What are the advantages of splay tree over hash table?

Since splay trees are used for caching I was wondering what are the advantages of Splay Tree over HashTable when I want to cache efficiently? When should I prefer splay tree over hash table? I guess it is a more specialized case than BST, there…
5
votes
4 answers

MySQL - How to find word with the most similar beginning

How to find varchar-word that has the most similar beginning of the specified word in MySQL database? For example: +-------------------+ | word_column | +-------------------+ | StackOferflow | | StackExchange | | MetaStackExchange | |…
Palindromer
  • 854
  • 1
  • 10
  • 29
5
votes
0 answers

Benefit of a sentinel node in a red black tree?

I created a doubly-linked list, and the benefits of a sentinel node were clear - no null checks or special cases at list boundaries. Now I'm writing a red black tree, and trying to figure out if there is any benefit to such a concept. My…
5
votes
2 answers

Balanced Binary Tree Vs Balanced Binary Search Tree

For each of these operations would a balanced binary search tree complete the task in a faster big oh time than a balanced binary tree? Finding the smallest item in the tree. I think balanced BST would have a faster big oh time than a balanced…
Jack Kong
  • 109
  • 1
  • 10