0% found this document useful (0 votes)
3 views2 pages

Tree Practice

The document outlines the implementation of a Binary Tree in C, detailing the creation of a sample binary tree and providing skeleton code for the task. It specifies various operations to be implemented, including different types of traversals, height calculation, and node counting. Additionally, it includes bonus tasks such as printing the left view and checking if the tree is height balanced.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Tree Practice

The document outlines the implementation of a Binary Tree in C, detailing the creation of a sample binary tree and providing skeleton code for the task. It specifies various operations to be implemented, including different types of traversals, height calculation, and node counting. Additionally, it includes bonus tasks such as printing the left view and checking if the tree is height balanced.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Binary Tree Operations

Objective:​
Implement a Binary Tree in C and perform various standard operations.

Part 1: Tree Creation (Provided Skeleton Code)

Complete the following code to create a sample binary tree manually.

Skeleton Code (C)


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

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

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

// Manually creating a binary tree


// 1
// / \
// 2 3
// / \ \
// 4 5 6

struct Node* createSampleTree() {


struct Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->right = createNode(6);
return root;
}
Part 2: Implement the Following Functions

Write separate functions in C to implement the following operations:

1.​ Inorder Traversal (Left, Root, Right)


2.​ Preorder Traversal (Root, Left, Right)
3.​ Postorder Traversal (Left, Right, Root)
4.​ Level Order Traversal (Use Queue)
5.​ Calculate Height of the Tree
6.​ Count Total Number of Nodes
7.​ Count Number of Leaf Nodes
8.​ Count Number of Internal Nodes
9.​ Find Maximum Value in the Tree
10.​Search for a Given Value in the Tree
11.​Check if Two Trees are Identical
12.​Mirror the Binary Tree

Bonus Tasks:

●​ Print the left view of the binary tree.


●​ Check if the binary tree is height balanced.
●​ Convert the binary tree into a doubly linked list (using in-order traversal)

You might also like