+ All Categories
Home > Documents > CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms...

CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms...

Date post: 04-Jan-2016
Category:
Upload: barbra-barber
View: 220 times
Download: 0 times
Share this document with a friend
Popular Tags:
97
CSE221/ICT221 CSE221/ICT221 Analysis and Design of Analysis and Design of Algorithms Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof. Dr.Surasak Mungsing Asst.Prof. Dr.Surasak Mungsing E-mail: [email protected] 06/27/22 1
Transcript
Page 1: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

CSE221/ICT221CSE221/ICT221Analysis and Design of AlgorithmsAnalysis and Design of Algorithms

Analysis of Algorithm using Tree Data Structure

Asst.Prof. Dr.Surasak MungsingAsst.Prof. Dr.Surasak MungsingE-mail: [email protected]

04/20/23 1

Page 2: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 2

Trees

Page 3: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 3

Introduction to TreesIntroduction to Trees

General Trees Binary Trees Binary Search Trees AVL Trees

Page 4: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 4

Tree

Page 5: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 5

DefinitionDefinition

A tree t is a finite nonempty set of elements.

One of these elements is called the root. The remaining elements, if any, are

partitioned into trees, which are called the subtrees of t.

Page 6: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 6

Sub-trees

Page 7: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 7

Tree

Page 8: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 8

Level 3

Object

Number Throwable OutputStream

Integer Double Exception FileOutputStream

RuntimeException

Level 4

Level 2

Level 1

height = depth = number of levels

Page 9: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 9

Object

Number Throwable OutputStream

Integer Double Exception FileOutputStream

RuntimeException

2 1 1

0 0 1 0

0

Node Degree = Number Of Children

Page 10: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 10

Binary Tree

Page 11: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 11

Binary TreeBinary Tree

Finite (possibly empty) collection of elements.

A nonempty binary tree has a root element. The remaining elements (if any) are

partitioned into two binary trees. These are called the left and right subtrees

of the binary tree.

Page 12: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 12

Binary Tree

Page 13: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 13

A Tree vs A Binary TreeA Tree vs A Binary Tree

No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree.

A binary tree may be empty; a tree cannot be empty.

Page 14: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 14

A Tree vs A Binary TreeA Tree vs A Binary Tree

The subtrees of a binary tree are ordered; those of a tree are not ordered.

a

b

a

b

• Are different when viewed as binary trees.

• Are the same when viewed as trees.

Page 15: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 15

Forms of Binary Trees

Page 16: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 16

Complete Binary Trees

Page 17: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 17

Tree Traversal

Page 18: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 18

Processing and Walking OrderProcessing and Walking Order

Page 19: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 19

Depth First ProcessingDepth First Processing

Page 20: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 20

Preorder TraversalPreorder Traversal

Page 21: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 21

Breath First ProcessingBreath First Processing

Page 22: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 22

Height and number of nodesHeight and number of nodes

Maximum height of a binary tree Hmax = N

Minimum height of a binary treeHmin = logN + 1

Maximum and Minimum number of nodesNmin = H and Nmax = 2H - 1

Page 23: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 23

Page 24: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 24

Expression Tree

การประยุ�กต์ใช้� Tree

Page 25: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 25

Arithmetic ExpressionsArithmetic Expressions

Expressions comprise three kinds of entities. Operators (+, -, /, *). Operands (a, b, c, d, e, f, g, h, 3.25, (a + b), (c + d),

etc.). Delimiters ((, )).

(a + b) * (c + d) + e – f/g*h + 3.25

Page 26: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 26

Infix FormInfix Form

Normal way to write an expression. Binary operators come in between their

left and right operands. a * b a + b * c a * b / c (a + b) * (c + d) + e – f/g*h + 3.25

Page 27: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 27

Operator PrioritiesOperator Priorities

How do you figure out the operands of an operator? a + b * c a * b + c / d

This is done by assigning operator priorities. priority(*) = priority(/) > priority(+) = priority(-)

When an operand lies between two operators, the operand associates with the operator that has higher priority.

Page 28: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 28

Tie BreakerTie Breaker

When an operand lies between two operators that have the same priority, the operand associates with the operator on the left. a + b - c a * b / c / d

Page 29: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 29

DelimitersDelimiters

Subexpression within delimiters is treated as a single operand, independent from the remainder of the expression. (a + b) * (c – d) / (e – f)

Page 30: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 30

Infix Expression Is Hard To ParseInfix Expression Is Hard To Parse

Need operator priorities, tie breaker, and delimiters.

This makes computer evaluation more difficult than is necessary.

Postfix and prefix expression forms do not rely on operator priorities, a tie breaker, or delimiters.

So it is easier for a computer to evaluate expressions that are in these forms.

Page 31: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 31

Postfix FormPostfix Form The postfix form of a variable or

constant is the same as its infix form. a, b, 3.25

The relative order of operands is the same in infix and postfix forms.

Operators come immediately after the postfix form of their operands. Infix = a + b Postfix = ab+

Page 32: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 32

Postfix ExamplesPostfix Examples

Infix = a + b * c Postfix =ab c * +

• Infix = a * b + c Postfix = a b * c +

• Infix = (a + b) * (c – d) / (e + f) Postfix = a b + c d - * e f + /

Page 33: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 33

Expression TreeExpression Tree

Page 34: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 34

Expression TreeExpression Tree

Page 35: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 35

Binary Tree FormBinary Tree Form a + b +

a b

• - a -

a

Page 36: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 36

Binary Tree FormBinary Tree Form

(a + b) * (c – d) / (e + f)

/

+

a b

-

c d

+

e f

*

/

Page 37: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 37

Expression Tree

Infix Expression =?

Page 38: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 38

Constructing an Expression TreeConstructing an Expression Treea b + c d * -

a bc da b

+

a b

+

c d

*

a b

+

c d

*

-

(a) (b)

(c) (d)

Page 39: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 39

Binary Search Trees

การประยุ�กต์ใช้� Tree

Page 40: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 40

Figure 8-1

Binary Search Tree

Page 41: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 41

Binary Search TreesBinary Search Trees

Page 42: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 42

Are these Binary Search Trees?

Page 43: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 43

Construct a Binary Search TreeConstruct a Binary Search Tree

เวลาที่��ใช้�ในการค้�นหาข้�อมู�ล Worst case?Average case?

Page 44: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 44

Page 45: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 45

AVL Trees

Balance Binary Search Tree

Page 46: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 46

AVL TreesAVL Trees

Balanced binary tree structure, named after Adelson, Velski, and Landis

An AVL tree is a height balanced binary search tree.

|HL – HR| <= 1

where HL is the height of the left subtree and HR is the height of the left subtree

Page 47: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 47

Binary Search TreesBinary Search Trees

(b) AVL Tree

(a) An unbalanced BST

Page 48: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 48

Out of BalanceOut of Balance

Four cases of out of balance:

left of left (LL) - requires single rotation right of right (RR) - requires single rotation

Left of right (LR) - requires double rotation Right of left (RL) - requires double rotation

Page 49: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 49

Out of Balance (left of left)Out of Balance (left of left)

Page 50: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 50

Out of Balance (left of left)Out of Balance (left of left)

Page 51: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 51

Out of Balance (right of right)Out of Balance (right of right)

Page 52: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 52

Out of Balance (right of right)Out of Balance (right of right)

Page 53: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 53

Simple double rotation rightSimple double rotation right

Page 54: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 54

Complex double rotation rightComplex double rotation right

Page 55: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 55

Insert a node to AVL treeInsert a node to AVL tree

Page 56: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 56

Balancing BSTBalancing BST

Page 57: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 57

Deleting a node from AVL treeDeleting a node from AVL tree

Page 58: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 58

เวลาที่��ใช้�ในการค้�นหาข้�อมู�ลใน AVL Tree

Worst case?Average case?

Balance Binary Search Tree

Page 59: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 59

Page 60: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Priority QueuePriority Queue

04/20/23 60

• Collection of elements.• Each element has a priority or key.• Supports following operations:

isEmpty size add/put an element into the priority queue get element with min/max priority remove element with min/max priority

Page 61: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Min Tree ExampleMin Tree Example

04/20/2361

2

4 9 3

4 8 7

9 9

Root is the minimum element

Page 62: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Max Tree ExampleMax Tree Example

04/20/2362

9

4 9 8

4 2 7

3 1

Root is the maximum element

Page 63: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Min Heap DefinitionMin Heap Definition

• complete binary tree• min tree

04/20/2363

Complete binary tree with 9 nodes that is also a min tree.

2

4

6 7 9 3

8 6

3

Page 64: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Max Heap With 9 NodesMax Heap With 9 Nodes

04/20/2364

9

8

6 7 2 6

5 1

7

Complete binary tree with 9 nodes that is also a max tree.

Page 65: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Heap HeightHeap Height

What is the height of an n node heap ?

04/20/23 65

Since a heap is a complete binary tree, the height of an n node heap is log2 (n+1).

Page 66: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

9 8 7 6 7 2 6 5 1

1 2 3 4 5 6 7 8 9 100

A Heap Is Efficiently Represented As An Array

9

8

6 7 2 6

5 1

7

Page 67: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Moving Up And Down A Heap

9

8

6 7 2 6

5 1

7

1

2 3

4 5 6 7

8 9

Page 68: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Putting An Element Into A Max Heap

Complete binary tree with 10 nodes.

9

8

6 7 2 6

5 1

7

7

Page 69: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Putting An Element Into A Max Heap

New element is 5.

9

8

6 7 2 6

5 1

7

75

Page 70: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Putting An Element Into A Max Heap

New element is 20.

9

8

6

7

2 6

5 1

7

7

7

Page 71: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Putting An Element Into A Max Heap

New element is 20.

9

8

6

7

2 6

5 1

7

77

Page 72: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Putting An Element Into A Max Heap

New element is 20.

9

86

7

2 6

5 1

7

77

Page 73: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Putting An Element Into A Max Heap

New element is 20.

9

86

7

2 6

5 1

7

77

20

Page 74: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Putting An Element Into A Max Heap

Complete binary tree with 11 nodes.

9

86

7

2 6

5 1

7

77

20

Page 75: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Putting An Element Into A Max Heap

New element is 15.

9

86

7

2 6

5 1

7

77

20

Page 76: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Putting An Element Into A Max Heap

New element is 15.

9

8

6

7

2 6

5 1

7

77

20

8

Page 77: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Putting An Element Into A Max Heap

New element is 15.

8

6

7

2 6

5 1

7

77

20

8

9

15

Page 78: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Complexity Of Put

Complexity is O(log n), where n is heap size.

8

6

7

2 6

5 1

7

77

20

8

9

15

Page 79: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Removing The Max Element

Max element is in the root.

8

6

7

2 6

5 1

7

77

20

8

9

15

Page 80: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Removing The Max Element

After max element is removed.

8

6

7

2 6

5 1

7

77 8

9

15

Page 81: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Removing The Max Element

Heap with 10 nodes.

8

6

7

2 6

5 1

7

77 8

9

15

Reinsert 8 into the heap.

Page 82: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Removing The Max Element

Reinsert 8 into the heap.

6

7

2 6

5 1

7

77

9

15

Page 83: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Removing The Max Element

Reinsert 8 into the heap.

6

7

2 6

5 1

7

77

9

15

Page 84: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Removing The Max Element

Reinsert 8 into the heap.

6

7

2 6

5 1

7

77

9

15

8

Page 85: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Removing The Max Element

Max element is 15.

6

7

2 6

5 1

7

77

9

15

8

Page 86: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Removing The Max Element

After max element is removed.

6

7

2 6

5 1

7

77

9

8

Page 87: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Removing The Max Element

Heap with 9 nodes.

6

7

2 6

5 1

7

77

9

8

Page 88: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Removing The Max Element

Reinsert 7.

6 2 6

5 1

79

8

Page 89: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Removing The Max Element

Reinsert 7.

6 2 6

5 1

7

9

8

Page 90: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Removing The Max Element

Reinsert 7.

6 2 6

5 1

7

9

8

7

Page 91: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Complexity Of Remove Max Element

Complexity is O(log n).

6 2 6

5 1

7

9

8

7

Page 92: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Complexity of Operations

Two good implementations are heaps and leftist trees.

isEmpty, size, and get => O(1) time

put and remove => O(log n) time where n is the size of the priority queue

Page 93: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 93

Practical ComplexitiesPractical Complexities

109 instructions/second

n n nlogn n2 n3

1000 1mic 10mic 1milli 1sec

10000 10mic 130mic 100milli 17min

106 1milli 20milli 17min 32years

Page 94: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 94

Impractical ComplexitiesImpractical Complexities109 instructions/second

n n4 n10 2n

1000 17min 3.2 x 1013 years

3.2 x 10283 years

10000

116 days

??? ???

106 3 x 107 years

?????? ??????

Page 95: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 95

Summary

n Insertion Sort

O(n2)

Shellsort

O(n7/6)

Heapsort

O(n log n)

Quicksort

O(n log n)

10 0.00044 0.00041 0.00057 0.00052

100 0.00675 0.00171 0.00420 0.00284

1000 0.59564 0.02927 0.05565 0.03153

10000 58864 0.42998 0.71650 0.36765

100000 NA 5.7298 8.8591 4.2298

1000000 NA 71.164 104.68 47.065

Page 96: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

04/20/23 96

Faster Computer Vs Better Faster Computer Vs Better AlgorithmAlgorithm

Algorithmic improvement more usefulthan hardware improvement.

E.g. 2n to n3

Page 97: CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Apr 20, 2023 97


Recommended