0% found this document useful (0 votes)
4 views4 pages

Trees Data Structure

The document contains two C programs: the first converts a binary tree into a doubly linked list and prints the list, while the second checks if a binary search tree is balanced. The first program uses a recursive function to link nodes, and the second program calculates heights to determine balance. Both programs include a main function to demonstrate their functionality.

Uploaded by

hgsju8559
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Trees Data Structure

The document contains two C programs: the first converts a binary tree into a doubly linked list and prints the list, while the second checks if a binary search tree is balanced. The first program uses a recursive function to link nodes, and the second program calculates heights to determine balance. Both programs include a main function to demonstrate their functionality.

Uploaded by

hgsju8559
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. Write a program to convert a binary tree into a doubly linked list.

#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node* left;

struct Node* right;

} Node;

Node* createNode(int data) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = data;

newNode->left = newNode->right = NULL;

return newNode;

void convertToDLL(Node* root, Node** head, Node** prev) {

if (root == NULL) {

return;

convertToDLL(root->left, head, prev);

if (*prev == NULL) {

*head = root;

} else {

(*prev)->right = root;

root->left = *prev;

*prev = root;

convertToDLL(root->right, head, prev);

Node* binaryTreeToDLL(Node* root) {

Node* head = NULL;


Node* prev = NULL;

convertToDLL(root, &head, &prev);

return head;

void printDLL(Node* head) {

Node* temp = head;

printf("Doubly Linked List: ");

while (temp) {

printf("%d ", temp->data);

temp = temp->right;

printf("\n");

int main() {

Node* root = createNode(10);

root->left = createNode(5);

root->right = createNode(20);

root->left->left = createNode(2);

root->left->right = createNode(8);

root->right->left = createNode(15);

root->right->right = createNode(25);

Node* head = binaryTreeToDLL(root);

printDLL(head);

return 0;

OUTPUT:-

Doubly Linked List: 2 5 8 10 15 20 25


2. Write a program to check whether a given binary search tree is balanced.

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* left;

struct Node* right;

};

struct Node* newNode(int data) {

struct Node* node = (struct Node*)malloc(sizeof(struct Node));

node->data = data;

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

return node;

int height(struct Node* root) {

if (root == NULL)

return 0;

int leftHeight = height(root->left);

int rightHeight = height(root->right);

return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight);

int isBalanced(struct Node* root) {

if (root == NULL)

return 1;

int leftHeight = height(root->left);

int rightHeight = height(root->right);

if (abs(leftHeight - rightHeight) > 1)

return 0;

return isBalanced(root->left) && isBalanced(root->right);

}
struct Node* insert(struct Node* root, int data) {

if (root == NULL)

return newNode(data);

if (data < root->data)

root->left = insert(root->left, data);

else if (data > root->data)

root->right = insert(root->right, data);

return root;

int main() {

struct Node* root = NULL;

root = insert(root, 10);

root = insert(root, 5);

root = insert(root, 15);

root = insert(root, 3);

root = insert(root, 7);

root = insert(root, 13);

root = insert(root, 17);

if (isBalanced(root))

printf("The BST is balanced.\n");

else

printf("The BST is not balanced.\n");

return 0;

OUTPUT:-

The BST is balanced.

You might also like