2. Binary Tree and Complete Binary Tree
Binary Tree(BT) is a tree in which each node contains at most two
child nodes(left child and right child).
A complete binary tree is a binary tree in which every node
contains exactly two child nodes except the leaf nodes. Figure 1
shows a tree which is not a binary tree since node ‘3’ contains three
child nodes. Figure 2 shows an example binary tree and Figure 3
shows a complete binary tree
3
3 3
3 33
3
3 3
3 3
3
3 3
3 333
Figure 1 Figure 2 Figure 3
3. Types of Binary Tree
Ordered Binary Tree: In an ordered binary tree, the left
child node will always be less than its parent and right child
node will greater than its parent. It is also known as Binary
Search Tree.
Unordered Binary Tree: Unordered Binary tree does not
follow any such ordering.
An example of ordered binary tree is given below.
6
3 9
8 1051
In the rest of the slides we only
focus on ordered binary tree since it
has many applications including
finding duplicates, data sorting and
searching.
4. Operations on Binary tree
Traversals
•Traversal refers to visiting all the nodes of a binary
tree exactly once in a systematic way.
Insertion
•Refers to inserting a new element into the tree
Deletion
•Refers to removing an element from the tree
Searching
•Search operation checks whether the given data is
present in the tree or not.
5. Applications of Binary tree
• Expression Evaluation (Expression tree)
• Data Searching
• Data sorting
6. Representation of Binary Trees
Array Representation
Linked Representation
Threaded Representation
7. Array Representation
• A binary tree may be represented using an array.
• The key concept is that, if a parent is stored in location
k, then its left and right child are located in locations 2k
and 2k+1 respectively.
• An Example tree and its array representation is given
below.
6
3 9
8 1051
Location 1 2 3 4 5 6 7
Element 6 3 9 1 5 8 10
8. Traversal Operations
Pre-order Traversal
1. Process the root node
2. Perform preorder traversal of left subtree
3. perform preorder traversal of right subtree
In-order Traversal
1.Perform Inorder traversal of left subtree
2.Process the root node
3.Perform Inorder traversal of right subtree
Post-order Traversal
1.Perform Postorder traversal of left subtree
2.Perform Postorder traversal of right subtree
3.Process the root node
11. INSERTION ROUTINE
treeNode * Insert(treeNode *node,int data)
{
if(node==NULL)
{
treeNode *temp;
temp = (treeNode *)malloc(sizeof(treeNode));
temp -> data = data;
temp -> left = temp -> right = NULL;
return temp;
}
if(data >(node->data))
{
node->right = Insert(node->right,data);
}
else if(data < (node->data))
{
node->left = Insert(node->left,data);
}
/* Else there is nothing to do as the data is already in the tree. */
return node;
}
12. DELETION ROUTINE
treeNode * Delete(treeNode *node, int data)
{
treeNode *temp;
if(node==NULL)
{
printf("Element Not Found");
}
else if(data < node->data)
{
node->left = Delete(node->left, data);
}
else if(data > node->data)
{
node->right = Delete(node->right, data);
}
else
{
/* Now We can delete this node and replace with either minimum element
in the right sub tree or maximum element in the left subtree */
19. ASSESSMENT
1. A binary tree whose every node has either
zero or two children is called
A. Complete binary tree
B. Binary search tree
C. Extended binary tree
D. None of above
20. Contd..
2. The depth of a complete binary tree is
given by
A. Dn = n log2n
B. Dn = n log2n+1
C. Dn = log2n
D. Dn = log2n+1
21. Contd..
3.The post order traversal of a binary tree is
DEBFCA. Find out the pre order traversal
A. ABFCDE
B. ADBFEC
C. ABDECF
D. ABDCEF
22. Contd..
4.In a binary tree, certain null entries are
replaced by special pointers which point to
nodes higher in the tree for efficiency. These
special pointers are called
A. Leaf
B. branch
C. path
D. thread
23. Contd..
5. The in-order traversal of tree will yield a
sorted listing of elements of tree in
A. Binary trees
B. Binary search trees
C. Heaps
D. None of above