# Binary tree data structure

Posted by phorman on Fri, 04 Mar 2022 11:57:41 +0100

# Binary tree data structure

A tree whose element has at most 2 children is called a binary tree. Because each element in a binary tree can only have two children, we usually name them left and right children. The binary tree node contains the following parts.

• data
• Pointer to left child
• Pointer to the right child

Binary tree includes:

1. Full binary tree
2. Complete binary tree
3. Perfect Binary Tree
4. Balanced Binary Tree
5. A degenerate (or pathological) tree

## Full binary tree:

If each node has 0 or 2 child nodes, the binary tree is a complete binary tree. The following is an example of a complete binary tree. We can also say that a complete binary tree is a binary tree, in which all nodes except leaf nodes have two children.

```              18
/       \
15         30
/  \        /  \
40    50    100   40

18
/    \
15     20
/  \
40    50
/   \
30   50

18
/     \
40       30
/  \
100   40
```

## Complete binary tree:

If all levels are fully populated (except the last level) and all keys of the last level are retained as much as possible, the binary tree is a complete binary tree

```           18
/ \
15  30
/ \  / \
40 50 100 40

18
/ \
15   30
/ \   / \
40 50  100 40
/ \ /
8 7 9

```

## Perfect Binary Tree:

A perfect binary tree, in which all internal nodes have two child nodes, and all leaf nodes are at the same level.

• In a perfect binary tree, the number of leaf nodes is the number of internal nodes plus 1

• L = I + 1, where l = number of leaf nodes and I = number of internal nodes.

• A perfect binary tree with height h (where the height of the binary tree is the longest path from the root node to any leaf node in the tree) has 2 h + 1 – 1 nodes.

• An example of a perfect binary tree is the ancestor of the family. Stick to one's foundation, parents as children, parents as children

```               18
/       \
15         30
/  \        /  \
40    50    100   40

18
/       \
15         30
```

## Balanced Binary Tree:

If the height of the binary tree is O (log n), the binary tree is balanced, where n is the number of nodes. For example, the AVL tree maintains the height of O (log n) by ensuring that the height difference between the left and right subtrees does not exceed 1. Red black trees maintain the height of O (log n) by ensuring the number of trees. The black nodes on each root to the leaf path are the same, and there are no adjacent red nodes. Balanced binary search trees perform well because they provide o (log n) time for search, insertion, and deletion.

## A degenerate (or pathological) tree:

Each internal node has a child. Such a tree has the same performance as a linked list.

```      10
/
20
\
30
\
40
```

Binary tree representation in C language: a tree is represented by a pointer to the highest node in the tree. If the tree is empty, the value of root is NULL.

In C language, we can use structure to represent tree nodes. The following is an example of a tree node with integer data.

```/* Class containing left and right child
of current node and key value*/
class Node
{
int key;
Node left, right;

public Node(int item)
{
key = item;
left = right = null;
}
}
```

Let's create a simple tree with four nodes in C. The tree created is as follows

```      tree
----
1    <-- root
/   \
2     3
/
4
```

Demonstrate in C language:

```#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};

/* newNode() allocates a new node
with the given data and NULL left
and right pointers. */
struct node* newNode(int data)
{
// Allocate memory for new node
struct node* node
= (struct node*)malloc(sizeof(struct node));

// Assign data to this node
node->data = data;

// Initialize left and
// right children as NULL
node->left = NULL;
node->right = NULL;
return (node);
}

int main()
{
/*create root*/
struct node* root = newNode(1);
/* following is the tree after above statement
1
/ \
NULL NULL
*/

root->left = newNode(2);
root->right = newNode(3);
/* 2 and 3 become left and right children of 1
1
/    \
2      3
/  \    /  \
NULL NULL NULL NULL
*/

root->left->left = newNode(4);
/* 4 becomes left child of 2
1
/    \
2      3
/  \    /  \
4 NULL NULL NULL
/ \
NULL NULL
*/

getchar();
return 0;
}
```

A tree is a hierarchical data structure. The main uses of trees include maintaining hierarchical data, providing appropriate access rights and insert / delete operations. Binary tree is a special case of tree, in which each node has at most two children.