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.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
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.
Node A is the parent of node B if B is the root of the left or right subtree of A
Node B is the left (right) child of node A if A is the parent of B.
Node B and node C are siblings if they are left and right siblings of the same node A
A node is called a leaf if it has no children
Node A is ancestor of node B if A is either the parent of B or is the parent of some ancestor of B
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
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
The depth of a tree is the maximum level of any leaf in the tree (also called the height of the tree)
typedef struct TreeNode * PtrToNode;
struct TreeNode
{ ElementType element;
PtrToNode FirstChild;
PtrToNode NextSibling;
}
FirstChild pointer: arrow that points downward
NextSibling pointer: arrow that goes left to right
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); }
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; }
postorder traversal strategy
preorder traversal strategy
inorder traversal strategy
Preorder traversal (depthfirst order)
1. Visit the node
2. Traverse the left subtree in preorder
3. Traverse the right subtree in preorder
1. Traverse the left subtree in inorder
2. Visit the node
3. Traverse the right subtree in inorder
1. Traverse the left subtree in postorder
2. Traverse the right subtree in postorder
3. Visit the node
Preorder:
+/+AB*CD*EFGH
Inorder :
A+B/C*DE*FG+H
Postorder: AB+CD*/EFG*H+
+

H
/
*
+

*
E
G
A
B
C
D
F
postorder traversal strategy
preorder traversal strategy
inorder traversal strategy
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.
A binary tree is called strictly binary tree if every nonleaf node in the tree has nonempty left and right subtrees
A complete binary tree of depth d is a strictly binary tree with all leaf nodes at level d.
A binary tree of depth d is an almost complete binary tree if
(a) Any node at level less than d1 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.
Please refer to figure 4.15
X
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 );
SearchTree MakeEmpty( SearchTree T )
{ if( T != NULL )
{
MakeEmpty( TLeft );
MakeEmpty( T Right );
free( T );
}
return NULL;
}
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;
}
Position FindMin( SearchTree T )
{ if ( T == NULL )
return NULL;
else
if( T Left == NULL )
return T;
else
return FindMin( T Left );
}
Position FindMax( SearchTree T )
{
if ( T != NULL )
while ( T Right != NULL )
T = T Right;
return T;
}
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;
}
}
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!! */
}
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 */
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;}
Expression tree for (a+b*c) +((d*e+f)*g)
Algorithm to convert postfix expression into expression tree
e.g. input: ab+cde+**