0% found this document useful (0 votes)
2 views

7. Implement Binary Search Tree ADT using Linked List

The document outlines an experiment to implement a Binary Search Tree (BST) Abstract Data Type (ADT) using a linked list in C programming. It provides theoretical background on binary trees, including definitions of nodes, depth, height, and types of binary trees. The program constructs a BST and performs an in-order traversal, demonstrating the implementation of the BST ADT.

Uploaded by

tuba.khan7575
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

7. Implement Binary Search Tree ADT using Linked List

The document outlines an experiment to implement a Binary Search Tree (BST) Abstract Data Type (ADT) using a linked list in C programming. It provides theoretical background on binary trees, including definitions of nodes, depth, height, and types of binary trees. The program constructs a BST and performs an in-order traversal, demonstrating the implementation of the BST ADT.

Uploaded by

tuba.khan7575
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

EXPERIMENT NO.

7
Aim: Implement Binary Search Tree ADT using Linked List.

LO: LO4: Students will be able to select appropriate searching techniques for given problems

Software: ‘C’ Programming

Theory: A binary tree is a tree data structure in which each node has at most two children, which are
referred to as the left child and the right child. Every node excluding a root in a tree is connected by a
directed edge from exactly one other node. This node is called a parent. On the other hand, each node
can be connected to arbitrary number of nodes, called children. Nodes with no children are called
leaves, or external nodes. Nodes which are not leaves are called internal nodes. Nodes with the same
parent are called siblings.
 The depth of a node is the number of edges from the root to the node.
 The height of a node is the number of edges from the node to the deepest leaf.
 The height of a tree is a height of the root.
 A full binary tree.is a binary tree in which each node has exactly zero or two children.
 A complete binary tree is a binary tree, which is completely filled, with the possible exception
of the bottom level, which is filled from left to right.

Binary Search tree is a binary tree in which each internal node x stores an element such that the
element stored in the left subtree of x are less than or equal to x and elements stored in the right
subtree of x are greater than or equal to x. This is called binary-search-tree property.
Program:

#include <stdio.h>

#include <malloc.h>

struct node {

struct node * left;

char data;

struct node * right;

};

struct node *constructTree( int );

void inorder(struct node *);

char array[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0', '\0', 'H' };

int leftcount[ ] = { 1, 3, 5, -1, 9, -1, -1, -1, -1, -1 };

int rightcount[ ] = { 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 };

void main() {

struct node *root;

root = constructTree( 0 );

printf("In-order Traversal: \n");

inorder(root);

struct node * constructTree( int index ) {

struct node *temp = NULL;

if (index != -1) {

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

temp->left = constructTree( leftcount[index] );

temp->data = array[index];

temp->right = constructTree( rightcount[index] );

return temp;
}

void inorder( struct node *root ) {

if (root != NULL) {

inorder(root->left);

printf("%c\t", root->data);

inorder(root->right);

Output:

Conclusion: We have implemented Binary Search Tree ADT using Linked List.

You might also like