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 subtree
right(p)
Returns a pointer to the right subtree
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
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
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 twoway 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.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '10
 Dr.Naveed Malik
 Data Structures, Madrid Metro

Click to edit the document details