Advanced Data Structures: Binary Trees in C

Hello there, fellow coder! Today, we’re going to dive deep into the world of advanced data structures, specifically focusing on binary trees. If you’ve ever wondered how search algorithms can find information so quickly or how databases store and retrieve data efficiently, you’re in the right place. So, let’s get started!

What is a Binary Tree?

A binary tree is a tree-type non-linear data structure with a maximum of two children for each parent. Every node in a binary tree has a left and right reference along with the data element. The node at the top of the hierarchy of a tree is called the root node. The nodes that hold other sub-nodes are the parent nodes.

Types of Binary Trees

There are several types of binary trees, each with its unique properties and uses. Here are a few:

  1. Full Binary Tree: A full binary tree is a binary tree type where every node has either 0 or 2 child nodes.
  2. Complete Binary Tree: In a complete binary tree, all levels are completely filled except possibly the last level, which is filled from left to right.
  3. Perfect Binary Tree: A perfect binary tree is a type of binary tree in which every internal node has exactly two child nodes and all the leaf nodes are at the same level.
  4. Balanced Binary Tree: A balanced binary tree is a binary tree structure in which the left and right subtrees of every node differ in height by no more than 1.
  5. Degenerate (or pathological) Tree: A degenerate tree is a tree where every internal node has one child. Such trees are performance-wise same as linked list.

Code Example

Let’s look at some code examples of binary trees. Here’s a simple implementation of a binary tree in C:

#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node* left;
    struct node* right;
};

// New node creation
struct node* newNode(int data) {
    struct node* node = (struct node*)malloc(sizeof(struct node));

    node->data = data;
    node->left = NULL;
    node->right = NULL;

    printf("Created node with data: %d\n", data);  // Print the data of the newly created node

    return(node);
}

void printTree(struct node* root, int space) {
    int i;

    // Base case
    if (root == NULL) return;

    // Increase distance between levels
    space += 10;

    // Process right child first
    printTree(root->right, space);

    // Print current node after space count
    printf("\n");
    for (i = 10; i < space; i++) {
        printf(" ");
    }
    printf("%d\n", root->data);

    // Process left child
    printTree(root->left, space);
}

int main() {
    struct node *root = newNode(1);  
    root->left        = newNode(2);
    root->right       = newNode(3);
    root->left->left  = newNode(4);
    root->left->right = newNode(5); 

    printf("\nStructure of the tree:\n");
    printTree(root, 0);

    return 0;
}

Code Explanation in Brief

In this code, we first define the structure of a node. Each node contains an integer data and pointers to its left and right child nodes. We then create a newNode function that allocates memory for a new node and sets its data and child nodes. In the main function, we create a root node and add child nodes to it.

Code Explanation in Details

In this code, we first define a structure node that has an integer data and two pointers left and right to the left and right child nodes.

The newNode function creates a new node with a given data. It allocates memory for the new node using the malloc function, assigns the data to the data field of the node, and sets the left and right pointers to NULL, indicating that this node has no children yet.

In the main function, we create a new node with data 1 and assign it to the root pointer. We then create two more nodes with data 2 and 3 and assign them to the left and right children of the root. Finally, we create two more nodes with data 4 and 5 and assign them to the left and right children of the node with data 2.

Wrapping up

Binary trees are a fundamental part of computer science and are used in many areas, including search algorithms, sorting algorithms, and database algorithms. Understanding how they work and how to implement them is crucial for any serious programmer.

Frequently Asked Questions (FAQ)

  • Which data structure implements binary tree?

    The binary tree is implemented using a specific data structure known as a node-based binary tree. Each node in this structure contains a key, a value, and two pointers to their left and right child nodes.

  • What are examples of binary trees in data structure?

    Examples of binary trees include a binary search tree, AVL tree, treap, heap, B-tree, and R-tree. These are all specialized types of binary trees that are used in various applications.

  • What is advanced data structures?

    Advanced data structures are more complex types of data structures that are built upon the basic data structures. They include binary trees, heaps, graphs, hash tables, and others. These data structures are used in more complex algorithms and applications.

  • Which is the most efficient data structure used in binary tree construction?

    The most efficient data structure for constructing a binary tree is the node-based binary tree data structure. This structure allows for efficient insertion, deletion, and search operations, which are crucial for many algorithms and applications that use binary trees.

  • What is a full binary tree?

    A full binary tree is a binary tree in which every node has either 0 or 2 children. In other words, every node is either a leaf node or has two children.

  • What is a complete binary tree?

    A complete binary tree is a binary tree in which all levels are completely filled except possibly the last level, which is filled from left to right.

  • What is a perfect binary tree?

    A perfect binary tree is a binary tree in which all internal nodes have exactly two children and all leaf nodes are at the same level.

  • What is a balanced binary tree?

    A balanced binary tree is a binary tree in which the height of the left and right subtrees ofevery node differ by no more than 1. This balance helps ensure that operations like insertion, deletion, and search can be performed efficiently.

  • What is a degenerate tree?

    A degenerate (or pathological) tree is a tree where every internal node has one child. Such trees are performance-wise the same as a linked list.

  • What is a pointer in C?

    A pointer in C is a variable that stores the memory address of another variable. Pointers are used for dynamic memory allocation and to create complex data structures like linked lists and trees.

Scroll to Top