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
10
votes
4 answers

Array to Binary Search Trees Quick

Given an array of integers, is there a way to convert it into Binary Search Tree (unbalanced) quickly? I have tried inserting it one by one for each element, but this means that I have to traverse from the beginning for each insertion. It works…
jamesalone
  • 301
  • 1
  • 6
  • 19
10
votes
4 answers

Algorithm- Sum of distances between every two nodes of a Binary Search Tree in O(n)?

The question is to find out sum of distances between every two nodes of BinarySearchTree given that every parent-child pair is separated by unit distance. It is to be calculated after every insertion. ex: ->first node is inserted.. (root) …
10
votes
2 answers

Handling duplicates keys within an AVL tree

I want to make my avl-tree support duplicate keys but there is a problem with the default behavior of the binary search tree with duplicates that the rotation could make nodes with equal key be on the left and the right of the parent. For example…
Ahmed Kotb
  • 6,269
  • 6
  • 33
  • 52
10
votes
4 answers

Find number of permutations of a given sequence of integers which yield the same binary search tree

Given an array of integers arr = [5, 6, 1]. When we construct a BST with this input in the same order, we will have "5" as root, "6" as the right child and "1" as left child. Now if our input is changed to [5,1,6], our BST structure will still be…
Anonymous
  • 101
  • 1
  • 1
  • 3
10
votes
5 answers

Inserting an element in Binary Tree

Tried exploring a lot over the net, but could get any help, Everywhere its like adding a node to the Binary Search tree. Question: Requesting for algorithm and code snippet for adding a node to the Binary tree. ( or point me to correct URL…
Raa
  • 321
  • 2
  • 5
  • 16
10
votes
2 answers

What's the difference between `ImmutableSortedSet` and fsharp `Set`?

BCL has introduced a group of Immutable Collections I am wondering what's the difference between ImmutableSortedSet and the native FSharp Set? It seems that the performance signatures of both are similar. Also I saw somewhere that SortedSet is…
colinfang
  • 20,909
  • 19
  • 90
  • 173
10
votes
3 answers

Iterate through binary search tree to find all leaves

I am pretty new to trees, and I am trying to create kind of a "leaf iterator". I'm thinking it should put all nodes that does not have a .left and .right value onto a stack, but I'm not sure how or even if it's the right thing to do. I have tried…
Sti
  • 8,275
  • 9
  • 62
  • 124
10
votes
3 answers

Given a sorted integer array, how may Binary Search trees can be formed from it?

Consider I have an array [3,18,15,25,26], how many possible binary search trees can be formed from it?
Rahul
  • 44,892
  • 25
  • 73
  • 103
10
votes
10 answers

Java : How do I implement a generic Binary Search Tree?

Until now, I have been writing a Node class as class Node { private value; private Node left; private Node right; public int getValue() { return value; } public void setValue(int…
daydreamer
  • 87,243
  • 191
  • 450
  • 722
9
votes
1 answer

Java generics issue: Class "not within bounds of type-variable" error.

I'm working on a project for class that involves generics. public interface Keyable {public String getKey();} public interface DataElement extends Comparable>, Keyable, Serializable {...} public class Course…
Sam
  • 387
  • 1
  • 6
  • 15
9
votes
5 answers

Find whether a tree is a binary search tree in Haskell

type BSTree a = BinaryTree a data BinaryTree a = Null | Node (BinaryTree a) a (BinaryTree a) deriving Show flattenTree :: BinaryTree a -> [a] flattenTree tree = case tree of Null -> [] Node left val right…
Jayyyyyy
  • 197
  • 1
  • 10
9
votes
3 answers

How to Find the Branching Factor of a Tree

A particular search tree has 6 nodes at level 3. At the next level, there are 24 nodes. What is the branching factor at level 3? The answer is 4, but can someone tell me why, I thought that it was 2.
blazing
  • 557
  • 2
  • 4
  • 20
9
votes
0 answers

What is the paradigmatic way to create a Rust tree with a parent pointer?

I need to define a binary search tree where each node has access to the parent: enum Tree<'a> { Leaf, Node { left: Box>, right: Box>, parent: &'a Tree<'a>, data: u64, } } impl <'a>…
user1413793
  • 9,057
  • 7
  • 30
  • 42
9
votes
4 answers

How to traverse through all possible paths to a solution and pick the optimum path

I am not good at programmatically implementing a heuristic search algorithm / Dijkstra's algorithm/ A* search algorithm mentioned. However, while solving a problem mentioned in one of my post (Matrix manipulation: logic not fetching correct answer…
yeppe
  • 679
  • 1
  • 11
  • 43
9
votes
4 answers

Deletion procedure for a Binary Search Tree

Consider the deletion procedure on a BST, when the node to delete has two children. Let's say i always replace it with the node holding the minimum key in its right subtree. The question is: is this procedure commutative? That is, deleting x and…
Metz
  • 675
  • 1
  • 9
  • 17