Sei sulla pagina 1di 62

Informatics Engineering

University Muhammadiyah of Malang


2018

Operation at
Binary Search Tree
Didih Rizki Chandranegara
didihrizki@umm.ac.id

4/6/2019 Binary Search Tree 1


Introduction
 A Binary Search Tree (BST) is a binary tree with the following
properties: (Given a node x in the tree)
 if y is a node in the left subtree of x, then y-
>key ≤ x > key or all values in the node’s left subtree are
less than node value, where Key, which is key stored at the
node
 if y is a node in the right subtree of x, then x->key
≤ y->key or all values in the node’s right subtree are greater
than node value
4/6/2019 Binary Search Tree 2
Illustration

4/6/2019 Binary Search Tree 3


Illustration
< >

4/6/2019 Binary Search Tree 4


Illustration

4/6/2019 Binary Search Tree 5


Implementation
class node {
int key;
node *left, *right, *parent;
};

key

4/6/2019 Binary Search Tree 6


Binary Search Tree
Given a binary search tree, there are several operations we
want to perform
 Insert an element
 Delete an element
 Search for an element
 Find the minimum/maximum element
 Find the successor/predecessor of a node
4/6/2019 Binary Search Tree 7
Binary Search Tree
 Once we see how these are done, it will be apparent that
the complexity of each of these is Θ(h), where h is the
height of the tree.
 The insert and delete operations are the hardest to
implement
 Finding the minimum/maximum and searching are the
easiest

4/6/2019 Binary Search Tree 8


Traversal BST
 By “traversal” we mean visiting all the nodes in a
graph
 Traversal strategies can be specified by the ordering
of the three objects to visit: the current node, the
left subtree, and the right subtree.
 We assume the the left subtree always comes
before the right subtree.
4/6/2019 Binary Search Tree 9
Traversal BST
Then there are three strategies
1. Inorder. The ordering is: the left subtree, the
current node, the right subtree
2. Preorder. The ordering is: the current node, the left
subtree, the right subtree
3. Postorder. The ordering is: the left subtree, the
right subtree, the current node
4/6/2019 Binary Search Tree 10
Traversal BST

4/6/2019 Binary Search Tree 11


Preorder
1. Visit the root.
2. Traverse the left subtree.
3. Traverse the right subtree.

4/6/2019 Binary Search Tree 12


Preorder
1. Visit the root.
2. Traverse the left subtree.
3. Traverse the right subtree.

F, B, A, D, C, E, G, I, H

4/6/2019 Binary Search Tree 13


Inorder
1. Traverse the left subtree.
2. Visit the root.
3. Traverse the right subtree.

4/6/2019 Binary Search Tree 14


Inorder
1. Traverse the left subtree.
2. Visit the root.
3. Traverse the right subtree.

A, B, C, D, E, F, G, H, I

4/6/2019 Binary Search Tree 15


Postorder
1. Traverse the left subtree.
2. Traverse the right subtree.
3. Visit the root.

4/6/2019 Binary Search Tree 16


Postorder
1. Traverse the left subtree.
2. Traverse the right subtree.
3. Visit the root.

A, C, E, D, B, H, I, G, F

4/6/2019 Binary Search Tree 17


Traversal BST

4/6/2019 Binary Search Tree 18


Traversal BST

4/6/2019 Binary Search Tree 19


Informatics Engineering
University Muhammadiyah of Malang
2018

Minimum/Maximum

4/6/2019 Binary Search Tree 20


Minimum/Maximum
 To find the minimum identify the leftmost node, i.e. the
farthest node you can reach by following only left branches
==> The minimum element is the left-most node
 To find the maximum identify the rightmost node, i.e. the
farthest node you can reach by following only right
branches ==> The maximum element is the right-most
node

4/6/2019 Binary Search Tree 21


Minimum/Maximum

4/6/2019 Binary Search Tree 22


Minimum/Maximum

4/6/2019 Binary Search Tree 23


Informatics Engineering
University Muhammadiyah of Malang
2018

Searching

4/6/2019 Binary Search Tree 24


Searching
Searching for a key
 We assume that a key and the subtree in which the key is
searched for are given as an input
 Suppose we are at a node. If the node has the key that is being
searched for, then the search is over. Otherwise, the key at the
current node is either strictly smaller than the key that is
searched for or strictly greater than the key that is searched for.

4/6/2019 Binary Search Tree 25


Searching
Searching for a key
 If the former is the case, then by the BST property, all the
keys in the left subtree are strictly less than the key that is
searched for.That means that we do not need to search in
the left subtree.
 Thus, we will examine only the right subtree. If the latter is
the case, by symmetry we will examine only the right
subtree
4/6/2019 Binary Search Tree 26
Searching
Algorithm
 Here k is the key that is searched for and x is the start node

4/6/2019 Binary Search Tree 27


Searching

4/6/2019 Binary Search Tree 28


Informatics Engineering
University Muhammadiyah of Malang
2018

Successor/Predecessor

4/6/2019 Binary Search Tree 29


Successor/Predecessor

 The successor of a key k in a search tree is the smallest key that


belongs to the tree and that is strictly greater than k.
 The predecessor of a key k in a search tree is the largest key that
belongs to the tree and that is strictly less than k.
 Finding the Successor/Predecessor of a node is a little harder.
 The predecessor operation is symmetric to successor.

4/6/2019 Binary Search Tree 30


Successor/Predecessor

 The idea for finding the successor of a given node x.


 If x has the right child, then the successor is the
minimum in the right subtree of x.
 Otherwise, the successor is the parent of the farthest
node that can be reached from x by following only right
branches backward.

4/6/2019 Binary Search Tree 31


Successor/Predecessor

 The predecessor can be found similarly with the roles of


left and right exchanged and with the roles of maximum
and minimum exchanged.

4/6/2019 Binary Search Tree 32


Successor Argument

 So, why is it that if x has an empty right subtree, then y is


the lowest ancestor of x whose left child is also an ancestor
of x?
 Let’s look at it the other way
 Let y be the lowest ancestor of x whose left child is also an
ancestor of x
 What is the predecessor of y?
4/6/2019 Binary Search Tree 33
Successor Argument

 Since y has a left child, it must be the largest element in the tree
rooted at y->left
 If x is not the largest element in the subtree rooted at y->left,
then some ancestor of x (in the subtree) is the left child of its
parent
 But y, which is not in this subtree, is the lowest such node
 Thus x is the predecessor of y, and y is the successor of x

4/6/2019 Binary Search Tree 34


Successor Example

4/6/2019 Binary Search Tree 35


Successor

4/6/2019 Binary Search Tree 36


Informatics Engineering
University Muhammadiyah of Malang
2018

Insertion

4/6/2019 Binary Search Tree 37


Insertion
 Suppose that we need to insert a node z such that
k=key[z]. To insert a node z into a binary tree,
we search the tree until we find a node whose
appropriate child is NULL. We insert the new node
there. And T is the tree
 When inserting a new element x to the tree, it is
inserted as a leaf

4/6/2019 Binary Search Tree 38


Insertion

4/6/2019 Binary Search Tree 39


Insertion

4/6/2019 Binary Search Tree 40


Insertion

4/6/2019 Binary Search Tree 41


Insertion

4/6/2019 Binary Search Tree 42


Insertion

4/6/2019 Binary Search Tree 43


Insertion
Insert(T,z) {
node *y=NULL;
node *x=T.root;
while(x != NULL) {
y = x;
if(z->key < x->key)
x = x->left;
or else
x = x->right;
}
z->parent = y;
if(y == NULL)
T.root = z;
else if(z->key < y->key)
y->left = z;
else
y->right = z;
4/6/2019 Binary Search Tree
} 44
Insertion Example

4/6/2019 Binary Search Tree 45


Informatics Engineering
University Muhammadiyah of Malang
2018

Deletion

4/6/2019 Binary Search Tree 46


Deletion
 Deleting a node z is by far the most difficult operation
 There are 3 cases to consider
1. If z has no children, just delete it with replace with NULL
2. If z has one child, splice out z. That is, link z’s parent and child
3. If z has two children, splice out z’s successor y, and replace the
contents of z with the contents of y

4/6/2019 Binary Search Tree 47


Deletion
 The last case works because if z has 2 children, then its
successor has no left child. Why?
 Deletion is made worse by the fact that we have to worry
about boundary conditions
 To simplify things, we will first define a function called
SpliceOut

4/6/2019 Binary Search Tree 48


Splice Out
 Any node with ≤ 1 child can be
“spliced out”
 Splicing out a node involves
linking the parent and child of a
node. The algorithm:

4/6/2019 Binary Search Tree 49


Splice Out Example

4/6/2019 Binary Search Tree 50


Deletion Algorithm
 Once we have defined the function SpliceOut,
deletion looks simple
 Here is the algorithm to delete z from tree T

4/6/2019 Binary Search Tree 51


Deletion Example

4/6/2019 Binary Search Tree 52


Informatics Engineering
University Muhammadiyah of Malang
2018

Time Complexity

4/6/2019 Binary Search Tree 53


Time Complexity
Best Case
 All leaves have depths within 1: depth ⎣lgn⎦(lg: base-two
logarithm) n = #items
Can achieve if tree is
static (insertion order
chosen by implementation,
no deletions)

4/6/2019 Binary Search Tree 54


Time Complexity
Average Case
 Starting with an empty tree, if n items are inserted in
random order, expected tree depth (access time) is
O(logn)

4/6/2019 Binary Search Tree 55


Time Complexity
Worst Case
 Natural but bad insertion order: sorted.
Insert A, B, C, D, E, F, G,...

4/6/2019 Binary Search Tree 56


Time Complexity
 We stated earlier, and have now seen, that all of the BST
operations have time complexity Θ(h), where h is the
height of the tree
 However, in the worst case, the height of a BST is Ω(n),
where n is the number of nodes
 In this case, the BST has gained us nothing

4/6/2019 Binary Search Tree 57


Time Complexity
 To prevent this worst-case behavior, we need to develop a
method which ensures that the height of a BST is kept to a
minimum
 Red-Black Trees are binary search trees which have height
Θ(log n)

4/6/2019 Binary Search Tree 58


Informatics Engineering
University Muhammadiyah of Malang
2018

Any Question???

4/6/2019 Binary Search Tree 59


Attention
Progress First Design Team Testing Team
Analysis Team Implementation Final
Team Progress

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Today Progress Final


Final Project Test

4/6/2019 Binary Search Tree 60


Attention (week 10)
 Minggu depan siapkan ide untuk Tugas
Besar
 Presentasikan di depan kelas
 Presentasi dilakukan diakhir materi

4/6/2019 Binary Search Tree 61


4/6/2019 Binary Search Tree 62

Potrebbero piacerti anche