{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS301-Lec13 handout

# CS301-Lec13 handout - CS301 Data Structures Lecture No 13...

This preview shows pages 1–3. Sign up to view the full content.

CS301 – Data Structures Lecture No. 13 ___________________________________________________________________ Data Structures Lecture No. 13 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 4 4.3, 4.6 Summary Cost of Search Binary Search Tree (BST) Traversing a Binary Tree C++ code Example Exercise In the previous lecture, we had written and demonstrated the use of C++ code of insert routine for a tree through an example. We also saw how a new node is inserted into a binary tree. If the to-be-inserted number (node) is already in the tree i.e. it matches a number already present in the tree, we display a message that the number is already in the tree. In the last lecture, the advantages of the tree data structure vis-à- vis linked list data structure were also discussed. In a linked list, a programmer has to search the whole list to find out a duplicate of a number to be inserted. It is very tedious job as the number of stored items in a linked list is very large. But in case of tree data structure, we get a dynamic structure in which any number of items as long as memory is available, can be stored. By using tree data structure, the search operation can be carried out very fast. Now we will see how the use of binary tree can help in searching the duplicate number in a very fast manner. Cost of Search Consider the previous example where we inserted the number 17 in the tree. We executed a while loop in the insert method and carried out a comparison in while loop. If the comparison is true, it will reflect that in this case, the number in the node where the pointer p is pointing is not equal to 17 and also q is not NULL. Then we move p actually q to the left or right side. This means that if the condition of the while loop is true then we go one level down in the tree. Thus we can understand it easily that if there is a tree of 6 levels, the while loop will execute maximum 6 times. We conclude from it that in a given binary tree of depth d, the maximum number of executions of the while loop will be equal to d . The code after the while loop will do the process depending upon the result of the while loop. It will insert the new number or display a message if the number was already there in the tree. Page 1 of 11

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

View Full Document
CS301 – Data Structures Lecture No. 13 ___________________________________________________________________ Now suppose we have another method find. This method does not insert a new number in the tree. Rather, it traverses a tree to find if a given number is already present in the tree or not. The tree which the find method traverses was made in such an order that all the numbers less than the number at the root are in the left sub-tree of the root while the right sub-tree of the root contains the numbers greater than the number at the root. Now the find method takes a number x and searches out its duplicate in the given tree. The find method will return true if x is present in the tree. Otherwise, it will return false. This
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 11

CS301-Lec13 handout - CS301 Data Structures Lecture No 13...

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

View Full Document
Ask a homework question - tutors are online