0% found this document useful (0 votes)
2 views

Lecture 6 Trees Part2

The document covers data structures and algorithms related to trees, specifically focusing on trees, priority queues, heap trees, and binary search trees. It explains the definitions, applications, and operations such as insertion, deletion, and searching for these structures, along with their performance characteristics. Exercises are included to reinforce understanding of constructing and manipulating these data structures.

Uploaded by

23020717
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 6 Trees Part2

The document covers data structures and algorithms related to trees, specifically focusing on trees, priority queues, heap trees, and binary search trees. It explains the definitions, applications, and operations such as insertion, deletion, and searching for these structures, along with their performance characteristics. Exercises are included to reinforce understanding of constructing and manipulating these data structures.

Uploaded by

23020717
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Data Structures

and Algorithms
Trees – Part 2

University of Technology and Engineering


Vietnam National University Hanoi
What is a Tree
➢ In computer science,
Manufacturing Computer Company
a tree is an abstract
model of a
hierarchical structure
Sale R&
➢ A tree consists of Manufacturing
s D
nodes with a parent-
child relation U Laptop Desktop
International
➢ Applications: S s s
❖ Organization charts
❖ File systems Europ Asi Canad
❖ Programming e a a
environments

2
List of Children Tree
Presentation

Template <class Item> root


class Node {
Item data;
A
List<Node*> children;
}
B C D

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

Max operation: get the node with maximum value


(the root)
44

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

➢ Construct a max heap tree including: 52, 69,


38, 79, 66, 64, 72, 3, 16, 89, 15, 37, 0, 28, 73,
95.
➢ Insert the following numbers into the above
max heap tree: 5, 3, 9, 7, 2, 4, 6, 1, 8.

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

Algorithm remove_max (T)


➢ Step 1 − Remove the root node.
➢ Step 2 − Move the last element of the heap to
the root
➢ Step 3 − Start from the root, compare the
value at the node to their children. If it is
smaller than the largest child, swap them,
move down and continue Step 2. 17
Exercise 3
Describe step by step of removing max from the
following max heap tree

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

The height of the tree:


❖ Worse case: O(n)
❖ Best case: O(log n) when the heap
tree is balanced

Balance the heap tree: If insertions and


deletions make the heap tree
unbalanced, perform balancing
operations to make balanced again. 19
Using heap tree library
Using make_heap, pop_heap, push_heap,
and sort_heap from the algorithm library
of C++ programming language to
implement the following tasks:
❖ Make a heap tree
❖ Remove the max from the heap tree
❖ Insert a node to the heap tree
❖ Sort elements in the heap 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

The height of the tree:


➢ Worse case: O(n)
➢ Best case: O(log n) when
the heap tree is
balanced

Balance the binary search


tree: If insertions and
deletions make the binary
search tree unbalanced,
perform balancing 26
operations to make it
Exercise 4

➢ Create a binary search tree from following


numbers: 34, 15, 65, 62, 69, 42, 40, 80, 50, 59,
23, 46, 57, 3, 29
➢ Draw BSTs after deleting keys 62, 42 and 3 from
the above tree.

27
Exercise 5
➢ Draw the BST for items with keys
EASYQUESTION

➢ Draw the BST for items with keys


DATASTRUCTRESANDALGORITHM

28

You might also like