CS301-Lec29 handout - CS301 Data Structures Lecture No. 29...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 29 ___________________________________________________________________ Data Structures Lecture No. 29 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 6 6.3 Summary Complete Binary Tree Heap Max Heap Insertion in a Heap Complete Binary Tree In the previous lecture, we talked about the ways to store a complete binary tree in an array. The 2i and 2i+1 scheme were employed to store the link of the parent to children and link of children to parent. Through this link, a programmer can go to the children of a node. We know that array is a very efficient data structure. In a number of languages, it is found as a built-in data type. Now the question arises if we can store the binary tree in an array, why there should be the use of pointers? It is very simple that an array is used when the tree is a complete binary tree. Array can also be used for the trees that are not complete binary trees. But there will be a problem in this case. The size of the array will be with respect to the deepest level of the tree according to the traversal order. Due to incomplete binary tree, there will be holes in the array that means that there will be some positions in the array with no value of data. We can understand it with a simple example. Look at the following figure where we store a complete binary tree in an array by using 2i and 2i+1 scheme. Here we stored the nodes from A to J in the array at index 1 to 10 respectively.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS301 – Data Structures Lecture No. 29 ___________________________________________________________________ Suppose that this tree is not complete. In other words, B has no right subtree that means E and J are not there. Similarly we suppose that there is no right subtree of A. Now the tree will be in the form as shown in the following figure (29.2). 8 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 A C B E I D H J G F A B C D E F G H I J 1 2 3 4 5 6 7 9 10 Figure 29.1: Complete Binary Tree 8 14 1 2 3 4 5 6 7 8 9 10 11 12 13 0 A B I D H A B D H I 1 2 4 9 Figure 29.2: Not a complete binary tree
Background image of page 2
CS301 – Data Structures Lecture No. 29 ___________________________________________________________________ In this case, the effort to store this tree in the array will be of no use as the 2i and 2i+1 scheme cannot be applied to it. To store this tree, it may be supposed that there are nodes at the positions of C, F, G, E and J (that were there in previous figure). Thus we transform it into a complete binary tree. Now we store the tree in the array by using 2i and 2i +1 scheme. Afterwards, the data is removed from the array at the positions of the imaginary nodes (in this example, the nodes are C, F, G, E and J). Thus we notice that the nodes A, B and H etc are at the positions, depicting the presence of a complete binary tree. The locations of C, f, G, E and J in the array are empty as shown in the following figure. Now imagine that an incomplete binary tree is very deep. We can store this tree in the
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/21/2010 for the course CS CS301 taught by Professor Dr.naveed malik during the Spring '10 term at Virtual University of Pakistan.

Page1 / 15

CS301-Lec29 handout - CS301 Data Structures Lecture No. 29...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online