960 likes | 1.01k Views
Notes for students of University Women's Polytechnic, AMU
E N D
UNIT IV UNIT - IV Non Linear Data Structures Non Linear Data Structures TREES & GRAPHS TREES & GRAPHS TREES & GRAPHS TREES & GRAPHS
TREES TREES A Binary Tree T is define as a finite set of elements called nodes such that: T is empty (called the null tree or empty tree) or T contains a distinguished node R, called the root of T and the remaining nodes of T form an ordered pair of disjoint binary trees T1 and T2 T2. If T contains a root R then T1 and T2 are called respectively subtrees of R. the left and right 18-11-2019 Jahangir Alam 2
If T1 is non empty then its root is called the left successor of R. Similarly if T2 is the left successor of non-empty then its root is called right successor of R. Similarly is A binary tree is frequently represented by means of diagram. Following example discusses the anatomy of discusses the anatomy of a Binary Tree (T): Binary Tree T consists of 11 nodes (A – L excluding I) 18-11-2019 Jahangir Alam 3
A B C D E G H J K F L Fig 1: Binary Tree
The root of tree is A at the top of diagram. A left-downward slanted line from a node N indicates a left successor of N and a right- downward slanted line indicates a right successor of N from a node N B is left successor and C is right successor of node A. The left subtree of the root A consists of the nodes B, D, E and F, and the right subtree of A consists of the nodes C, G, H, J, K and L. 18-11-2019 Jahangir Alam 5
Any node N in a binary tree T has either 0 (e.g. nodes D, F, G, L and K), 1 e.g. nodes E and J) or 2 (e.g. nodes A, B, C and H) successors. The nodes with no successors are called terminal nodes. Binary similar if they have the same structure or in other words if they have the same shape. Binary trees T1 and T2 are said to be 18-11-2019 Jahangir Alam 6
The trees are said to be copies if they are similar and if they have the same contents similar and they have the same contents at corresponding nodes. Examples: 7.1 and 7.2 at Page 215, Schaum’s Outline Series. 18-11-2019 Jahangir Alam 7
Terminology Terminology Terminology relationships is frequently used to describe relationships is frequently used to describe relationships between the nodes of tree T. describing family Suppose N is a node in T with left successor S1 and right successor S2, then N is called the parent (or father) of S1 and S2 and S2. S1 is called the left child (or son) of N and S2 is called the right child (or son) of N. 18-11-2019 Jahangir Alam 8
S1 and S2 are said to be siblings (or brothers). Every node N in a binary tree T, except the root, has a unique parent called the predecessor of N. A node L is called descendent of a node N (and N is called an ancestor of L) if there is a succession of children from N to L is a succession of children from N to L. In particular L is called a left or right descendent of N according to whether L belongs to the left or right subtree of N. Every node N in a binary tree T, except the 18-11-2019 Jahangir Alam 9
Terminology horticulture is also used with a binary tree horticulture is also used T. from graph theory a binary tree and Specifically, the line drawn from a node N of T to a successor is called an edge and a sequence of consecutive edges is called path a path. A terminal node is called a leaf and a path ending in a leaf is called a branch. 18-11-2019 Jahangir Alam 10
The depth (or height) of a tree T is the maximum number of nodes in a branch of T T. Each node in a binary tree T is assigned a level number as follows. The root R of T is assigned the level number 0 and every other node is assigned a level number which is one more than the level number which is one more than the level number of its parent. Nodes with the same level number are said to belong to same generation. 18-11-2019 Jahangir Alam 11
Complete Binary Trees Complete Binary Trees Each node of a binary tree T can have at the most two nodes. So, it is evident that level r of T can have at the most 2rnodes. The tree T is said to be complete if all its levels, except possibly the last, have the maximum number of possible nodes, and if all the nodes at the last level the last level appear as far left as possible. Thus there is a unique complete tree Tnwith exactly n nodes. Following complete T14with 14 nodes. far left possible figure shows 18-11-2019 Jahangir Alam 12
1 LEVEL 0 3 2 LEVEL 1 7 6 4 LEVEL 2 5 14 LEVEL 3 12 13 9 8 10 11 Fig 2:Complete T14
The depth Dnof a Complete Binary Tree, with n nodes is given by: nodes is given by log 2 Dn 1 n 18-11-2019 Jahangir Alam 14
Representation of Binary Trees in Memory Representation of Binary Trees in Memory BINARY TREE LINKED SEQUENTIAL REPRESENTATION REPRESENTATION 18-11-2019 Jahangir Alam 15
Linked Representation Linked Representation Linked representation of tree T uses the following: Three parallel arrays INFO, LEFT & RIGHT A pointer variable ROOT INFO[K] – Contains the data at the node N. LEFT[K] – Contains the location of the left child of node N. RIGHT[K] – Contains the location of the right child of node N. ROOT contains the location of the root node R of T. 18-11-2019 Jahangir Alam 16
If any subtree is empty then the corresponding pointer will contain the null value (X) value (X). If the tree T itself is empty, then ROOT itself will contain null value (X). Fig. 3 shows the linked representation of the tree shown in Fig. 1. Fig 4 shows how the linked representation may appear in memory. AVAIL list keeps track of blank records. It has been maintained using array LEFT. Fig. 4 shows how the linked representation 18-11-2019 Jahangir Alam 17
ROOT ROOT A A B B C C X X G G X X H H X X D D X X E E X X J J X X X X F F X X X X K K X X X X X X L L L L X X X X Fig 3: Linked Representation of Fig.1 Tree Fig 3: Linked Representation of Fig.1 Tree
ROOT INFO LEFT RIGHT 5 1 K 0 0 2 C 3 6 3 G 0 0 AVAIL 4 14 5 8 A 10 2 6 H 17 1 7 L 0 0 8 9 9 4 10 B 18 13 11 19 12 F 0 0 13 E 12 0 14 15 15 16 16 11 17 J 7 0 18 D 0 0 19 20 20 0 Fig 4: Memory Map for Linked Representation of Fig.1 Tree
Sequential Representation Sequential Representation Suppose T is a binary tree that is complete or nearly complete. Then there is an efficient way of maintaining T in memory called the sequential representation. This representation uses only a single array TREE as follows: The root R of T is stored in TREE[1] The root R of T is stored in TREE[1] If a node N occupies TREE[K] then its left child is stored in TREE[2*K] and its right child is stored in TREE[2*K+1]. 18-11-2019 Jahangir Alam 20
Null is used to indicate an empty subtree. If TREE[1]=Null then the tree is empty. Fig shows 5 shows a tree representation: Fig. and and its its sequential sequential We require 14 locations in the array TREE for 9 nodes of the given tree. If we include null entries for the successors of terminal nodes then we would actually require terminal nodes then we would actually require TREE[29] for right successor of TREE[14]. In general the sequential representation of with depth d will require an array with approximately 2d+1elements. a tree 18-11-2019 Jahangir Alam 21
TREE 45 1 2 3 4 5 45 22 77 11 30 22 77 6 11 30 90 7 8 9 90 90 15 25 10 11 12 13 88 25 15 14 88 15 16 . . . 29 Fig 5: Sequential Representation of Given Tree
TR EE 45 22 77 11 30 90 15 25 88 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 18-11-2019 Jahangir Alam 23
Clearly usually inefficient unless the binary tree is complete or nearly complete. this sequential representation is 18-11-2019 Jahangir Alam 24
Traversing Binary Trees Traversing Binary Trees There traversing a binary tree T with root R. traversing a binary tree are three standard ways of root These three algorithms are: Preorder Traversal Inorder Traversal Postorder Traversal Following table explains each of them: 18-11-2019 Jahangir Alam 25
Preorder 1. Process the root R 2. Traverse the left subtree of R in preorder 3. Traverse the left subtree of R in preorder preorder 1.Traverse the left subtree of R in inorder 2.Process the root R 3.Traverse the right subtree of R in inorder Inorder Postorder 1. Traverse the left subtree of R in postorder postorder 2. Traverse the right subtree of R in postorder 3. Process the root R
Preorder: ABDECF A B C Inorder: DBEACF D E F Postorder: DEBFCA Right Subtree Left Subtree Fig: 6 Traversal Algorithms on given graph
Assignment Let T, be a given binary tree, then prove that: The maximum number of nodes at level i is 2ifor i > 0. The maximum number of all nodes in a tree of height h is 2h+1- 1. If a binary tree of height h has N nodes then h ≥ log2(N + 1) – 1 If a binary tree of height h has l leaves, then h ≥ log2l i. ii. iii. iv. 18-11-2019 Jahangir Alam 28
Heap Suppose H is a complete binary tree with n elements and is being maintained in the memory of computer using a linear array TREE in sequential fashion. Then H is called HEAP or a MAXHEAP if each node N of H satisfies the following property: The value at any node K of H is greater than or equal to the value of each children of K. Similarly a MINHEAP can be defined as reciprocal maxheap. 18-11-2019 Jahangir Alam 29
Example of a Heap (Maxheap) 97 88 95 66 55 95 48 66 35 62 77 48 55 25 38 18 40 30 26 24 TREE 97 88 95 66 55 95 48 66 35 48 55 62 77 25 38 18 40 30 26 24 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Fig 8: A Heap with its Sequential Representation 18-11-2019 Jahangir Alam 30
Insertions in a Heap Let H be a given Heap and ITEM be a given piece of information to be inserted to H. piece of information to be inserted to Then following steps insert ITEM into H: 1. Adjoin ITEM at the end of H so that H is still a complete binary tree but not necessarily a heap. 2. Then let ITEM to rise its “appropriate place” in H so that H is finally a Heap. 18-11-2019 Jahangir Alam 31
Exercise examples 7.20 and 7.21 at Page No. 244 – 245 of Schaums Outline Series. No 245 of Schaums Outline Series 18-11-2019 Jahangir Alam 32
Deleting the root of a Heap Suppose H is a Heap with N elements and we want to delete the root R of H. Then we want to delete the root following steps accomplish this: 1. Assign the root R to some variable ITEM. 2. Replace the deleted node R by the last node L of H so that H is still a complete tree but not necessarily a heap. 3. (Reheap) Let L sink to its appropriate place in H so that H is finally a Heap. of Then 18-11-2019 Jahangir Alam 33
Exercise examples 7.22 at Page No. 246 – 247 of Schaums Outline Series. of Schaums Outline Series 18-11-2019 Jahangir Alam 34
Applications of Heap: Heapsort Let A be a given array with N elements, then Heapsort algorithm for sorting A proceeds Heapsort algorithm for sorting as follows: 1. Build a heap H out of the elements of A. 2. Repeatedly delete and add to DECK (an array going to consist sorted array) the root node of H until H becomes empty. Note: The average complexities of hepsort are O(nlog2n). proceeds and worst case 18-11-2019 Jahangir Alam 35
Path Lengths & Huffman’s Algorithm Extended Binary Tree or 2 – Tree: It is a Binary Tree in which each node has either 0 or 2 children. It is a Binary Tree in which each node The nodes with 0 children are called External Nodes and the nodes with 2 children are called Internal Nodes. Following figure shows an Extended Binary Tree (2 – Tree): 18-11-2019 Jahangir Alam 36
R : External Nodes : Internal Nodes Fig 9: An Extended Binary Tree or 2 - Tree 18-11-2019 Jahangir Alam 37
In the above 2-Tree note that: No of Internal Nodes (NI) = 6 No of External Nodes (NE) = 7 Always remember that in a 2-Tree the number of External Nodes is always one more than the number of Internal Nodes i i.e. : NE= NI+ 1 No of Internal Nodes (NI) 6 18-11-2019 Jahangir Alam 38
External Path Length The external path length LEof a 2 Tree T is defined as the sum of 2-Tree T, is defined as the sum of all path lengths summed over each path from the root R of T to an external node. For the tree shown in Fig 9, LE= 2+2+3+4+4+3+3 = 21 18-11-2019 Jahangir Alam 39
Internal Path Length The internal path length LIof a 2-Tree T, is defined as the sum of all path lengths summed over each path from the root R of T to an internal node. For the tree shown in Fig 9, LI= 0+1+1+2+3+2 = 9 For any 2-Tree with m internal nodes we always have: LE= LI+ 2m 18-11-2019 Jahangir Alam 40
Weighted Path Length 18-11-2019 Jahangir Alam 41
An Example: 11 2 2 3 5 11 11 5 (a) T1 3 5 2 3 PT1 = 2.2+3.2+5.2+11.2 =42 (b) T3 (b) T2 PT3 = 5.2+2.3+3.3+11.1 =36 PT3 = 5.2+2.3+3.3+11.1 =36 PT2 = 11.2+3.3+5.3+1.2 =48 PT2 = 11.2+3.3+5.3+1.2 =48 Fig 10 18-11-2019 Jahangir Alam 42
Conclusions Drawn From Above Example: Different 2-trees produce different weighted path lengths with same weights assigned to path lengths external nodes. trees may weighted path lengths with same weights assigned to external nodes (e.g.T2 and T3) So, the real problem is “How to construct such a 2-Tree from given weights which produces minimum weighted path length?” weights assigned Similar produce different So, the real problem is “How to construct 18-11-2019 Jahangir Alam 43
Solution to this problem is given by Huffman so the Huffman so the constructed with given weights that produces minimum weighted path length is called Huffman’s Tree. The Huffman’s Algorithm proceeds as follows: 2-tree 2 tree to to be be The Huffman’s Algorithm proceeds 18-11-2019 Jahangir Alam 44
Huffman’s Algorithm: Input: Weights W1, W2, W3,…,Wn Output: A Huffman tree with weights at the External Nodes Algorithm: 1. Suppose w1and w2are two minimum weights among the n given weights w1, w2,….wn. Then, find a tree T’ which gives a solution for the n-1 weights ,w3,w4,…..wn. w1+ w2 2. Then in the tree T’, replace the external node by the subtree W1 W2 w1+ w2 3. The new 2-tree T is the desired solution. 18-11-2019 Jahangir Alam 45
18-11-2019 Jahangir Alam 46
Example 2: Suppose a set of 8 data items with their weights are given. Construct a 2-tree having minimum weighted path length using Huffman’s algorithm weighted path length using Huffman’s algorithm. Data Item: A B C D E F G H 22 5 11 19 2 11 25 5 Weights: 18-11-2019 Jahangir Alam 47
Answer: The Resulting 2 Answer: The Resulting 2- -tree with minimum weighted tree with minimum weighted- -path length path length 18-11-2019 Jahangir Alam 48
GRAPHS GRAPHS A graph ( or undirected graph) denoted as G = <V, E> consists of the following two things: things: A set of elements called nodes or vertices or points. A set E of edges such that each edge e in E is indentified with a unique (unordered) pair of nodes in V. For example e = <u, v>, e є E and nodes in V. For example e u, v є V. Consider the graph shown in fig. 7. For this graph V = {A, B, C, D} and E = {<A, B>, <B, C>, <C, D>, <D, A>, <B, D> <u, v>, e є E and 18-11-2019 Jahangir Alam 49
A D B C Fig: 11 Example Graph