Trees

44
Presented By: Susheel Thakur M.Tech(C.S) Roll No- 324

Transcript of Trees

Page 1: Trees

Presented By:Susheel Thakur

M.Tech(C.S)Roll No- 324

Page 2: Trees

Have you ever thought how does the OS manages or files?

Why do we have a hierarchical file system?

How do files get saved and deleted under hierarchical directories ?

SOLUTION: A hierarchical data structure called Trees.

04/12/23 04:50 AM 2Data Structures and Algorithms

Page 3: Trees

Tree Nodes Each node can have 0 or more children A node can have at most one parent

Binary tree Tree with 0–2 children per node

Tree Binary Tree04/12/23 04:50 AM 3Data Structures and Algorithms

Page 4: Trees

Terminology Root no parent Leaf no child Interior non-leaf Height maximum level of a node Level Length of path from root to node plus

1 Root node

Leaf nodes

Interior nodes Height

04/12/23 04:50 AM 4Data Structures and Algorithms

Page 5: Trees

General Tree – organization chart format Parenthetical Listing

04/12/23 04:50 AM 5Data Structures and Algorithms

Page 6: Trees

04/12/23 04:50 AM 6Data Structures and Algorithms

Page 7: Trees

Parenthetical Listing – the algebraic expression, where each open parenthesis indicates the start of a new level and each closing parenthesis completes the current level and moves up one level in the tree.

04/12/23 04:50 AM 7Data Structures and Algorithms

Page 8: Trees

A (B (C D) E F (G H I) )

04/12/23 04:50 AM 8Data Structures and Algorithms

Page 9: Trees

List Representation( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )The root comes first, followed by a list of sub-trees

data link 1 link 2 ... link n

04/12/23 04:50 AM 9Data Structures and Algorithms

Page 10: Trees

Every tree node: object – useful information children – pointers to its children nodes

O

O O

O

O

04/12/23 04:50 AM 10Data Structures and Algorithms

Page 11: 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.

04/12/23 04:50 AM 11Data Structures and Algorithms

Page 12: Trees

04/12/23 04:50 AM 12Data Structures and Algorithms

Page 13: Trees

A null tree is a tree with no nodes

04/12/23 04:50 AM 13Data Structures and Algorithms

Page 14: 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

maxH NA tree with a maximum height is rare. It occurs when all of the nodes in the entire tree have only one successor.

14Data Structures and Algorithms

Page 15: Trees

The minimum height of a binary tree is determined as follows:

min 2log 1H N

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

15Data Structures and Algorithms

Page 16: Trees

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

minN H

16Data Structures and Algorithms

Page 17: Trees

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

max 2 1HN

17Data Structures and Algorithms

Page 18: 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

04/12/23 04:50 AM 18Data Structures and Algorithms

Page 19: Trees

04/12/23 04:50 AM 19Data Structures and Algorithms

Page 20: Trees

04/12/23 04:50 AM 20Data Structures and Algorithms

Page 21: Trees

Sequential Representation

AB--C------D--.E

[1][2][3][4][5][6][7][8][9].[16]

[1][2][3][4][5][6][7][8][9]

ABCDEFGHI

A

B

E

C

D

A

B C

GE

I

D

H

F

(1) waste space(2) insertion/deletion problem

04/12/23 04:50 AM 21Data Structures and Algorithms

Page 22: Trees

Linked Representationtypedef struct tnode *ptnode;

typedef struct tnode {

int data;

ptnode left, right;

};

dataleft right

data

left right

04/12/23 04:50 AM 22Data Structures and Algorithms

Page 23: Trees

04/12/23 04:50 AM 23Data Structures and Algorithms

Page 24: Trees

A binary tree traversal requires that each node of the tree be processed once and only once in a predetermined sequence.

04/12/23 04:50 AM 24Data Structures and Algorithms

Page 25: Trees

04/12/23 04:50 AM 25Data Structures and Algorithms

Page 26: Trees

04/12/23 04:50 AM 26Data Structures and Algorithms

Page 27: Trees

04/12/23 04:50 AM 27Data Structures and Algorithms

Page 28: Trees

04/12/23 04:50 AM 28Data Structures and Algorithms

Page 29: Trees

04/12/23 04:50 AM 29Data Structures and Algorithms

Page 30: Trees

Preorder Traversal (recursive version)

void preorder(tnode ptr)

/* preorder tree traversal */

{

if (ptr) {

printf(“%d”, ptr->data);

preorder(ptr->left);

predorder(ptr->right);

}

}

04/12/23 04:50 AM 30Data Structures and Algorithms

Page 31: Trees

04/12/23 04:50 AM 31Data Structures and Algorithms

Page 32: Trees

04/12/23 04:50 AM 32Data Structures and Algorithms

Page 33: Trees

Inorder Traversal (recursive version)

void inorder(tnode ptr)

/* inorder tree traversal */

{

if (ptr) {

inorder(ptr->left);

printf(“%d”, ptr->data);

indorder(ptr->right);

}

}

04/12/23 04:50 AM 33Data Structures and Algorithms

Page 34: Trees

04/12/23 04:50 AM 34Data Structures and Algorithms

Page 35: Trees

04/12/23 04:50 AM 35Data Structures and Algorithms

Page 36: Trees

Postorder Traversal (recursive version)

void postorder(tnode ptr)

/* postorder tree traversal */

{

if (ptr) {

postorder(ptr->left);

postdorder(ptr->right);

printf(“%d”, ptr->data);

}

}

04/12/23 04:50 AM 36Data Structures and Algorithms

Page 37: Trees

Using Pre order and Post order Traversal

Using Post order and In order Traversal

04/12/23 04:50 AM 37Data Structures and Algorithms

Page 38: Trees

1. Scan the preorder traversal from left to right.

2. For each scanned node locate its position in inorder traversal. Let the scanned node be X.

3. The nodes preceding X in inorder form its left subtree and nodes succeeding it form right subtree.

4. Repeat step1 for each symbol in the preorder.

04/12/23 04:50 AM 38Data Structures and Algorithms

Page 39: Trees

Preorder: A B D H E C F G

Inorder: D H B E A F C G

A

B C

D E

H

F G

04/12/23 04:50 AM 39Data Structures and Algorithms

Page 40: Trees

1. Scan the post order traversal from right to left.

2. For each node scanned locate its position in order traversal. Let the scanned node be X.

3. The node preceding X in inorder, form its left subtree and nodes succeeding it form right subtree.

04/12/23 04:50 AM 40Data Structures and Algorithms

Page 41: Trees

A

B C

D E

I

F G

Postorder: H D I E B J F K L G C A

Inorder: H D B I E A F J C K G L

H JK L

04/12/23 04:50 AM 41Data Structures and Algorithms

Page 42: Trees

A variable is an expression. If x and y are expressions, then ¬x, xy,

xy are expressions. Parentheses can be used to alter the

normal order of evaluation (¬ > > ). Example: x1 (x2 ¬x3)

04/12/23 04:50 AM 42Data Structures and Algorithms

Page 43: Trees

X3X1

X2 X1

X3

(x1 ¬x2) (¬ x1 x3) ¬x3

postorder traversal (postfix evaluation)04/12/23 04:50 AM 43Data Structures and Algorithms

Page 44: Trees

04/12/23 04:50 AM 44Data Structures and Algorithms