Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
Page 2 of 11
While discussing the search procedure, the tree for search was built in a specific order.
The order was such that on the addition of a number in the tree, we compare it with a
node. If it is less than this, it can be added to the left sub-tree of the node. Otherwise, it
will be added on the right sub-tree. This way, the tree built by us has numbers less than
the root in the left sub-tree and the numbers greater than the root in the right sub-tree.
A binary tree with such a property that items in the left sub-tree are smaller than the
root and items in the right sub-tree are larger than the root is called a binary search
tree (BST). The searching and sorting operations are very common in computer
science. We will be discussing them many times during this course. In most of the
cases, we sort the data before a search operation. The building process of a binary
search tree is actually a process of storing the data in a sorted form. The BST has many
variations, which will be discussed later. The BST and its variations play an important
role in searching algorithms. As data in a BST is in an order, it may also be termed as
ordered tree.
Now lets discuss the ways to print the numbers present in a BST. In a linked list, the
printing of stored values is easy. It is due to the fact that we know wherefrom, a
programmer needs to start and where the next element is. Equally is true about printing
of the elements in an array. We execute a for loop starting from the first element (i.e.
index 0) to the last element of the array. Now lets see how we can traverse a tree to
print (display) the numbers (or any data items) of the tree.
We can explain this process with the help of the following example in which we
traverse a binary search tree. Suppose there are three nodes tree with three numbers
stored in it as shown below.
14
15
Page 3 of 11
node
R
left
right
subtre
e
subtree
Page 4 of 11
C++ code
Lets write the C++ code for it. Following is the code of the preorder method.
void preorder(TreeNode<int>* treeNode)
{
if( treeNode != NULL )
{
cout << *(treeNode->getInfo())<<" ";
preorder(treeNode->getLeft());
preorder(treeNode->getRight());
}
}
In the arguments, there is a pointer to a TreeNode. We may start from any node and the
pointer of the node will be provided as argument to the preorder method. In this
method, first of all we check whether the pointer provided is NULL or not. If it is not
NULL, we print the information stored in that node with the help of the getInfo()
method. Then we call the getLeft() method that returns a pointer of left node, which
may be a complete subtree. With the help of this method, we get the root of that
subtree. We call the preorder method again passing that pointer. When we return from
that, the preorder method is called for the right node. Lets see what is happening in
this method. We are calling the preorder method within the preorder method. This is
actually a recursive call. Recursion is supported in C++ and other languages.
Recursion means that a function can call itself. We may want to know why we are
doing this recursive call. We will see some more examples in this regard and
Page 5 of 11
Page 6 of 11
Example
Lets have a look on the following tree.
Page 7 of 11
15
9
7
18
16
20
17
Fig 13.3:
20
Preorder: 14 4 3 9 7 5 15 18 16 17
This is the same tree we have been using previously. Here we want to traverse the tree.
In the bottom of the figure, the numbers are printed with the help of preorder method.
These numbers are as 14 4 3 9 7 5 15 18 16 17 20. Now take these numbers and
traverse the tree. In the preorder method, we print the root, followed by traversing of
the left subtree and the right subtree respectively. As the value of the root node is 14,
so it will be printed first of all. After printing the value of the root node, we call the
preorder for the left node which is 4. Forget the node 14 as the root is 4 now. So the
value 4 is printed and we call the preorder again with the left sub tree i.e. the node with
value 3. Now the root is the node with value 3. We will print its value before going to
its left. The left side of node with value 3 is NULL. Preorder will be called if condition
is false. In this case, no action will be taken. Now the preorder of the left subtree is
finished. As the right subtree of this node is also NULL, so there is no need of any
action. Now the left subtree of the node with value 4 is complete. The method
preorder will be called for the right subtree of the node with value 4. So we call the
preorder with the right node of the node with value 4. Here, the root is the node with
value 9 that is printed. We will call its left subtree where the node value is 7. It will be
followed by its left subtree i.e. node 5 which will be printed.
In the preorder method, we take the root i.e. 14 in this case. Its value is printed,
followed by its left subtree and so on. This activity takes us to the extreme left node.
Then we back track and print the right subtrees.
Lets try to understand the inorder method from the following statement.
Page 8 of 11
15
9
7
18
16
20
17
Fig 13.4:
20
Inorder: 3 4 5 7 9 14 15 16 17 18
Page 9 of 11
Parameters(F)
Local variables(F)
Local variables(F)
Local variables(F)
Return address(F)
sp
Parameters(F)
Return address(F)
Parameters(F)
Parameters(F)
Return address(F)
sp
Local variables(F)
sp
At point of call
Return address(F)
During execution of F
After call
Page 10 of 11
Exercise
Please find out the preorder, inorder and postorder traversal of the tree given below:
40
14
50
45
10
19
80
42
12
11
15
60
46
17
44
16
55
200
70
Fig 13.6
Page 11 of 11