Binary Search Trees
Implementation and Operations
17
SoftUni Team
9
Technical Trainers
Software University
http://softuni.bg
6
19
12
25
Table of Contents
1. Binary Search Trees
2. BST Operations
Search()
DeleteMin()
Range()
2
Have a Question?
sli.do
#DsAlgo
3
Binary Search Trees
Two Children at Most
Binary Search Trees
Binary search trees are ordered
For each node x
what about ==
Elements in left subtree of x are < x
Elements in right subtree of x are > x
17
9
25
nodes are < 17
3
11
20
31
5
BST - Search
Search for x in BST
if node is not null
17
if x < node.value go left
else if x > node.value go right
else if x == node.value return node
9
6
19
12
25
Search 12 17 9 12
Search 27 17 19 25 null
6
BST - Insert
Insert x in BST
17
if node is null insert x
else if x < node.value go left
9
19
else if x > node.value go right
else node exists
6
12
25
Insert 12 17 9 12 return
Insert 27 17 19 25 null(insert)
7
Problem: BST
You are given a skeleton
Implement BinarySearchTree<T>
bool Contains(T value)
void Insert(T value)
void EachInOrder(Action<T>)
Same as before
2
1
3
8
Solution: BST Node
Inside BST
private Node root;
private class Node
{
public Node(T value)
{
this.Value = value;
}
public T Value { get; set; }
public Node Left { get; set; }
public Node Right { get; set; }
}
9
Solution: BST Contains
public bool Contains(T value)
{
Node current = this.root;
while (current != null)
{
if (value.CompareTo(current.Value) < 0)
current = current.Left;
else if (value.CompareTo(current.Value) > 0)
current = current.Right;
else
break;
}
return current != null;
}
10
Solution: BST Insert
public void Insert(T value)
{
if (this.root == null)
{
this.root = new Node(value);
}
return;
Node parent = null;
Node current = this.root;
while(current != null) { //TODO: search for node }
//TODO: insert node
}
11
Problem: BST Search
Implement:
BST<T> Search(T value)
Make sure the method works for:
empty tree
tree with one element
tree with two elements - root + left/right
tree with multiple elements
12
Solution: BST Search
public BinarySearchTree<T> Search(T item)
{
Node current = this.root;
while (current != null)
{
if (item.CompareTo(current.Value) < 0)
current = current.Left;
else if (item.CompareTo(current.Value) > 0)
current = current.Right;
else
break;
}
return new BinarySearchTree<T>(current);
}
13
Solution: BST Search (2)
private BinarySearchTree(Node root)
{
this.Copy(root);
}
private void Copy(Node node)
{
if (node == null) return;
this.Insert(node.Value);
this.Copy(node.Left);
this.Copy(node.Right);
Pre-Order
Traversal
}
14
Lab Exercise
BST - Insert, Contains, Search
BST - Search Operation Speed - Quiz
TIME’S UP!
TIME:
What is the speed of the Search(T) operation on BST?
O(n)
O(log(n))
O(1)
16
BST - Search Operation Speed - Answer
What is the speed of the Search() operation on BST?
O(n)
O(log(n))
17
O(1)
19
25
34
17
BST - Remove
Remove x from BST
if node is null exit
17
if node is leaf remove
if node is non-leaf find replacement
9
19
3 cases (continues…)
6
18
25
18
BST – Remove (1)
1. Deleted has no right child
promote its left child
17
Example: Delete 9
9
6
19
18
25
19
BST – Remove (2)
2. Deleted right's child has no left child
promote right child
17
Example: Delete 19
9
6
19
25
18
null
20
BST – Remove (3)
3. Deleted right's child has left child
find min in deleted right's left
17
promote min
9
19
Example: Delete 17
6
18
25
21
Problem: BST Delete Min
Implement:
void DeleteMin()
Make sure the method works for:
empty tree
tree with one element
tree with two elements - root + left/right
tree with multiple elements
22
Solution: BST Delete Min
public void DeleteMin()
{
if (this.root == null) { return; }
Node<T> parent = null;
Node<T> current = this.root;
while (current.Left != null)
{
parent = current;
current = parent.Left;
}
if (parent == null) { this.root = current.Right; }
else { parent.Left = current.Right; }
}
23
Lab Exercise
BST - DeleteMin
Binary Search Trees – Operation Speed
Insert – height of tree
Search – height of tree
O(n)
Delete – height of tree
17
9
6
19
18
25
25
Binary Search Trees – Best Case
Example: Insert 17, 10, 25, 5, 15, 19, 34
17
10
5
25
15
19
34
26
Binary Search Trees – Average Case
You can insert values in ever random order
Example: Insert 17, 19, 9, 6, 25, 28, 18
17
9
6
19
18
25
28
27
Binary Search Trees – Worst Case
You can insert values in ever increasing/decreasing order
Example: Insert 17, 19, 25, 34
17
Linked List
19
25
34
28
Balanced Binary Search Trees
Binary search trees can be balanced
Balanced trees have for each node
Nearly equal number of nodes in its subtrees
Balanced trees have height of ~ log(n)
29
BST - Range
All elements between 2 values
10
Return enumerator with the elements
Find the
5
37
8
3
Range 4, 37
4 5 8 9 10 37
1
4
9
30
Lab Exercise
BST - Range
BST - Range Operation Speed - Quiz
TIME’S UP!
TIME:
What is the speed of the Range(T, T) operation on BST?
O(n)
O(log(n))
O(1)
32
BST - Range Operation Speed - Answer
What is the speed of the Range(T, T) operation on BST?
O(n)
O(log(n))
O(1)
33
Summary
Binary search trees are ordered binary trees
Balanced trees have roughly the same height
of their left and right children
34
Binary Search Trees
?
https://softuni.bg/opencourses/data-structures
License
This course (slides, examples, labs, videos, homework, etc.)
is licensed under the "Creative Commons AttributionNonCommercial-ShareAlike 4.0 International" license
Attribution: this work may contain portions from
"Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA license
"Data Structures and Algorithms" course by Telerik Academy under CC-BY-NC-SA license
36
Trainings @ Software University (SoftUni)
Software University – High-Quality Education,
Profession and Job for Software Developers
softuni.bg
Software University Foundation
softuni.org
Software University @ Facebook
facebook.com/SoftwareUniversity
Software University Forums
forum.softuni.bg
© Copyright 2026 Paperzz