CS301-Lec12 handout - CS301 Data Structures Lecture No. 12...

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

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 12 ___________________________________________________________________ Data Structures Lecture No. 12 Reading Material Data Structures And Algorithm analysis in C++ Chapter 4 4.2, 4.3(4.3.2, 4.3.4) Summary Operations on Binary Tree Applications of Binary Tree Searching for Duplicates C++ Implementation of Binary Tree Trace of insert Operations on Binary Tree In the last lecture, we talked about the uses of binary tree, which is an abstract data type. We discussed about the functions to find the information inside a node, the parent, the siblings(brothers), the left and right children of a node. In this lecture, we will talk about the algorithms and implementation of those functions. When we discuss about an abstract data type, firstly we focus what it can do for us and don’t bother about the how part. The how part or implementation is thought out later. While implementing these abstract data types, the implementation is hidden in a class so it is abstract to the user. The user only needs to be aware of the interface. But there can be situations when the users may like to know about the implementation detail, for example, when a data type is performing quite slower than promised. For now, we start our discussion from the methods of tree data type. Consider a tree has been formed already, following methods will be used to perform different operations on a node of this tree: Operation Description left(p) Returns a pointer to the left sub-tree right(p) Returns a pointer to the right sub-tree parent(p) Returns the father node of p brother(p) Returns the brother node of p info(p) Returns the contents of node p Page 1 of 13
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. 12 ___________________________________________________________________ These methods have already been discussed at the end of the previous lecture, however, few more methods are required to construct a binary tree: Operation Description setLeft(p, x) Creates the left child node of p and set the value x into it. setRight(p, x) Creates the right child node of p , the child node contains the info x . All these methods are required to build and to retrieve values from a tree. Applications of Binary Tree Let’s take few examples to understand how the tree data type is used and what are its benefits. We will also develop some algorithms that may by useful in future while working with this data type. Binary tree is useful structure when two-way decisions are made at each point. Suppose we want to find all duplicates in a list of the following numbers: 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 This list may comprise numbers of any nature. For example, roll numbers, telephone numbers or voter’s list. In addition to the presence of duplicate number, we may also require the frequency of numbers in the list. As it is a small list, so only a cursory view may reveal that there are some duplicate numbers present in this list. Practically, this list can be of very huge size ranging to thousands or millions.
Background image of page 2
Image of page 3
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 / 13

CS301-Lec12 handout - CS301 Data Structures Lecture No. 12...

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

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