Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 to support searching operations in O(logN) average time

2 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 T 1, T 2, … T k, each of whose roots are connected by a direct edge from r.

3 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.

4 4 4.1: Preliminaries Sibling Node B and node C are siblings if they are left and right siblings of the same node A Leaf A node is called a leaf if it has no children

5 5 4.1: Preliminaries Ancestor Node 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) Descendant Node 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

6 6 4.1: Preliminaries Level of a Node Level 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 Tree The depth of a tree is the maximum level of any leaf in the tree (also called the height of the tree)

7 7 4.1.1 Preliminaries: Implementation of Tree typedef struct TreeNode * PtrToNode; struct TreeNode {ElementType element; PtrToNode FirstChild; PtrToNode NextSibling; }

8 8 4.1.1 Preliminaries: Implementation of Tree Example

9 9 4.1.1 Preliminaries: Implementation of Tree FirstChild pointer: arrow that points downward NextSibling pointer: arrow that goes left to right

10 10 4.1.1 Preliminaries: Unix File System

11 11 4.1.1 Preliminaries: Unix File System Any suggestions to handle such a file system?

12 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 D ListDir(C, Depth +1);} } void ListDirectory(DirectoryOrFile D) {ListDir (D, 0); }

13 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 D TotalSize += SizeDirectory(C); } return TotalSize; }

14 14 4.1.1 Preliminaries: Traversal Strategy 3 methods of traversal postorder traversal strategy preorder traversal strategy inorder traversal strategy

15 15 4.1.1 Preliminaries: Traversal Strategy Preorder traversal (depth-first order) 1.Visit the node 2.Traverse the left subtree in preorder 3.Traverse the right subtree in preorder

16 16 4.1.1 Preliminaries: Traversal Strategy Inorder traversal (symmetric order) 1.Traverse the left subtree in inorder 2.Visit the node 3.Traverse the right subtree in inorder

17 17 4.1.1 Preliminaries: Traversal Strategy Postorder traversal (breadth-first order) 1.Traverse the left subtree in postorder 2.Traverse the right subtree in postorder 3.Visit the node

18 18 4.1.1 Preliminaries: Traversal Strategy + -H /* + *E - ABCDF G Example

19 19 4.1.1 Preliminaries: Traversal Strategy + -H /* + *E - ABCDF G Preorder: +-/+AB*CD*E-FGH Inorder : A+B/C*D-E*F-G+H Postorder: AB+CD*/EFG-*-H+ Preorder: +-/+AB*CD*E-FGH Inorder : A+B/C*D-E*F-G+H Postorder: AB+CD*/EFG-*-H+

20 20 4.1.1 Preliminaries: Traversal Strategy Exercise: Using the tree shown in page 9, write down the results if employing the postorder traversal strategy preorder traversal strategy inorder traversal strategy

21 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.

22 22 4.2 Binary Trees Strictly Binary Tree A binary tree is called strictly binary tree if every nonleaf node in the tree has nonempty left and right subtrees Complete (Full) Binary Tree A complete binary tree of depth d is a strictly binary tree with all leaf nodes at level d.

23 23 4.2 Binary Trees Almost Complete Binary Tree A 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.

24 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.

25 25 4.2 Binary Trees Strictly binary but not almost complete Level 1, 2, 3

26 26 4.2 Binary Trees Strictly binary but not almost complete Violate condition 2

27 27 4.2 Binary Trees Strictly binary and almost complete

28 28 4.2 Binary Trees Almost complete but not strictly binary

29 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

30 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 );

31 31 4.3.1 BST: Implementation struct TreeNode { ElementType Element; SearchTree Left; SearchTree Right; };

32 32 4.3.1 BST Implementation: MakeEmpty SearchTree MakeEmpty( SearchTree T ) { if( T != NULL ) { MakeEmpty( T  Left ); MakeEmpty( T  Right ); free( T ); } return NULL; }

33 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 ); elsereturn T; }

34 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 ); }

35 35 4.3.1 BST Implementation: FindMax Position FindMax( SearchTree T ) { if ( T != NULL ) while ( T  Right != NULL ) T = T  Right; return T; }

36 36 4.3.2 BST Implementation: Insert Please refer to Figure 4.21 

37 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; }

38 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!! */ }

39 39 4.3.3 BST Implementation: Delete 3 cases (please refer to Figure 4.24) 

40 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 */

41 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;}

42 42 4.4 : Expression Tree Expression tree for (a+b*c) +((d*e+f)*g)

43 43 4.4 : Expression Tree Algorithm to convert postfix expression into expression tree e.g. input: ab+cde+**

44 44 4.4 : Expression Tree


Download ppt "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."

Similar presentations


Ads by Google