+ All Categories
Home > Documents > 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used...

1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used...

Date post: 06-Jan-2018
Category:
Upload: jane-fletcher
View: 216 times
Download: 1 times
Share this document with a friend
Description:
3 4.1: Preliminaries Parent Node A is the parent of node B if B is the root of the left or right subtree of A Left (Right) Child Node B is the left (right) child of node A if A is the parent of B.
44
1 Chapter 4 Trees • Basic concept • How tree are used to implement the file system • How tree can be used to evaluate arithmetic expressions • How to use trees to support searching operations in O(logN) average time
Transcript
Page 1: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

1

Chapter 4 Trees

• Basic concept• How tree are used to implement the file

system• How tree can be used to evaluate

arithmetic expressions• How to use trees to support searching

operations in O(logN) average time

Page 2: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

2

4.1: Preliminaries

One natural way to define a tree is recursively. A tree is a collection of nodes. The collection can be empty; otherwise, a tree consists of a distinguished node r, called root, and zero or more nonempty (sub)trees T1, T2, … Tk, each of whose roots are connected by a direct edge from r.

Page 3: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

3

4.1: Preliminaries

• ParentNode A is the parent of node B if B is the root of the left or right subtree of A

• Left (Right) ChildNode B is the left (right) child of node A if A is the parent of B.

Page 4: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

4

4.1: Preliminaries

• SiblingNode B and node C are siblings if they are left and right siblings of the same node A

• LeafA node is called a leaf if it has no children

Page 5: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

5

4.1: Preliminaries

• AncestorNode A is ancestor of node B if A is either the parent of B or is the parent of some ancestor of B

• Left (Right) DescendantNode B is the left (right) descendant of node A if B is either the left (right) child of A or a descendant of a left (right) child of A

Page 6: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

6

4.1: Preliminaries

• Level of a NodeLevel of the root of a binary tree is 0, and the level of any other node in the tree is one more than the level of its parent

• Depth of a TreeThe depth of a tree is the maximum level of any leaf in the tree (also called the height of the tree)

Page 7: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

7

4.1.1 Preliminaries: Implementation of Tree

typedef struct TreeNode * PtrToNode;struct TreeNode{ ElementType element;

PtrToNode FirstChild;PtrToNode NextSibling;

}

Page 8: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

8

4.1.1 Preliminaries: Implementation of Tree

Example

Page 9: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

9

4.1.1 Preliminaries: Implementation of Tree

FirstChild pointer: arrow that points downward

NextSibling pointer: arrow that goes left to right

Page 10: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

10

4.1.1 Preliminaries: Unix File System

Page 11: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

11

4.1.1 Preliminaries: Unix File System

• Any suggestions to handle such a file system?

Page 12: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

12

4.1.1 Preliminaries: Unix File System

Static void ListDir(DirectoryOrFile D, int Depth){ if (D is a legitimate entry)

{ PrintName(D, Depth);if (D is a directory)for each child,C, of DListDir(C, Depth +1);}

}void ListDirectory(DirectoryOrFile D){ListDir (D, 0); }

Page 13: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

13

4.1.1 Preliminaries: Unix File System

Static void SizeDirectory(DirectoryOrFile D){ int TotalSize;

TotalSize = 0;if (D is a legitimate entry){ TotalSize = FileSize(D);if (D is a directory)for each child, C, of DTotalSize += SizeDirectory(C); }

return TotalSize; }

Page 14: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

14

4.1.1 Preliminaries: Traversal Strategy

• 3 methods of traversal postorder traversal strategypreorder traversal strategyinorder traversal strategy

Page 15: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

15

4.1.1 Preliminaries: Traversal Strategy

Preorder traversal (depth-first order)1.Visit the node2.Traverse the left subtree in preorder3.Traverse the right subtree in preorder

Page 16: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

16

4.1.1 Preliminaries: Traversal Strategy

• Inorder traversal (symmetric order)1. Traverse the left subtree in inorder2. Visit the node3. Traverse the right subtree in inorder

Page 17: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

17

4.1.1 Preliminaries: Traversal Strategy

• Postorder traversal (breadth-first order)1. Traverse the left subtree in postorder2. Traverse the right subtree in

postorder3. Visit the node

Page 18: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

18

4.1.1 Preliminaries: Traversal Strategy

+

- H

/ *

+ * E -

A B C D F G

Example

Page 19: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

19

4.1.1 Preliminaries: Traversal Strategy

+

- H

/ *

+ * E -

A B C D F G

Preorder: +-/+AB*CD*E-FGHInorder : A+B/C*D-E*F-G+HPostorder: AB+CD*/EFG-*-H+

Page 20: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

20

4.1.1 Preliminaries: Traversal Strategy

• Exercise:• Using the tree shown in page 9, write

down the results if employing thepostorder traversal strategypreorder traversal strategyinorder traversal strategy

Page 21: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

21

4.2 Binary Trees

A binary tree is a finite set of elements called nodes. This set is either empty or it can be partitioned into 3 subsets. The first subset is a single node called the root of the tree. The other two subsets are themselves binary trees. One is called the left subtree and the other the right subtree of the binary tree.

Page 22: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

22

4.2 Binary Trees

• Strictly Binary TreeA binary tree is called strictly binary tree if every nonleaf node in the tree has nonempty left and right subtrees

• Complete (Full) Binary TreeA complete binary tree of depth d is a strictly binary tree with all leaf nodes at level d.

Page 23: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

23

4.2 Binary Trees

• Almost Complete Binary TreeA binary tree of depth d is an almost complete binary tree if(a) Any node at level less than d-1 has 2 children.(b) For any node N in the tree with a right descendant at level d, N must have a left child and every left descendant of N is either a leaf at level d or has 2 children.

Page 24: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

24

4.2 Binary Trees

An almost complete strictly binary tree with n leaves has 2n - 1 nodes.

There is only a single almost complete binary tree with n nodes. This tree is strictly binary if and only if n is odd.

Page 25: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

25

4.2 Binary Trees

• Strictly binary but not almost complete

Level 1, 2, 3

Page 26: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

26

4.2 Binary Trees

• Strictly binary but not almost complete

Violate condition 2

Page 27: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

27

4.2 Binary Trees

• Strictly binary and almost complete

Page 28: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

28

4.2 Binary Trees

• Almost complete but not strictly binary

Page 29: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

29

4.3 Binary Search Trees

• Every node, X, in the tree, the value of all the keys in its left subtree are smaller than the key value of X, and the values of all the keys in its right subtree are larger than the key value of X.

X

Please refer to figure 4.15

Page 30: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

30

4.3.1 BST: Implementation

struct TreeNode;typedef struct TreeNode *Position;typedef struct TreeNode *SearchTree;SearchTree MakeEmpty( SearchTree T );Position Find( ElementType X, SearchTree T );Position FindMin( SearchTree T );Position FindMax( SearchTree T );SearchTree Insert( ElementType X, SearchTree T );SearchTree Delete( ElementType X, SearchTree T );ElementType Retrieve( Position P );

Page 31: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

31

4.3.1 BST: Implementation

struct TreeNode{

ElementType Element;SearchTree Left;SearchTree Right;

};

Page 32: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

32

4.3.1 BST Implementation: MakeEmpty

SearchTree MakeEmpty( SearchTree T ) { if( T != NULL ) { MakeEmpty( TLeft ); MakeEmpty( T Right ); free( T ); } return NULL; }

Page 33: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

33

4.3.1 BST Implementation: Find

Position Find( ElementType X, SearchTree T ){ if ( T == NULL ) return NULL; if ( X < T Element ) return Find( X, T Left ); else if ( X > T Element ) return Find( X, T Right ); else return T; }

Page 34: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

34

4.3.1 BST Implementation: FindMin

Position FindMin( SearchTree T ) { if ( T == NULL ) return NULL; else if( T Left == NULL ) return T; else return FindMin( T Left );}

Page 35: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

35

4.3.1 BST Implementation: FindMax

Position FindMax( SearchTree T ){

if ( T != NULL ) while ( T Right != NULL ) T = T Right;

return T;}

Page 36: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

36

4.3.2 BST Implementation: Insert

• Please refer to Figure 4.21

Page 37: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

37

4.3.2 BST Implementation: Insert

SearchTree Insert( ElementType X, SearchTree T ){ if ( T == NULL ) { T = malloc( sizeof( struct TreeNode ) ); if ( T == NULL )

FatalError( "Out of space!!!" ); else

{ T Element = X;

T Left = T Right = NULL; } }

Page 38: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

38

4.3.2 BST Implementation: Insert

else

if ( X < T Element )

T Left = Insert( X, T Left ); else

if( X > T Element )

T Right = Insert( X, T Right ); /* Else X is in the tree already; we'll do nothing */

return T; /* Do not forget this line!! */ }

Page 39: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

39

4.3.3 BST Implementation: Delete

• 3 cases (please refer to Figure 4.24)

Page 40: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

40

4.3.3 BST Implementation: Delete SearchTree Delete( ElementType X, SearchTree T ) { Position TmpCell; if ( T == NULL )

Error( "Element not found" ); else

if ( X < T Element ) /* Go left */

T Left = Delete( X, T Left ); else

if ( X > T Element ) /* Go right */

T Right = Delete( X, T Right ); else /* Found element to be deleted */

Page 41: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

41

4.3.3 BST Implementation: Delete if ( T Left && T Right ) /* Two children */

{ TmpCell = FindMin( T Right );

T Element = TmpCell Element;

T Right = Delete( T Element, T Right );} else /* One or zero children */ { TmpCell = T;

if ( T Left == NULL ) /* Also handles 0 children */

T = T Right;

else if ( T Right == NULL ) T = T Left; free( TmpCell );} return T;}

Page 42: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

42

4.4 : Expression Tree

Expression tree for (a+b*c) +((d*e+f)*g)

Page 43: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

43

4.4 : Expression Tree

Algorithm to convert postfix expression into expression treee.g. input: ab+cde+**

Page 44: 1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

44

4.4 : Expression Tree


Recommended