Professional Documents
Culture Documents
LECTURER:
DIDAR YEDILKHAN
D.YEDILKHAN@ASTANAIT.EDU.KZ
LESSON AGENDA
1. Binary search trees theory - Basics
2. Binary search trees theory – Search, insert
3. Binary search trees theory - Delete
4. Binary search trees theory – In-order traversal
5. Binary search trees theory – Running times
6. Binary search trees implementation – Node class
7. Binary search trees implementation - Insertion
8. Binary search trees implementation – Traverse, min, max
9. Binary search trees implementation – Testing
10. Binary search trees implementation – Practical (real-world)
applications of trees
LESSON OUTCOMES
By the end of this lesson, you will be able to understand:
1. Basics of binary search trees including operations with binary search
trees (search, insert, delete, traverse)
2. Running time (complexity) of binary search trees
3. Implementation of binary search trees
4. Real-world (practical) applications of binary search trees
INTRODUCTION TO BST
In computer science, binary search trees, or simply BST (sometimes
also called ordered or sorted binary trees), is a note-based data
structure which has following properties: The left subtree of a node
contains only nodes with keys lesser than the node’s key, and the right
subtree of a node contains only nodes with keys greater than the node’s
key.
Binary search trees are going to make all of these operations quite fast
with only O(logN) time complexity !!!
LOG N - EXPLAINED
Imagine a classroom of 100 students in which you gave your pen to one
person. Now, you want that pen.
1. You go and ask the first person of the class, if he has the pen. Also,
you ask this person about other 99 people if they have, This is what
we call O(N^2).
2. Going and asking each of them individually is O(N).
3. Now, divide the class in two groups. Ask which group has the pen,
let it be A. Now, divide this group A further in two parts B and C.
Again ask these two groups which have the pen. Take the group (B
or C which has the pen) and further divide it. Repeat the process till
you are left with 1 student who has you pen.
This is what you mean by O(logN).
LOG N - EXPLAINED
LOG N - EXPLAINED
https://www.youtube.com/watch?v=iP897Z5Nerk – Binary search
with Flamengo dance
https://hackernoon.com/what-does-the-time-complexity-o-log-n-act
ually-mean-45f94bb5bfbf
- What does the time complexity O(log n) actually mean?
OPERATIONS WITH BST: INSERT
We start at the root node. If the data we want to insert is greater than the
root node we go to the right, if it is smaller, we go to the left.
1. binarytree.insert (12)
2. binarytree.insert (4)
3. binarytree.insert (5)
4. binarytree.insert (20)
5. binarytree.insert (1)
OPERATIONS: INSERT INTO BST
We start at the root node. If the data we want to insert is greater than the
root node we go to the right, if it is smaller, we go to the left.
1. binarytree.insert (12)
2. binarytree.insert (4)
3. binarytree.insert (5)
4. binarytree.insert (20)
5. binarytree.insert (1)
OPERATIONS WITH BST: SEARCH
We start at the root node. If the data we want to insert is greater than the
root node we go to the right, if it is smaller, we go to the left.
binarytree.search (5)
If we want to find the biggest node: we just have to go to the right as far
as possible -> it will be the greatest one!!!
OPERATIONS WITH BST: DELETE
Easiest way to perform deletion is “soft delete” -> we do not remove the
node from the BST, we just mark that it has been removed – not very
efficient.
binarytree.remove (5)
binarytree.remove (1)
Complexity: we have to find the item we want to get rid of and we have
to update the references – set parent’s pointer point to it’s grandchild
directly. O(logN) find operation + O(1) update references = O(logN)
OPERATIONS WITH BST: DELETE
3. The node we want to get rid of has 2 children
In order traversal: 1 -> 10 -> 16 -> 19 -> 23 -> 32 -> 55 -> 79 --- it can
be seen that the order is numerical
OPERATIONS WITH BST: TRAVERSE
b. Pre-order traversal – we visit the root + left subtree + the right
subtree recursively
OPERATIONS WITH BST: TRAVERSE
b. Pre-order traversal – we visit the root + left subtree + the right
subtree recursively
Pre-order traversal: 32 -> 10 -> 1 -> 19 -> 16 -> 23 -> 55 -> 79 ---
OPERATIONS WITH BST: TRAVERSE
c. Post-order traversal – we visit the left subtree + the right subtree +
the root recursively
OPERATIONS WITH BST: TRAVERSE
c. Post-order traversal – we visit the left subtree + the right subtree +
the root recursively