Vous êtes sur la page 1sur 54

Session: Jan June 2013

UNIT 3 & 5 Trees & Search Trees

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

Session: Jan June 2013

Agenda
1) Introduction & Basic Terminologies 2) Complete & Extended Binary tree 3) Tree Traversal Algorithms 4) BST and primitive operations, 5) Threaded Binary tree and traversal 6) Huffman Algorithm 7) Heap sort 8) AVL tree 9) Introduction to m-way Search Trees, B Trees & B+ trees 10) Storage Management: Garbage Collection
Prof. (Dr) N Guruprasad EEC 012 - DATA STRUCTURES TREES 2

Session: Jan June 2013

Introduction & definition


One of the most important data structures in CS. A tree consists of finite set of elements, called nodes, and a finite set of directed lines called branches, that connect the nodes. Non linear data structure capable of expressing more complex relationship than that of physical adjacency.

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

Session: Jan June 2013

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

Session: Jan June 2013

Applications
1) Manipulate hierarchical data. 2) Make information easy to search (traversal). 3) Manipulate sorted lists of data. 4) As a workflow for composing digital images for visual effects. 5) Router algorithms.

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

Session: Jan June 2013

Basic Tree Terminologies


Degree The number of branches associated with a node is the degree of the node. Edge Path Branch Terminal node Level number Height / depth

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

Session: Jan June 2013

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

Session: Jan June 2013

Basic Tree Terminologies


Internal node node that is not a root or a leaf is known as an internal node. When the branch is directed toward the node, it is indegree branch. When the branch is directed away from the node, it is an outdegree branch. The sum of the indegree and outdegree branches is the degree of the node. If the tree is not empty, the first node is called the root.

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

Session: Jan June 2013

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

Session: Jan June 2013

Binary Trees
A binary tree is a tree in which no node can have more than two subtrees; the maximum outdegree for a node is two. In other words, a node can have zero, one, or two subtrees. These subtrees are designated as the left subtree and the right subtree.

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

10

Session: Jan June 2013

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

11

Session: Jan June 2013

A null tree is a tree with no nodes

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

12

Session: Jan June 2013

Some Properties of Binary Trees


The height of binary trees can be mathematically predicted Given that we need to store N nodes in a binary tree, the maximum height is

H max N
A tree with a maximum height is rare. It occurs when all of the nodes in the entire tree have only one successor.

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

13

Session: Jan June 2013

Some Properties of Binary Trees The minimum height of a binary tree is determined as follows:

Hmin log2 N 1
For instance, if there are three nodes to be stored in the binary tree (N=3) then Hmin=2.

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

14

Session: Jan June 2013

Some Properties of Binary Trees


Given a height of the binary tree, H, the minimum number of nodes in the tree is given as follows:

N min H

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

15

Session: Jan June 2013

Some Properties of Binary Trees


The formula for the maximum number of nodes is derived from the fact that each node can have only two descendents. Given a height of the binary tree, H, the maximum number of nodes in the tree is given as follows:

N max 2 1
H

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

16

Session: Jan June 2013

Some Properties of Binary Trees


The children of any node in a tree can be accessed by following only one branch path, the one that leads to the desired node. The nodes at level 1, which are children of the root, can be accessed by following only one branch; the nodes of level 2 of a tree can be accessed by following only two branches from the root, etc. The balance factor of a binary tree is the difference in height between its left and right subtrees:

B HL HR
Prof. (Dr) N Guruprasad EEC 012 - DATA STRUCTURES TREES 17

Session: Jan June 2013

Balance of the tree

B=0

B=0

B=1

B=-1

B=0

B=1

B=-2

B=2

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

18

Session: Jan June 2013

Complete and nearly complete binary trees


A complete tree has the maximum number of entries for its height. The maximum number is reached when the last level is full. A tree is considered nearly complete if it has the minimum height for its nodes and all nodes in the last level are found on the left

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

19

Session: Jan June 2013

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

20

Session: Jan June 2013

Representation of a Binary Tree There are two ways of representing tree in memory : 1) Linked representation 2) Sequential representation

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

21

Session: Jan June 2013

Linked Representation : We use three arrays : 1) INFO[K] contains data at node N 2) LEFT[K] contains location of left child of N 3) RIGHT [K] contains location of right child of N ROOT will contain location of root R of T.

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

22

Session: Jan June 2013

Consider the binary tree and represent it in linked representation format.

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

23

Session: Jan June 2013

Sequential Representation : Uses single linear array called TREE : 1) ROOT node R is stored in TREE[1] 2) LEFT node is placed at TREE[2*K] where K position of its parent node 3) RIGHT node is placed at TREE[2*K+1] where K position of its parent node

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

24

Session: Jan June 2013

Consider the binary tree and represent it in sequential representation format.

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

25

Session: Jan June 2013

Primitive operations
Traversing Insertion Deletion

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

26

Session: Jan June 2013

Binary Tree Traversal


A binary tree traversal requires that each node of the tree be processed once and only once in a predetermined sequence. Three types : Pre order traversal algorithm In order traversal algorithm Post order traversal algorithm

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

27

Session: Jan June 2013

Preorder Traversal
Step 1 : Process root T Step 2 : Traverse left subtree again in PREORDER Step 3 : Traverse right subtree again in PREORDER Also called NLR ( Node, Left, Right ) Traversal Ex : A B D E F C G H J L K

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

28

Session: Jan June 2013

Function representation :
pre_trav(struct btree *tree) { if(root==NULL) printf(\n Tree is empty ); getch(); } if(tree!=NULL) { printf(\n %d ,treeinfo); pre_trav(treeleft); pre_trav(treeright); } else return;
Prof. (Dr) N Guruprasad EEC 012 - DATA STRUCTURES TREES 29

Session: Jan June 2013

Inorder Traversal
Step 1 : Traverse left subtree in INORDER Step 2 : Process root T Step 3 : Traverse right subtree again in INORDER Also called LNR (Left, Node, Right ) Traversal Ex : D B F E A G C L J H K

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

30

Session: Jan June 2013

Function representation :
in_trav(struct btree *tree) { if(root==NULL) printf(\n Tree is empty ); getch(); } if(tree!=NULL) { in_trav(treeleft); printf(\n %d ,treeinfo); in_trav(treeright); } else return;
Prof. (Dr) N Guruprasad EEC 012 - DATA STRUCTURES TREES 31

Session: Jan June 2013

Postorder Traversal
Step 1 : Traverse left subtree in POSTORDER Step 2 : Traverse right subtree again in POSTORDER Step 3 : Process root T Also called LRN (Left, Right, Node ) Traversal Ex : D F E B G L J K H C A

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

32

Session: Jan June 2013

Function representation :
post_trav(struct btree *tree) { if(root==NULL) printf(\n Tree is empty ); getch(); } if(tree!=NULL) { post_trav(treeleft); post_trav(treeright); printf(\n %d ,treeinfo); } else return;
Prof. (Dr) N Guruprasad EEC 012 - DATA STRUCTURES TREES 33

Session: Jan June 2013

Binary Search Trees


This data structure enables us to search an element. Suppose T is a binary tree ,then T is called BINARY SEARCH TREE if each node N of T has the following properties :-

the value at any node N is greater than every value in left subtree and less than every value in right subtree.
Also called BINARY SORTED TREE

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

34

Session: Jan June 2013

Example : ( Fig BST 1 )


38

14

56

23

45

82

18

70

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

35

Session: Jan June 2013

Primitive operations on BST


Traversing Insertion Deletion

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

36

Session: Jan June 2013

w.r.t fig BST-1

Pre order traversal : 38 14 8 23 18 56 45 82 70 In order traversal : 8 14 18 23 38 45 56 70 82 Post order traversal : 8 18 23 14 45 70 82 56 38

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

37

Session: Jan June 2013

Searching & Inserting in a BST


Suppose an item of information is given, the following algorithm finds the location of ITEM in binary search tree T or inserts ITEM as a new node in its appropriate place. a) Compare ITEM with root node N 1) If ITEM<N, proceed to left subtree. 2) If ITEM>N, proceed to right subtree.

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

38

Session: Jan June 2013

Searching & Inserting in a BST


b) Repeat step (a) until one of the following occurs, 1) We meet a node N such that ITEM=N. In this case, search is successful and no insertion takes place. 2) We meet an empty subtree which indicates search is unsuccessful and ITEM is inserted in place of an empty subtree.

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

39

Session: Jan June 2013

Example : Consider fig BST 1 Suppose ITEM = 20 is to be inserted.

Answer : We insert as right successor of 18

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

40

Session: Jan June 2013

Deleting in a BST There are three different types of deletion in a BST. 1) Nodes with no successor (leaf node ) 2) Nodes with one successor 3) Nodes with two successors Let us consider the tree T ( BST-2) and its linked representation

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

41

Session: Jan June 2013

Example : ( Fig BST 2 )


60

25

75

15

50

66

33

44
Prof. (Dr) N Guruprasad EEC 012 - DATA STRUCTURES TREES 42

Session: Jan June 2013

INFO 1 33

RIGHT 0

LEFT 9

Root
3

2
3 4 5

25
60 66

8
2 0 6 0

10
7 0

Avail

7
8 9 10

75
15 44 50

4
0 0 1

0
0 0 0

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

43

Session: Jan June 2013

1) Nodes with no successor (leaf node ) Ex : 15, 44 & 66 Delete : 44 Draw the tree and linked representation.

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

44

Session: Jan June 2013

60

25

75

15

50

66

33

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

45

Session: Jan June 2013

INFO 1 33

RIGHT 0

LEFT 9

Root
3

2
3 4 5

25
60 66

8
2 0 6 0

10
7 0

Avail

7
8 9 10

75
15 50

4
0 5 1

0
0 0

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

46

Session: Jan June 2013

2) Nodes with one successor Ex : 50, 33 & 75 Delete : 75 Draw the tree and linked representation.

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

47

Session: Jan June 2013

60

25

66

15

50

33

44
Prof. (Dr) N Guruprasad EEC 012 - DATA STRUCTURES TREES 48

Session: Jan June 2013

INFO 1 33

RIGHT 0

LEFT 9

Root
3

2
3 4 5

25
60 66

8
2 0 6 0

10
4 0

Avail

7
8 9 10 15 44 50

5
0 0 1

0
0 0 0

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

49

Session: Jan June 2013

3) Nodes with two successor Ex : 25 & 60 Delete : 25 Draw the tree and linked representation.

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

50

Session: Jan June 2013

60

33

75

15

50

66

44

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

51

Session: Jan June 2013

INFO 1 33 60 66

RIGHT 8

LEFT 10 7 0

Root
3

2
3 4 5

5
1 0 6 0

Avail

7
8 9 10

75
15 44 50

4
0 0 9

0
0 0 0

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

52

Session: Jan June 2013

Problem Suppose the following eight numbers are inserted in order into an empty BST T. 50 33 44 22 77 Draw the tree T 35 60

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

53

Session: Jan June 2013

Problem Suppose the following list of letters is inserted in order into an empty BST T. J R D G T E M H P A F Q Draw the tree T and also find INORDER traversal.

Prof. (Dr) N Guruprasad

EEC 012 - DATA STRUCTURES TREES

54

Vous aimerez peut-être aussi