"trees - PowerPoint"
Introduction to Trees Submitted by :Lovish Bajaj Tree : Non linear – data structure . Root node B D E F C G Leaf nodes Basic terms & definitions :Leaf node :- Node which does not have any child. Parent :- Predecessor of a node is called it’s parent. A B D siblings Root node C F G Child :- Successor of a node is called it’s child. Siblings:-Children of same parent are called siblings. E Leaf nodes A Level number :- Rank of hierarchy. Level 0 Level 1 B Height of tree :- Maximum number of nodes in a path starting from root node to leaf node. D E F C G Level 2 H I Level 4 Level 3 Degree of node :- Maximum number of children a node can have. J General tree :A finite set of one or more nodes such that there is specially designated node i.e. root node and remaining nodes are partitioned into disjoint sets T1 ,T2 ,T3 ,…..TN where each Ti is a sub tree. Binary tree :A binary tree T is defined as finite set of elements ,called nodes such that : a)T is empty (called null tree or empty tree),or b)T contains a distinguished node R , called the root node of T, and the remaining nodes of T form an ordered pair of disjoint binary trees T1 ,T2. • If T contains a root R then two trees T1 ,T2 are called ,respectively the left and right sub trees of R. • If T1 is non-empty, then it’s root is called left successor of R, similarly if T 2 is non-empty then it’s root is called right successor of R. Representation of binary tree :- Maximum number of nodes in a tree of height h are :-2 h - 1 Minimum number of nodes in tree of height h are :- h Full Binary Tree :- When all the levels have maximum number of possible nodes . Complete binary tree :- When all the levels have maximum number of nodes except the last level and in last level the nodes should appear towards left . How to represent a tree in memory ? Generally we follow two ways in which we represent a tree in memory : Array representation Linked list representation Array representation :In this if parent node is stored at (i)th position then :Left child is stored at = 2 * i Right child is stored at = (2 * i) + 1 Eg :D A B E C F G A 1 B 2 C 3 D 4 E 5 F 6 G 7 Eg:A 1 2 B C A B C 3 4 5 M D M N D E F 7 N 9 E F 13 26 LOT OF MEMORY WASTED Linked list representation :Simple representation :- LC ROOT RC Linked list representation :A A B C B C M D M E F D O O E F Tree traversing :There are three ways in which we do tree traversing : Preorder traversing Inorder traversing Postorder traversing Preorder traversing :Process the root R . 2. Traverse the left subtree of R in preorder . 3. Traverse the right subtree of R in preorder . 1. A RESULT OF PREORDER TRAVERSAL : B D E C F ABDECF Inorder traversing :Traverse the left subtree of R in inorder . 2. Process the root R . 3. Traverse the right subtree of R in inorder . 1. A RESULT OF INORDER TRAVERSAL : B D E C F DBEACF Postorder traversing :Traverse the left subtree of R in postorder . 2. Traverse the right subtree of R in postorder . 3. Process the root R . 1. A RESULT OF PREORDER TRAVERSAL : B D E C F DEBFCA THANKS