You are on page 1of 17
Terminologies : tree, degree of a node, degree of a tree, level of a node, leaf node. Depth/Height of a tree, In-degree and Out-Degree, Path, Ancestor and descendant nodes Tree Types and Traversal methods Types of Trees : General tree, Binary tree, Binary search tree (st). Binary tree traversal : In order traversal, Preorder traversal, Post order traversal Expression tree. = _ Introduction to Trees > (MSBTE : S-12, S-13, S-15, W-15, W-17) 3.6.1.1 What is tree ? (Refer Section 6.1) 2.8.1.2 Define the term tree (Refer Section 6.1) 2.6.1.3 Define tree, (Refer section 6.1) A tree is a collection of elements called “nodes”, one of wesch is distinguished as a root say r, along with a relation =srenthood” that places a hierarchical structure on the nodes. The t can have zero or more nonempty subtrees T,, T,, .... T, each # whose roots are connected by a directed edge from r. ‘The root of each subtree is said to be a child of r and r is rent of each subtree root. Fig. 6.1.1 is @ typical tree using the seoursive definition. Fig. 6.1.1: Generic tree Fig, 6.1.2: A tree 6.1.1 Basic Terms > (MSBTE : W-08, S-09, W-09, S-11, S-12, S-13, W-13, S-14, W-14, S-15, W-15, S-16, S-17, W-17) Q.6.1.4 Define the terms related to Binary Tree : Level, Depth, Leaf Node, Root Node. : (Refer Section 6.1.1) ‘(TEISE Describe in brief, the terms related to Binary Tree: Level, Depth, Leaf Node, Root Node, _ Siblings. (Refer Section 6.1.1) Q@.6.1.6 Explain the terms with the heip of diagram. {) Siblings (i) Leat Node (Refer Section 6.1.1) a. S17 Explain ny eight terms of tree termi ology. : (Refer Section 6.1.1) 0.6. 18 Define any. four terminologies related (Refer Section 6.74) a. 6 9 Detine the given'tree terminology (1) Degree of node (Refer Seétion 6.7, 4} = (2) Leaf node (Refer Section 6.1.4) @.6.1.10 Explain level of a node w.rt tree. -(Reter Section 6.1.1) 6.1.11 Explain height of a tree . 6.1. 12 Detine the flowing with respect 0 198: ‘ Depth Pte sectn 3.4 : (i) Dagiee (Refer section 6.1. : : o Path (Refer s83t006.1.4) Depth of tree (Rafer section 6 Degree of tree, (Refer section 6.1.7) [3 F Root: itis @ special node in a tree structure and the entire tee is referenced through it, This node does not have a Parent, In the tree of Fig, 6.1.2, the root is A. ® Parent: Itisan immediate predecessor of a node, In the Fig. 6.1.2, Ais the parent of B, C, D, ig 612A tee © Child : Allimmediate successors of anode are its children, In the Fig.6.1.2, B,C and D are the children of A. * Siblings : Nodes with the same parent are siblings. H, Land Jare siblings as they are children of the same parent D, ® Path ; Itis number of successive ‘edges from source node t destination node. The path length from node D to node N is Fig.6.1.2 is 3. Node D is connected to N through three edges D-H, H-M, M-N. Degree of a node The degree of a node is a number of children of a node, In Fig. 6.1.2, A and D are nodes of degree 3, B, F and M are nodes of degree 2, C and H are nodes of degree 1 and E, K. bc LN, O, I, Fare nodes of degree 0. A node of degree 0 is also kmown as the leaf node, A leaf node is a terminal node and it has no children. Leaf node A node of degree 0) is also known as a leaf node, A leaf node 18 4 terminal node and it has no children, In the Fig. 6.1.2 E, KL,G,N, 0,1, J are leaf nodes. © Level ‘The level of'a node in a binary tree is defined as 1. The root of the tree has level 0 2 The evel of any other node in the tree is one more than the level of its father ‘The levels of various nodes in the tree of Fig. 617 ic given below, E.F,G,HIS KLM NO 4 SS eee = Depth The depth of a binary node in the tree, tee is the maximum level of any ‘This is equal to the node in the tree. This is eqs] to the longest: path from the root tp any leaf node. The depth of the roe in Fig. 6.1.2 is 4. Fig 62:4 te @ Degree of a tree The degree of a tree is the maximum degree of a node in the tree, For example, the degree of the tree shown in the Fig. 6.1.2 is 3. The node D and node A have maximum degree and it is 3. © Depth/Height of a tree Height of a tree is the distance of the root node from its farthest descendant. Height of the tree shown in the Fig. 6.1.2 is 4. ‘The distance of the root node A from its farthest descendant N is 4. © Ancestor/descendant node Alll the nodes on the path leadning to root node are ancestors of a node. In the Fig. 6.1.2, ancestors of the node N are (M, H, D, A). all the nodes in the subtress of a node are its descendants, In the Fig, 6.1.2, the descendants of node D are (H, I, J, M, N, 0). Fig 62:4 te > (MSBTE : W-08, W-10, S-11, S-12, W-12, W-13, S-14, S-15, W-16, W-17) A tree is binary if each node of the tree can have maximum of two children. Moreover, children of a node of binary tree are ordered. One child is called the “left” child and the other is called 6 © the “right” child, An example of binary tree is shown in Fig. 6.2.1 Node A has two children B and C. Similarly, nodes B and C, each have one child namely G and D respectively. Representation of a Binary Tree using an Array > (MSBTE : W-10, W-13, S-16) “sequential 2.6.2.6 Describe (an... example oe eee eee - (Refer. Section 6.2.4) Ilene Dasonbe weieserinion of emer nae (OE | (Peter Section 6.2.1) = In order to represent a tree in a single one-dimensional arrny. the nodes are numbered sequentially level by level left jto right Even empty nodes are numbered. When the data of the tree is stored in an array then the number appearing against the nede will work as indices of the mode in an array. Location number zero of the array can be used to store the sine of the tree in terms of total number of nedes (existing or mot aeons Array representation of the given tree Step 1: Numbering of nodes : Fig, Ex, 6.2,2(a) - 2: — Representation 12.3 4 5 6 7 8 9 10 11 12 13 14 15 Efe [wl] Te Fig. Ex. 6.2.2(b) ked Representation of a Binary > (MSBTE : W-10, W-13) Linked representation of a binary tree is more efficient than aay representation. A node of. binary tree consists of three fields as shown below: = Data — Address of the let child = Address of the right child [te os [i Left and right are pointer type fields. Left holds the addr of eft child and right holds the address of right child. In “C” language, the following structure can be defined to represent one node of a binary tree. It is assumed that a node contains an integer data. typedef struct node ~ { intdata; struct node lefty Fig. 6.2.5 : A sample binary tree Fig. 6.2.6 ; Linked representation of the binary tree of Fig. 6.2.5 The level of root node is always 0. The immediate children of the root are at level | and their immediate children at level 2 and so on. Height of a node : Height of a node is the distance of the node from its farthest descendant. Height of the node C is 2 as its BF D,E, HG 0 Height of various nodes of the tree shown in Fig. 6.2.7. ‘Height of a tree : Height of a tree is height of the root node. Height of the tree shown in Fig. 6.2.7 is 3 as the height of the root neds A is 3. Fig. 6.2.7 : Levels

You might also like