Binary Tree Data Structures and Algorithms: Assignment 8, Assignments of Data Structures and Algorithms

This assignment focuses on binary tree data structures and algorithms. It includes exercises and code examples for creating, traversing, and manipulating binary trees. The assignment covers topics such as inorder, preorder, and postorder traversal, level-order traversal, height calculation, node counting, and degree analysis. It also explores tree construction from pre-order and in-order traversals, as well as post-order and in-order traversals.

Typology: Assignments

2023/2024

Uploaded on 09/29/2024

anuj-kumar-34
anuj-kumar-34 🇮🇳

1 document

1 / 31

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ASSIGNMENT 8
BINARY TREE
NAME ANUJ KUMAR
ROLL NO. 202UGCS036
Q1. Write a program to create a binary tree .
Q4. Write a recursive program to traverse a binary tree using the
following
traversal:
a. Inorder Traversal
b. Preorder Traversal
c. Postorder Traversal
CODE:-
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Node {
int data;
struct Node* lchild;
struct Node* rchild;
};
struct Node* root = NULL;
struct Queue {
int front, rear;
int size;
struct Node** Q;
};
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download Binary Tree Data Structures and Algorithms: Assignment 8 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

ASSIGNMENT 8

BINARY TREE

NAME – ANUJ KUMAR

ROLL NO. 202UGCS

Q1. Write a program to create a binary tree.

Q4. Write a recursive program to traverse a binary tree using the

following

traversal:

a. Inorder Traversal

b. Preorder Traversal

c. Postorder Traversal

CODE:-

#include <stdio.h> #include <stdlib.h> #include <stdbool.h> struct Node { int data; struct Node* lchild; struct Node* rchild; }; struct Node* root = NULL; struct Queue { int front, rear; int size; struct Node** Q; };

void create(struct Queue* q, int size) { q->size = size; q->front = q->rear = 0 ; q->Q = (struct Node)malloc(q->size * sizeof(struct Node)); } void enqueue(struct Queue q, struct Node* x) { if ((q->rear + 1 ) % q->size == q->front) printf("Queue is full\n"); else { q->rear = (q->rear + 1 ) % q->size; q->Q[q->rear] = x; } } struct Node* dequeue(struct Queue* q) { struct Node* x = NULL; if (q->front == q->rear) printf("Queue is empty\n"); else { q->front = (q->front + 1 ) % q->size; x = q->Q[q->front]; } return x; } int isempty(struct Queue q) { return q.front == q.rear; } void binarytree() { struct Node *p, t; int x; struct Queue q; create(&q, 100 ); printf("Enter root value: "); scanf("%d", &x); root = (struct Node)malloc(sizeof(struct Node)); root->data = x; root->lchild = root->rchild = NULL; enqueue(&q, root);

Inorder(p->lchild); printf("%d ",p->data); Inorder(p->rchild); } } int main() { binarytree(); printf("Preorder traversal: "); Preorder(root); printf("Postorder traversal: "); Postorder(root); printf("inorder traversal: "); Inorder(root); return 0 ; }

OUTPUT:-

Q 7. Write a program to perform level-order traversal of a binary

tree.

Q10.Write a program to count the height of a binary tree

Q.11Write a program to count the total number of nodes in a binary

tree

Q12.Write a program to count the number of following nodes in a

binary tree:

a. Nodes with Degree Zero (0)

b. Nodes with Degree One (1)

c. Nodes with Degree Two (2)

CODE:-

#include <stdio.h> #include <stdlib.h> #include <stdbool.h> struct Node { int data; struct Node* lchild; struct Node* rchild; }; struct Node* root = NULL; struct Queue { int front, rear; int size; struct Node** Q; }; void create(struct Queue* q, int size) { q->size = size; q->front = q->rear = 0 ; q->Q = (struct Node)malloc(q->size * sizeof(struct Node)); } void enqueue(struct Queue q, struct Node* x) { if ((q->rear + 1 ) % q->size == q->front) printf("Queue is full\n"); else { q->rear = (q->rear + 1 ) % q->size; q->Q[q->rear] = x;

printf("Enter the right child of %d : ", p->data); scanf("%d", &x); if (x != - 1 ) { t = (struct Node)malloc(sizeof(struct Node)); t->data = x; t->lchild = t->rchild = NULL; p->rchild = t; enqueue(&q, t); } } } void levelordertraversal(struct Node root) { if (root == NULL) { return; } struct Queue q; create(&q, 100 ); enqueue(&q, root); while (!isempty(q)) { root = dequeue(&q); printf("%d ", root->data); if (root->lchild) { enqueue(&q, root->lchild); } if (root->rchild) { enqueue(&q, root->rchild); } } } int Height(struct Node *p) { int l = 0 ; int r = 0 ; if (p == NULL){ return 0 ; }

l = Height(p->lchild); r = Height(p->rchild); if (l > r){ return l + 1 ; } else { return r + 1 ; } } int Count(struct Node *p) { int x; int y; if (p != NULL){ x = Count(p->lchild); y = Count(p->rchild); return x + y + 1 ; } return 0 ; } int Sum(struct Node *p) { int x; int y; if (p != NULL){ x = Sum(p->lchild); y = Sum(p->rchild); return x + y + p->data; } return 0 ; } int degree0node(struct Node *p){ int x; int y; if (p != NULL){ x = degree0node(p->lchild); y = degree0node(p->rchild); if (p->lchild == NULL && p->rchild == NULL){ return x + y + 1 ; } else { return x + y; }

printf("Number of leaf nodes (degree 0): %d\n", degree0node(root)); printf("Number of nodes with degree 1: %d\n", degree1NodeCount(root)); printf("Number of nodes with degree 2: %d\n", degree2NodeCount(root)); return 0 ; }

OUTPUT:

Q8. Write a program to generate tree from a given Pre-order and

Inorder Traversal

of a binary tree.

9. Write a program to generate tree from a given Postorder and

Inorder Traversal

of a binary tree.

CODE:-

#include <stdio.h> #include <stdlib.h> #define MAX 5 struct Node { int data; struct Node* left; struct Node* right; }; struct CircularQueue { struct Node* arr[MAX]; int front; int rear; }; void initQueue(struct CircularQueue* cq) { cq->front = - 1 ; cq->rear = - 1 ; } int isFull(struct CircularQueue* cq) { return (cq->front == (cq->rear + 1 ) % MAX); } int isEmpty(struct CircularQueue* cq) { return (cq->front == - 1 && cq->rear == - 1 ); } struct Node* createNode(int x) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = x; newNode->left = NULL; newNode->right = NULL; return newNode; }

newNode->left = buildTreeFromPreIn(pre, in, start, i - 1 , idx); newNode->right = buildTreeFromPreIn(pre, in, i + 1 , end, idx); return newNode; } struct Node* buildTreeFromPostIn(int post[], int in[], int start, int end, int* idx) { if (start > end) { return NULL; } int data = post[(idx)--]; struct Node newNode = createNode(data); if (start == end) { return newNode; } int i; for (i = start; i <= end; i++) { if (in[i] == data) { break; } } newNode->right = buildTreeFromPostIn(post, in, i + 1 , end, idx); newNode->left = buildTreeFromPostIn(post, in, start, i - 1 , idx); return newNode; } int levelOrder(struct Node* root) { if (root == NULL) { printf("Tree is empty\n"); return 0 ; } struct CircularQueue cq; initQueue(&cq);

enqueue(&cq, root); printf("%d ", root->data); while (!isEmpty(&cq)) { struct Node* temp = dequeue(&cq); if (temp->left) { printf("%d ", temp->left->data); enqueue(&cq, temp->left); } if (temp->right) { printf("%d ", temp->right->data); enqueue(&cq, temp->right); } } printf("\n"); return 1 ; } int main() { struct Node* rootPreIn; int inPre[] = { 4 , 2 , 5 , 1 , 6 , 7 }; int pre[] = { 1 , 2 , 4 , 5 , 6 , 7 }; int n = sizeof(inPre) / sizeof(inPre[ 0 ]); int preorderIndex = 0 ; rootPreIn = buildTreeFromPreIn(pre, inPre, 0 , n - 1 , &preorderIndex); printf("Level-Order Traversal of the built tree from Preorder and Inorder: "); levelOrder(rootPreIn); struct Node* rootPostIn; int post[] = { 4 , 5 , 2 , 6 , 7 , 1 }; int inPost[] = { 4 , 2 , 5 , 1 , 6 , 7 }; int postorderIndex = n - 1 ; rootPostIn = buildTreeFromPostIn(post, inPost, 0 , n - 1 , &postorderIndex); printf("Level-Order Traversal of the built tree from Postorder and Inorder: "); levelOrder(rootPostIn); return 0 ; }

q->Q = (struct Node)malloc(q->size * sizeof(struct Node)); } void enqueue(struct Queue q, struct Node* x) { if ((q->rear + 1 ) % q->size == q->front) printf("Queue is full\n"); else { q->rear = (q->rear + 1 ) % q->size; q->Q[q->rear] = x; } } struct Node* dequeue(struct Queue* q) { struct Node* x = NULL; if (q->front == q->rear) printf("Queue is empty\n"); else { q->front = (q->front + 1 ) % q->size; x = q->Q[q->front]; } return x; } int isempty(struct Queue q) { return q.front == q.rear; } void binarytree() { struct Node *p, t; int x; struct Queue q; create(&q, 100 ); printf("Enter root value: "); scanf("%d", &x); root = (struct Node)malloc(sizeof(struct Node)); root->data = x; root->lchild = root->rchild = NULL; enqueue(&q, root); while (!isempty(q)) { p = dequeue(&q); printf("Enter the left child of %d : ", p->data);

scanf("%d", &x); if (x != - 1 ) { t = (struct Node)malloc(sizeof(struct Node)); t->data = x; t->lchild = t->rchild = NULL; p->lchild = t; enqueue(&q, t); } printf("Enter the right child of %d : ", p->data); scanf("%d", &x); if (x != - 1 ) { t = (struct Node)malloc(sizeof(struct Node)); t->data = x; t->lchild = t->rchild = NULL; p->rchild = t; enqueue(&q, t); } } } int find_height(struct Node* root) { if (root == NULL) return 0 ; int leftHeight = find_height(root->lchild); int rightHeight = find_height(root->rchild); return 1 + (leftHeight > rightHeight? leftHeight : rightHeight); } int count_leaf_node(struct Node* root) { if (root == NULL) return 0 ; if (root->lchild == NULL && root->rchild == NULL) return 1 ; return count_leaf_node(root->lchild) + count_leaf_node(root->rchild); } bool is_strict(struct Node* root) { if (root == NULL) return true; if ((root->lchild == NULL) != (root->rchild == NULL)) return false; return is_strict(root->lchild) && is_strict(root->rchild); } bool is_complete(struct Node* root) { if (root == NULL) return true; int height = find_height(root);

while (!isempty(q)) { root = dequeue(&q); printf("%d ", root->data); if (root->lchild) enqueue(&q, root->lchild); if (root->rchild) enqueue(&q, root->rchild); } } int main() { binarytree(); printf("Level-order traversal: "); levelordertraversal(root); printf("\n"); //printf("Height of tree: %d\n", find_height(root)); //printf("Total number of leaf nodes: %d\n", count_leaf_node(root)); if (is_strict(root)) { printf("The tree is a strict binary tree.\n"); } else { printf("The tree is not a strict binary tree.\n"); } if (is_complete(root)) { printf("The tree is a complete binary tree.\n"); } else { printf("The tree is not a complete binary tree.\n"); } if (is_almost_complete(root)) { printf("The tree is almost complete.\n"); } else { printf("The tree is not almost complete.\n"); } return 0 ; }

OUTPUT:-

Q3. Two binary trees are similar if they are both empty or if they are

both

nonempty. Their left subtrees are similar, and their right subtrees

are similar.

Write an algorithm to determine if two binary trees are similar.

CODE:-

#include<stdio.h> #include<malloc.h> #include<stdbool.h> struct node { int data; struct node * left; struct node * right; }; struct node * new_node(int x){ struct node * temp = (struct node *)malloc(sizeof(struct node));