440 likes | 567 Views
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. 4.1: Preliminaries.
E N D
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
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.
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.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
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
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)
4.1.1 Preliminaries: Implementation of Tree typedef struct TreeNode * PtrToNode; struct TreeNode { ElementType element; PtrToNode FirstChild; PtrToNode NextSibling; }
4.1.1 Preliminaries: Implementation of Tree FirstChild pointer: arrow that points downward NextSibling pointer: arrow that goes left to right
4.1.1 Preliminaries: Unix File System • Any suggestions to handle such a file system?
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); }
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; }
4.1.1 Preliminaries: Traversal Strategy • 3 methods of traversal postorder traversal strategy preorder traversal strategy inorder traversal strategy
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
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
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
4.1.1 Preliminaries: Traversal Strategy + Example - H / * + - * E G A B C D F
4.1.1 Preliminaries: Traversal Strategy Preorder: +-/+AB*CD*E-FGH Inorder : A+B/C*D-E*F-G+H Postorder: AB+CD*/EFG-*-H+ + - H / * + - * E G A B C D F
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
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.
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.
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.
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.
4.2 Binary Trees • Strictly binary but not almost complete Level 1, 2, 3
4.2 Binary Trees • Strictly binary but not almost complete Violate condition 2
4.2 Binary Trees • Strictly binary and almost complete
4.2 Binary Trees • Almost complete but not strictly binary
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. Please refer to figure 4.15 X
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 );
4.3.1 BST: Implementation struct TreeNode { ElementType Element; SearchTree Left; SearchTree Right; };
4.3.1 BST Implementation: MakeEmpty SearchTree MakeEmpty( SearchTree T ) { if( T != NULL ) { MakeEmpty( TLeft ); MakeEmpty( T Right ); free( T ); } return NULL; }
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; }
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 ); }
4.3.1 BST Implementation: FindMax Position FindMax( SearchTree T ) { if ( T != NULL ) while ( T Right != NULL ) T = T Right; return T; }
4.3.2 BST Implementation: Insert • Please refer to Figure 4.21
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; } }
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!! */ }
4.3.3 BST Implementation: Delete • 3 cases (please refer to Figure 4.24)
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 */
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;}
4.4 : Expression Tree Expression tree for (a+b*c) +((d*e+f)*g)
4.4 : Expression Tree Algorithm to convert postfix expression into expression tree e.g. input: ab+cde+**