cs212: data structures

19
CS212: DATA STRUCTURES Trees Exercise Computer Science Department

Upload: sally

Post on 29-Jan-2016

31 views

Category:

Documents


0 download

DESCRIPTION

Cs212: Data Structures. Trees Exercise. Trees, Binary Trees, and Binary Search Trees. Answer the following questions for the tree shown below.  What is the path length of the path from node 20 to node 12?. Trees, Binary Trees, and Binary Search Trees. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Cs212: Data Structures

CS212: DATA STRUCTURES

Trees Exercise Computer Science

Department

Page 2: Cs212: Data Structures

Trees, Binary Trees, and Binary Search Trees

Answer the following questions for the tree shown below. 

What is the path length of the path from node 20 to node 12?

 

Page 3: Cs212: Data Structures

Trees, Binary Trees, and Binary Search Trees

 What is the path length of the path from node 20 to node 12? 3

 

Page 4: Cs212: Data Structures

Trees, Binary Trees, and Binary Search Trees

What is the height of the tree?

 

Page 5: Cs212: Data Structures

Trees, Binary Trees, and Binary Search Trees

What is the height of the tree? 4

 

Page 6: Cs212: Data Structures

Trees, Binary Trees, and Binary Search Trees

Show what would this tree look like after: Adding 18

 

Page 7: Cs212: Data Structures

Trees, Binary Trees, and Binary Search Trees

Show what would this tree look like after: Adding 18

 

18

18>16

18<20

Page 8: Cs212: Data Structures

Trees, Binary Trees, and Binary Search Trees

Show what would this tree look like after: Delete 35

 

18

Page 9: Cs212: Data Structures

Trees, Binary Trees, and Binary Search Trees

Show what would this tree look like after: Delete 35

 

18

Page 10: Cs212: Data Structures

Trees, Binary Trees, and Binary Search Trees

Show what would this tree look like after: Delete 16

 

18

Page 11: Cs212: Data Structures

Trees, Binary Trees, and Binary Search Trees

Show what would this tree look like after: Delete 16 , After delete node 16 we replace it by

predecessor(largest node of left sub tree) .

 

18

12

Page 12: Cs212: Data Structures

Trees, Binary Trees, and Binary Search Trees

Show what would this tree look like after: Delete 16 , After delete node 16 we

replace it by successor(smallest node of right sub tree) .

 18

Page 13: Cs212: Data Structures

Trees, Binary Trees, and Binary Search Trees

Show what would this tree look like after: Delete 20

 

18

Page 14: Cs212: Data Structures

Trees, Binary Trees, and Binary Search Trees

Show what would this tree look like after: Delete 20, After delete node 20 we replace it by

predecessor(largest node of left sub tree) .

 

18

Page 15: Cs212: Data Structures

Trees, Binary Trees, and Binary Search Trees

Show what would this tree look like after: Delete 20,After delete node 20 we replace it by

successor(smallest node of right sub tree) .

 

Page 16: Cs212: Data Structures

Trees, Binary Trees, and Binary Search Trees

EX2:Traverse the tree shown below in: Preorder travers VLR:

In order travers LVR:

Post order travers LRV:

Page 17: Cs212: Data Structures

Trees, Binary Trees, and Binary Search Trees

EX2:Traverse the tree shown below in: Preorder travers VLR: 20,16,11,10,12,43,35,48,45,65

In order travers LVR: 10,11,12,16,20,35,43,45,48,65.

Post order travers LRV: 10,12,11,16,35,45,65,48,43,20

Page 18: Cs212: Data Structures

AVL Tree

EX3: test if tree is AVL tree or not, if not convert it to AVL tree.

20

10

5

1

30

Page 19: Cs212: Data Structures

AVL Tree

EX3: test if tree is AVL tree or not, if not convert it to AVL tree. Not AVL tree because node 10 out of balance.

20

10

5

1

30

EH

LH

LH

LH

EH

Out of balance

20

5 30

1

EH

EH

EH

EH

LH

Left to left imbalance case

AVL tree

10