Lecture 6 Trees Part2
Lecture 6 Trees Part2
and Algorithms
Trees – Part 2
2
List of Children Tree
Presentation
Node<Item>* root;
E F G
3
Priority Queue
➢ A priority queue stores a
collection of entries
➢ Each entry is a pair
(key, value)
➢ Main methods of the
Priority Queue ADT
❖ insert(k, x)
inserts an entry with key
k and value x
❖ removeMax()
removes and returns the
entry with smallest key
➢ Example:
4
Priority Queue
➢ Additional methods
❖ max()
returns, but does not remove, an entry with smallest key
❖ size(), isEmpty()
➢ Applications:
❖ Standby flyers
❖ Auctions
❖ Stock market
5
Heap tree
➢ Heap tree is a binary tree where the value of
any internal node is greater or equal to theirs
children
➢ Application: Build the priority queue
44
42 35
33 31 19 27
10 26 14
6
Heap tree
42 35
33 31 19 27
10 26 14
7
Heap tree insertion
Insert 70 95
61 83
53 39 72 16
24 48 70
8
Heap tree insertion
Insert 70 95
61 83
53 70 72 16
24 48 39
9
Heap tree insertion
Insert 70 95
70 83
53 61 72 16
24 48 39
10
Heap tree insertion
Algorithm insert(v):
➢ Step 1 − Create a new node at the end of
heap.
➢ Step 2 − Start the new node, compare the
value at this node to its parent. If it is larger
than its parent, swap them, move up and
continue Step 2.
11
Exercise 2
12
Remove max
Return value 95 95
70 83
53 61 72 16
24 48 39
13
Remove max
39
70 83
53 61 72 16
24 48
14
Heap tree deletion
83
70 39
53 61 72 16
24 48
15
Remove max
83
70 72
53 61 39 16
24 48
16
Remove max
44
42 35
33 31 19 27
10 26 14
18
Heap tree performance
• Max operation: O(1)
• Insertion operation: The height of the
tree
• Deletion operation: The height of the
tree
20
Binary Search Trees
➢ A binary search tree
is a binary tree such 6
that the value at the 2 9
parent node is larger
or equal to values of 1 4 8
the left child, and
smaller or equal to
values of the right
child.
➢ An inorder traversal
of a binary search
trees visits the keys 21
in increasing order
Search
➢ To search for a key k, Algorithm TreeSearch(k, v)
we trace a downward if T.isExternal (v)
path starting at the return v;
root if k • key(v)
➢ The next node return TreeSearch(k, T.left(v));
visited depends on else if k = key(v)
the outcome of the return v;
comparison of k with else { k • key(v); }
the key of the return TreeSearch(k, T.right(v));
current node
➢ If we reach a leaf, 6
the key is not found •
and we return null 2 9
•
➢ Example: find(4):
❖ Call TreeSearch(4,root) 1 4 = 8
22
Insertion
6
•
➢ Insert a value k into the 2 9
•
binary tree.
1 4 8
➢ Algorithm: Start from •
the root, compare k to
the value at this node. If w
k is smaller, insert k
into the left tree, 6
otherwise insert k into
2 9
the right tree.
➢ Example: insert 5 1 4 8
w
5
23
Deletion
6
•
➢ To perform operation 2 9
remove(k), we search for •
key k 1 4 v 8
➢ Assume key k is in the
5
tree, and let let v be the
node storing k
➢ If node v is a leaf,
remove v. If v has one 6
child, remove v and
2 9
connect its child to its
parent. 1 5 8
➢ Example: remove 4
24
Deletion (cont.)
➢ We consider the case
1
where the key k to be v
removed is stored at a 3
node v with two children: 2 8
❖ we find the node w that
6 9
follows v in an inorder
traversal (the most-left w
5
leaf of the right child of
v).
1
❖ we replace node v by w v
❖ we remove node w 5
➢ Example: remove 3 2 8
6 9
25
Performance
Operations: Searching,
insertion, deletion: The
height of the tree
27
Exercise 5
➢ Draw the BST for items with keys
EASYQUESTION
28