MC0068 - Set 2
MC0068 - Set 2
Example :
A strictly binary tree in which the number of nodes at any level i is 2 i-1,
then the tree is said to be a complete binary tree. The tree shown in
figure below is a strictly binary tree and at the same time it is a complete
binary tree.
Example:
……………………………………
……………………………………
……………………………………
It is clear from the figure that all the nodes in the final level d are
leaf nodes and the total number of leaf nodes is 2d. In a complete binary
tree, we can easily find the number of non-leaf nodes. Now, let us find
the number of non-leaf nodes in a complete binary tree. Total number of
nodes in the complete binary tree =
2° + 21 + 22 + ………….2d.
Summation of this series is given by
S = a( rn- 1) 1( r- 1)
where a = 1, n = d+ 1 and r = 2
Nodes at level d are all leaf nodes. So, number of non-leaf nodes is given
by 2d+1 – 1 –2d which is equal to 2d – 1.
A) Insertion
Consider the BST shown in above figure. Suppose the node pointed
to by temp (with item 140) has to be inserted. The item 140 is compared
with root node i.e., 100. Since it is greater, the right subtree of root node
has to be considered. Now compare 140 with 200 and since it is less,
consider the left subtree of the node 200. Now, compare 140 with 150.
Since it is less, consider the left subtree of node 150. Since, left subtree
of node 150 empty, the node containing the Item 140 has to be inserted
towards left of a node 150. Thus, to find the appropriate place and insert
an item, the search should start from the root node. This is achieved by
using two pointer variables prev and cur. The pointer variable prev
always points to parent of cur node. Initially cur points to root node and
prev points to NULL i.e.,
prev = NULL
cur = root
Now, as long as the item is less than info (cur), keep updating the
node pointed to by the pointer variable cur towards left. Otherwise,
update towards right. The pointer variable prev always points to the
parent node and cur points to the child node. Once cur points to NULL,
insert the node temp towards left (prev) if item is less than info (prev),
otherwise insert towards right. The code corresponding to this can be
prev = NULL;
cur = root;
/* find the position where to insert */
while ( cur != NULL )
{
prev = cur;
cur = ( item < cur->info ) ? cur->llink : cur->rlink;
}
if ( item < prev->info)
prev->llink = temp;
else
prev->rlink = temp;
The above steps can be executed provided the tree exists. If the tree
is empty initially, then make the node pointed to by temp itself as root
node. The complete C function to insert an item into a binary search tree
is shown in below example
B) Searching
Start searching from the root node and move downward towards left
or right based on whether the item lies towards left subtree or right
subtree. This is achieved by comparing this item with root node of an
appropriate subtree (left subtree or right subtree). If two items are same
then search is successful and address of that node is returned. If the
item is less than info (root), search in the left subtree, otherwise search
in the right subtree. If item is not found in the tree, finally root points to
NULL and return root indicating search is unsuccessful. The iterative
function to search for this item is shown in below example.
void main()
{ clrscr();
int a[50],n,p,s,bsearch(int [],int,int);
void bubsort(int a[],int n)
cout<<"Enter the no. of elements: ";cin>>n;
cout<<"Enter the array...\n";
for (int i=0;i<n;i++) cin>>a[i];
cout<<"Enter the no. to be searched: ";cin>>s;
bubsort(a,n);
p=bsearch(a,n,s);
if (p==-1) cout<<"Sorry, element not found";
else cout<<s<<" was found at position "<<p+1;
getch();
}
void sort(int a[],int n)
{ int t;
for (int i=0;i<n;i++)
{ for (int j=0;j<(n-1)-i;j++)
{ if (a[j]>a[j+1])
{ t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
Example : Sort the following list using the insertion sort method:
Thus to find the correct position search the list till an item just greater
than the target is found. Shift all the items from this point one, down the
list. Insert the target in the vacated slot.
6. With your own example explain breadth first
traversal technique, and analyze its complexity.
Ans –
2. We then loop, dequeuing the queue and processing the vertex from
the front of the queue. After processing the vertex, we place all of its
adjacent vertices into the queue. Thus, at Step 2 in Figure 7.8(b]), we
dequeue vertex X, process it, and then place vertices G and H in the
queue. We are then ready for Step 3, in which we process vertex G.
Proof: The tree rooted at any node x contains at least 2 bh(x) – 1 internal
nodes.
Base case: If the height of x is zero, then x must be a leaf and the tree
rooted at x contains at least 20 – 1 = 0 internal nodes.
Ans –
A) Sequential files
(2) it provides fast access to the next record using lexicographic order.
Its disadvantages:
An inverted file is a file structure in which every list contains only one
record. Remember that a list is defined with respect to a keyword K, so
every K-list contains only one record. This implies that the directory will
be such that ni = hi for all i, that is, the number of records containing Ki
will equal the number of Ki-lists. So the directory will have an address for
each record containing Ki . For document retrieval this means that given
a keyword we can immediately locate the addresses of all the documents
containing that keyword. For the previous example let us assume that a
non-black entry in the field corresponding to an attribute indicates the
presence of a keyword and a black entry its absence. Then the directory
will point to the file in the way shown in Figure 6.3. The definition of an
inverted files does not require that the addresses in the directory are in
any order. However, to facilitate operations such as conjunction (’and’)
and disjunction (’or’) on any two inverted lists, the addresses are
normally kept in record number order. This means that ‘and’ and ‘or’
operations can be performed with one pass through both lists. The
penalty we pay is of course that the inverted file becomes slower to
update.