Week 10 - Binary Search Tree
Week 10 - Binary Search Tree
Week 10
Program Studi Teknik Informatika
Fakultas Teknik – Universitas Surabaya
Background
• In all previous cases we consider the best data structure
to be used for linear fashion (like array, list, linked list, etc)
• However there are some problems that can be solved
better using data structure that is represented in a non-
linear fashion.
• This data structure is called: Tree.
Representing Directory Structure
Tree
• By looking at the graph, designed to represent the
problem/solution, we can see that all graphs resemble a
tree (but an upside down tree)
• This tree has a root at the top and it grows its branches
and leaves downward
• The Tree data type has a representation that is similar to
the graph of the problem/solution.
• Therefore you can guess that the Tree data type will fit
perfectly to be used as the data structure for the problems.
Tree
• Tree: A node can have any child/children
Binary Tree
• Binary Tree: a specific type of Tree, where each node
can have at most 2 children
Binary Search Tree
• Binary SearchTree: a specific type of Binary Tree,
where:
– all nodes on the left of the parent node will have value that is
less than its parent, and
– all nodes on the right of the parent node will have value that
is greater than its parent.
Because the concept of Tree and
Binary Tree is the same, then in this
course we will use Binary Tree (more
specifically: Binary Search Tree) as
the case of study.
Binary Search Tree
• Before we go deeper to the BST, you have to know some
terms related to a general Tree (see also the next slide)
Binary Search Tree
• Binary Search Tree: is a data structure that is made up
of nodes where each node contains:
– Value, and
– Left and Right pointers that may contain null or another
node.
• Will be demonstrated in
class
Exercise 1: Add data to BST
• Draw a Binary Search Tree that will be created for the
following data:
10, 12, 5, 8, 9, 3, 6, 16, 15, 20
• Do it in 10 minutes.
BST: Data Search
• Searching a certain data from a
Binary Search Tree can be
performed in similar way to the
data addition
• Can you write the comparison
that are performed to find data
with value 4 from a binary
search tree shown here?
• Do the same thing to find data
with value 14
BST: Data Display
• Displaying data in BST can be performed by traversing all nodes in
it.
• Several traversal methods available are:
– Preorder (visit Node, then visit Left and Right subtree)
– Inorder (visit Left subtree, Node, then Right subtree)
– PostOrder (visit Left and Right subtree, then Node)
• See demonstration in class, about how to traverse the BST in the
previous slide using Preorder, Inorder, and Postorder.
BST: Data Display
• Traversing the BST shown in this
slide will give you:
– Preorder: 7, 3, 1, 5, 4, 11, 10, 15
– Inorder: 1, 3, 4, 5, 7, 10, 11, 15
– PostOrder: 1, 4, 5, 3, 10, 15, 11, 7
• Do you see something special in
the Inorder result?
Exercise 2: Data Display
• Based on your answer from Exercise 1, display the BST
in Preorder, Inorder, and Postorder.
• Do it in 10 minutes.
BST: Data Deletion
• Deleting a node in BST should not break the rules used to
build the BST
• Therefore, deleting a node in BST can be performed by
considering the following conditions:
– If the node has no child
– If the node has one child
– If the node has two children
Deleting node with no child
• To delete a node with no child (leaf
node), just cut the leaf from its parent
(in C#, it can be performed by giving
null to its parent’s left or right pointer)
• Example: deleting 1, can be performed
by giving null to the 3’s left pointer
• Deleting 15 can be performed by giving
null to the 11’s right pointer
Deleting node with 1 child
• To delete a node with 1 child, just
continue the node’s parent pointer to
the node’s child pointer
• Example: deleting 5, can be performed
by continuing the 3’s right pointer to 4.
• Note: 3 is the parent of 5, and 4 is the
child of 5
Deleting node with 2 children
• Steps to delete node (say node d) with 2 children:
1. Find the greatest value from the d’s left sub-tree (or you can
find the smallest value from the d’s right sub-tree)
2. Replace the value in d to this value (greatest or smallest)
3. Delete the node that contains the greatest value from the d’s left
sub-tree (or the smallest value from the d’s right sub-tree,
dependent to the value you choose in the first step)
Deleting node with 2 children
• Example: suppose we want to delete 7
• The greatest value from the 7’s left
sub-tree is 5
• Replace 7 with 5
• Delete node that contains 5 in the left
sub-tree of 7 (be careful: the node
contains value 7 has been changed to 5)
Exercise 3: Data Deletion
• Based on your answer from Exercise 1, draw the last
BST condition after the following deletion: (for each
point, start the BST from its original condition)
– Delete 9
– Delete 12
– Delete 5
• Do it in 15 minutes.
ANY QUESTION ??