# Handshaking Lemma and Interesting Tree Properties

**What is Handshaking Lemma?**

Handshaking lemma is about undirected graph. In every finite undirected graph number of vertices with odd degree is always even. The handshaking lemma is a consequence of the degree sum formula (also sometimes called the handshaking lemma)

**How is Handshaking Lemma useful in Tree Data structure?**

Following are some interesting facts that can be proved using Handshaking lemma.

**1) In a k-ary tree where every node has either 0 or k children, following property is always true.**

L = (k - 1)*I + 1 Where L = Number of leaf nodes I = Number of internal nodes

**Proof:**

Proof can be divided in two cases.

** Case 1 ** (Root is Leaf):There is only one node in tree. The above formula is true for single node as L = 1, I = 0.

** Case 2 **(Root is Internal Node): For trees with more than 1 nodes, root is always internal node. The above formula can be proved using Handshaking Lemma for this case. A tree is an undirected acyclic graph.

Total number of edges in Tree is number of nodes minus 1, i.e., |E| = L + I – 1.

All internal nodes except root in the given type of tree have degree k + 1. Root has degree k. All leaves have degree 1. Applying the Handshaking lemma to such trees, we get following relation.

Sum of all degrees = 2 * (Sum of Edges) Sum of degrees of leaves + Sum of degrees for Internal Node except root + Root's degree = 2 * (No. of nodes - 1) Putting values of above terms, L + (I-1)*(k+1) + k = 2 * (L + I - 1) L + k*I - k + I -1 + k = 2*L + 2I - 2 L + K*I + I - 1 = 2*L + 2*I - 2 K*I + 1 - I = L (K-1)*I + 1 = L

So the above property is proved using Handshaking Lemma, let us discuss one more interesting property.

**2) In Binary tree, number of leaf nodes is always one more than nodes with two children.**

L = T + 1 Where L = Number of leaf nodes T = Number of internal nodes with two children

**Proof:**

Let number of nodes with 2 children be T. Proof can be divided in three cases.

** Case 1:** There is only one node, the relationship holds

as T = 0, L = 1.

** Case 2:** Root has two children, i.e., degree of root is 2.

Sum of degrees of nodes with two children except root + Sum of degrees of nodes with one child + Sum of degrees of leaves + Root's degree = 2 * (No. of Nodes - 1) Putting values of above terms, (T-1)*3 + S*2 + L + 2 = (S + T + L - 1)*2 Cancelling 2S from both sides. (T-1)*3 + L + 2 = (T + L - 1)*2 T - 1 = L - 2 T = L - 1

* Case 3: *Root has one child, i.e., degree of root is 1.

Sum of degrees of nodes with two children + Sum of degrees of nodes with one child except root + Sum of degrees of leaves + Root's degree = 2 * (No. of Nodes - 1) Putting values of above terms, T*3 + (S-1)*2 + L + 1 = (S + T + L - 1)*2 Cancelling 2S from both sides. 3*T + L -1 = 2*T + 2*L - 2 T - 1 = L - 2 T = L - 1

Therefore, in all three cases, we get T = L-1.

We have discussed proof of two important properties of Trees using Handshaking Lemma. Many GATE questions have been asked on these properties, following are few links.

GATE-CS-2015 (Set 3) | Question 35

GATE-CS-2015 (Set 2) | Question 20

GATE-CS-2005 | Question 36

GATE-CS-2002 | Question 34

GATE-CS-2007 | Question 43

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

## Recommended Posts:

- Binary Tree | Set 2 (Properties)
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Convert a given Binary tree to a tree that holds Logical AND property
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Sub-tree with minimum color difference in a 2-coloured tree
- Convert a Binary Tree into its Mirror Tree
- Check if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2
- Create a mirror tree from the given binary tree
- Minimum swap required to convert binary tree to binary search tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach