You are on page 1of 21

1

Lecture 11
Binary Search Tree
Sandy Ardianto & Erick Pranata
Sekolah Tinggi Teknik Surabaya
Structure
Terminology
Tree Variation
Action
Insert
Traverse
Search
Delete
2
Exercises
Sekolah Tinggi Teknik Surabaya
3

Sekolah Tinggi Teknik Surabaya


5

3 8

2 4 9
4

Sekolah Tinggi Teknik Surabaya


5

Sekolah Tinggi Teknik Surabaya


root
5

3 8
Subtree
2 4 9 leaf
6

Sekolah Tinggi Teknik Surabaya


7

Sekolah Tinggi Teknik Surabaya


Binary Search Tree
Ternary Search Tree
Red Black Tree
AVL Tree (Adelson-Velskii dan Landis)
etc

Sekolah Tinggi Teknik Surabaya


9

Sekolah Tinggi Teknik Surabaya


Insert
Traverse Show all elements
Search Find its element
Delete

10

Sekolah Tinggi Teknik Surabaya


11

Sekolah Tinggi Teknik Surabaya


If root is null
New node is root
Else
Find empty position
Locate New Node to there

In a binary tree, a small number will go


on the left, while a large number will be
entered in the right 12

Sekolah Tinggi Teknik Surabaya


13

Sekolah Tinggi Teknik Surabaya


Breadth First Search
Search Widens
Using Queue
Depth First Search
Search Depth
Do recursively
There are 3 types:
+ Pre-order traversal
+ In-order traversal
+ Post-order traversal 14

Sekolah Tinggi Teknik Surabaya


15

Sekolah Tinggi Teknik Surabaya


Done by utilizing the Traverse action.

16

Sekolah Tinggi Teknik Surabaya


17

Sekolah Tinggi Teknik Surabaya


Find the node to be deleted, reffered to as
del_node
If del_node dont have children, remove
immediately
But if del_node had one child, point the
pointer parent to a child of del_node
But if del_node had two children:
Option 1: find the rightmost child of the left
subtree, referred to as the replacer;
Option 2: find the leftmost child of the right
subtree, referred to as the replacer; 18
Replace del_node with replacer
Sekolah Tinggi Teknik Surabaya
19

Sekolah Tinggi Teknik Surabaya


Implement Binary Search Tree in C# .NET

20

Sekolah Tinggi Teknik Surabaya


Andrew Troelsen, Pro C# and The .Net
4.5 Framework (Sixth Edition), Apress,
2012
http://en.wikipedia.org/wiki/Binary_tre
e

21

Sekolah Tinggi Teknik Surabaya

You might also like