Binary Tree
Properties of Binary Tree
Property 1:
In any binary tree, the maximum number of nodes on level l is 2l where l≥0
Proof:
- In binary tree , length of the binary tree is l . The root node contains any one node on level 0.
- Hence , the maximum number of nodes on level 0 is 1→2°=1
- The maximum number of nodes on level 1 is 2→2¹=2
- The maximum number of nodes on level 2 is 4→2²=4
- Same like these. The maximum number of nodes on level l=i is 2i⇒2l=2i
- Hence , the maximum number of nodes on level l is 2l.
Fig:
Property 2:
The maximum number of nodes possible in a binary tree of height h is 2h-1.
Proof:
- The maximum number of nodes in a binary tree is the total count of the maximum number of nodes in at each level.
- Then, the maximum number of nodes in a binary tree is height 'h' is:
n=2lmax+1-1/2-1
⇒n=2lmax+1-1
where, lmax is a maximum level of the tree.
Based on definition of height h, h=lmax+1
Then, n=2h-1
Property:3
The maximum number of nodes possible in a binary tree of height h is h.
Proof:
- A binary tree has the minimum number of nodes in each level.
- The minimum number of nodes possible at every level is only one node, when ever the parent has one child node. These kind of binary tree is called Skewed Binary Tree. Then , the number of nodes of a binary tree :
nmin=h
Fig:
Property 4:
For any non-empty binary tree, if n is the number of nodes and e is the number of edges .
Then, n=e+1
Proof:
- For n=1, e=0 then, n=e+1=0+1 ⇒n=1
- For n=2, e=1 then , n=e+1=1+1⇒n=2
- For n=3, e=2 then, n=e+1=2+1 ⇒n=3
.
.
.
.
- For n=n1, e=n1-1 then, n1=(n1-1)+1
- Let n1 be the number of nodes,
- Thus, n1=e1+1⇒e1=n1-1
- If we add one more node into the binary tree having n1 nodes, then it will increases one more edge in the binary tree, then
⇒n1+1=(e1+1) +1
⇒n1+1=((n1-1)+1) +1
⇒n1+1=(n1-1+1) +1
⇒n1+1=n1+1
The formula is true when for any number of nodes n, then it is also true for n+1
Hence, n=e+1 is true.
Property :5
For any non-empty binary tree T , if n0 is the number of leaf nodes (degree=0) and n2 is the number of internal nodes(degree=2), then n0=n2+1
Proof:
- Let n be the total number of nodes in binary tree T . ni be the number of nodes having degree i.
Since 0≤i≤2
we have,
⇒ n=n0+n1+n2 ........ (i)
If e is the number of edges in T , then
⇒ e=n0×0+n1×1+n2×2
⇒ e=n1+2n2 ......... (ii)
since , n =e+1 ................ (iii)
- For non-empty binary tree T, if n is number of nodes and e is number of edges.
Substitute eq. (ii) in eq(iii)
⇒n=n1+2n2+1 ...... (iv)
Since eq. (i) and eq. (iv) are equal . Then,
⇒n0+n1+n2=n1+2n2+1
⇒n0=1+2n2-n2 n0=n2+1
Property :6
The height of complete binary tree with n number of nodes is log2(n+1)
Proof:
Let 'h' be the height of the complete binary tree.
Since, n≤2°+2¹+2²+2³+.......... +2h-1
⇒n≤2h-1
⇒n+1≤2h
⇒ 2h≥n+1
Taking log on both sides, we get
⇒h≥log2(n+1)
⇒h=log2(n+1)
Property :7
The total number of binary trees possible with n nodes is 2nCn 1/(n+1) .
-
Operations of a Binary Tree
There are four major representations on a binary tree :
- Insertion
- Deletion
- Traversal
- Merge
(We will discuss insertion and deletion here)
Insertion:
- To perform insertion operation, first find out the key element is presented in the tree or not, by using searching operation. Key element is not presented , then perform the insertion operation.
Algorithm:
- Search for the key node in the tree.
- if (l=0) or (The key node is present) then
- printf "No insertion"
- Exist
- end if
- (A[2*l]=NULL) or (A[2*l+1=NULL)
- if (key node had empty links) then
- if (option=l) then
- if (left child is null) then
- insertion as a left child
- else
- printf "Node already has left child:No insertion"
- exit
- end if
- end if
- if(option=R ) then
- if(right child=NULL) then
- insert item as a right child
- else
- printf " node already has right child"
- exit
- end if
- end if
- else
- print "item cannot be inserted as a leaf node"
- end if
- stop
Note:To perform the operations on left child and right child use the structure format. Suppose parent i as a root node , then 2*i as a left child node and 2*i+1 as a right child node.
Example:The given list is 10,11,12,13.The root node is 10 already inserted into tree.
Step:1
To insert 11 data element as a leaf node in the tree after completion of searching operation . The element not in the binary tree . Then, insert the data item either left child or right child.
If (2*i==NULL) ⇒(2*i==0)
Then , binary tree (2*i) =11
Fig:11 is inserted as leaf node.
Step:2 To insert '12' as a link node after completion of step 1 . Inserted as right child of root node.
Binary tree(2*i+1) =12
Fig:12 is inserted as a right leaf node.
Step:3 To insert 13 as a link node after completion of step:2 inserted at right child of root node.
Binary tree[2*i+1]=13
Algorithms for search:
Steps
- i=Index
- if (A[i]≠key) then
- if (2*i≤size) then
- search SEQ(2*i, Key)
- else
- if (2*i+1≤size) then
- search SEQ(2*i+1, Key)
- else
- return(0)
- end if
- end if
- else
- return
- end if
- stop.
Deletion:
- This operation is used to delete any node from any non-empty binary tree.
- Here, we will consider the case of deletion of a leaf node in any binary tree.
- Deletion operation in various cases of binary trees will be discussed in due time.
Algorithm
Search
-
flag=FALSE
-
l=search SEQ(l, key)
-
if l=0 goto step10
-
if (A[2*l]=NULL) and (A[2*l+1]=NULL)
-
flag=TRUE
-
A[l]=NULL
-
else
-
print " The node containing ITEM is not a leaf"
-
end if
-
if(flag=FALSE)
-
print "node does not exist"
-
end if
-
stop.
Example: The binary tree is:
Solution:
Step:1 To delete the key element is '11' . Then, first find out the key element is presented or not in the binary queue.
To perform this operation, use the searching algorithm.
The key element '11' is located at index i=2 .
After finding, the key element ,must check if it is leaf node or not.
If (A[2*i]=NULL) and (A[2*i+1]=NULL)
The key element 11 is a leaf node , then delete it from the binary tree.
Step:2
After deletion, binary tree is as follows: