Trees (Unit 7)
Outline Trees: Definition and Basic Terminologies Representation of Trees Binary Trees: Basic Terminologies and Types Representation of Binary Trees Array representation of binary trees Linked representation of binary trees Binary Tree Traversals Inorder traversal Postorder traversal Preorder traversal Threaded Binary Trees Applications
A tree is defined as a finite set of one or more nodes such that There is a specially designated node called the root and The rest of the nodes could be partitioned into t disjoint sets (t >0) each set representing a tree Ti, i=1,2, . . . t known as subtree of the tree. A node in the definition of the tree represents an item of information, and the links between the nodes termed as branches, represent an association between the items of information.
Basic terminologies degree of the node leaf nodes or terminal nodes non terminal nodes children siblings ancestors degree of a tree hierarchical structure height or depth of a tree forest 3 2 1 4
Representation of a tree List Representation Linked List Representation
(A (B(F,G,H), C, D(I), E(J,K(L))) ) List representation (A (B(F,G,H), C, D(I), E(J,K(L))) ) A B C D E F G H I J K L Level 1 Level 2 Level 3 Level 4
(b) Linked list representation of the tree DATA LINK 1 LINK 2 … LINK n (a) General node structure F K T A B G H I C D E L J (b) Linked list representation of the tree
An alternative elegant linked representation (a) General node structure 1 A C BH F G H D I E J O K L (b) Linked representation of the tree TAG DATA / DOWNLINK LINK 0/1 1
Binary Trees A binary tree has the characteristic of all nodes having at most two branches, that is, all nodes have a degree of at most 2. A binary tree can therefore be empty or consist of a root node and two disjoint binary trees termed left subtree and right subtree.
A G C D B E F Level 1 Level 2 Level 3 Fig. 2
Important observations regarding binary trees: The maximum number of nodes on level i of a binary tree is 2i-1, i>1 The maximum number of nodes in a binary tree of height h is 2h-1, h>1 For any non empty binary tree, if to is the number of terminal nodes and t2 is the number of nodes of degree 2, then to=t2+1
A binary tree of height h which has all its permissible maximum number of nodes viz., 2h-1 intact is known as a full binary tree of height h. A binary tree with n’ nodes and height h is complete if its nodes correspond to the nodes which are numbered 1 to n (n’ n) in a full binary tree of height h. Also, the height of a complete binary tree with n elements has a height h given by
A binary tree which is dominated solely by left child nodes or right child nodes is called a skewed binary tree or more specifically left skewed binary tree or right skewed binary tree respectively. a b c d n o Left skewed Right skewed m p
Types of Binary Trees Skewed – only one child Complete – always two children Balanced – “mostly” two children more formal definitions exist, above are intuitive ideas Skewed binary tree Balanced binary tree Complete binary tree
Binary Trees Properties Balanced Height = O( log(n) ) for n nodes Useful for searches Skewed Height = O(n) for n nodes Similar to linked list Skewed binary tree Balanced binary tree
A complete binary tree obeys the following properties with regard to its node numbering: If a parent node has a number i then its left child has the number 2i (2i<n). If 2i>n then i has no left child. If a parent node has a number i, then its right child has the number 2i+1 (2i+1<n). If 2i+1>n then i has no right child. If a child node (left or right) has a number i then the parent node has the number i /2 if i 1. If i =1 then i is the root and hence has no parent.
Representation Of Binary Trees Array Representation 10 5 4 2 1 a c b e d f 10 a b c d e f 1 2 3 4 5 6 7 8 9 11 11
Linked representation b d c e f LCHILD DATA RCHILD
Binary Tree Traversal A binary tree traversal requires that each node of the tree be processed once and only once in a predetermined sequence. In the depth-first traversal processing process along a path from the root through one child to the most distant descendant of that first child before processing a second child.
Pre order/In Order/Post Order traversals Pre Order: Contents of the root are printed, followed by the left subtree and then the right subtree (Visit – left – right) In Order: Left sub-tree traversed first before visiting the parent, followed by traversal of the right sub-tree (left - Visit – right) Post Order: Contents of left subtree are printed first, followed by right subtree and finally the root node (left - right – Visit) f j c h k a d i
Threaded Binary Trees A.J. Perlis and C.Thornton devised a prudent method to utilize the (n+1) empty pointers, introducing what are called threads. For a node NODE if RCHILD(NODE) is NIL then the null pointer is replaced by a thread which points to the node which would occur after NODE when the binary tree is traversed in inorder. Again if LCHILD (NODE) is NIL then the null pointer is replaced by a thread to the node which would immediately precede NODE when the binary tree is traversed in inorder.
A B F G C D E Inorder Traversal: G F B A D C E LEFT THREAD TAG LCHILD DATA RCHILD RIGHT THREAD TAG (True or False)
(a) Empty threaded binary tree (b) Non empty threaded binary tree true false LCHILD DATA RCHILD (a) Empty threaded binary tree T f A B t F C D E G (b) Non empty threaded binary tree LEFT THREAD TAG RIGHT THREAD TAG Head node f : false t : true
Applications Expression Trees ((A+B)*C-D) G Traversals of an expression tree Conversion of Infix Expression to Postfix Expression