























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 31
This page cannot be seen from the preview
Don't miss anything!
























#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 ; }
#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 ; }
#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 ; }
#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));