KEMBAR78
1.1 binary tree | PPT
Binary Tree
Definition - Operations -
Representations
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
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.
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.
Applications of Binary tree
• Expression Evaluation (Expression tree)
• Data Searching
• Data sorting
Representation of Binary Trees
Array Representation
Linked Representation
Threaded Representation
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
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
Traversal Operation - Examples

Inorder Traversal : 1 3 5 6 8 9 10
Preorder Traversal : 6 3 1 5 9 8 10
6
3 9
8 1051
NODE DECLARATION
typedef struct treeNode
{
int data;
struct treeNode *left;
struct treeNode *right;
}treeNode;
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;
}
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 */
CONTD..
  if(node->right && node->left)
                {
                        /* Here we will replace with minimum element in the right sub tree */
                        temp = FindMin(node->right);
                        node -> data = temp->data; 
                        /* As we replaced it with some other node, we have to delete that node */
                        node -> right = Delete(node->right,temp->data);
                }
                else
                {
                        /* If there is only one or zero children then we can directly 
                           remove it from the tree and connect its parent to its child */
                        temp = node;
                        if(node->left == NULL)
                                node = node->right;
                        else if(node->right == NULL)
                                node = node->left;
                        free(temp); /* temp is longer required */ 
                }
        }
        return node;
}
FINDMIN ROUTINE
treeNode* FindMin(treeNode *node)
{
        if(node==NULL)
        {
                /* There is no element in the tree */
                return NULL;
        }
        if(node->left) /* Go to the left sub tree to find the min 
element */
                return FindMin(node->left);
        else 
                return node;
}
                                                                                               
How many squares can you create in this figure by connecting any 4 dots (the corners of a 
square must lie upon a grid dot?
TRIANGLES: 
How many triangles are located in the image below?
There are 11 squares total; 5 small, 4 medium, and 2 large.
27 triangles.  There are 16 one-cell triangles, 7 four-cell triangles, 3 nine-cell 
triangles, and 1 sixteen-cell triangle.
GUIDED READING
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
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
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
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
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

1.1 binary tree