0% found this document useful (0 votes)
99 views6 pages

Group A Aassign 1 Book

The document describes a C++ program to create a binary tree to represent the hierarchical structure of a book containing chapters, sections, and subsections. The program defines a Book class with methods to create and traverse the tree. It stores the nodes in a binary tree data structure and includes methods to insert nodes, find the height of the tree, and print the nodes level-order. The main method allows the user to create the tree and display it level-order. The time complexity is O(n) for creation and traversal, and space required is O(n) to store the nodes.

Uploaded by

prajakta kudale
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)
99 views6 pages

Group A Aassign 1 Book

The document describes a C++ program to create a binary tree to represent the hierarchical structure of a book containing chapters, sections, and subsections. The program defines a Book class with methods to create and traverse the tree. It stores the nodes in a binary tree data structure and includes methods to insert nodes, find the height of the tree, and print the nodes level-order. The main method allows the user to create the tree and display it level-order. The time complexity is O(n) for creation and traversal, and space required is O(n) to store the nodes.

Uploaded by

prajakta kudale
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/ 6

College Name : Trinity College of Engineering and Research, Pune

Name : Prof. Krushna Belerao

Problem Statement: A book consists of chapters, chapters consist of sections and sections consist
of subsections. Construct a tree and print the nodes. Find the time and space requirements of your
method.

#include<iostream>

#include<string.h>

using namespace std;

class BTNode

public:

BTNode *left, *right;

char data[10];

};

class Book

public: BTNode *root, *temp;

char d[20];

Book()

temp = root = NULL;

void create()

char ans='y';

do

temp = new BTNode();


cout<<"\nEnter The Element:";

cin>>temp->data;

temp->left=NULL;

temp->right=NULL;

if(root == NULL)

root = temp;

else

insert(root, temp);

cout<<"\nDo You Want to Enter More Elements?(y/n)";

cin>>ans;

}while(ans == 'y'|| ans == 'Y');

void insert(BTNode *root, BTNode *temp)

char ch;

cout<<"\nWhere to insert left/right?"<<root->data<<":";

cin>>ch;

if(ch == 'r' || ch == 'R')

if(root->right == NULL)

root->right = temp;

else
insert(root->right, temp);

else

if(root->left == NULL)

root->left = temp;

else

insert(root->left, temp);

void rec_inorder(BTNode *root)

if(root!=NULL)

rec_inorder(root->left);

cout<<root->data;

rec_inorder(root->right);

void printLevelOrder(BTNode *root)

int h = height(root);

int i;

for(i=1;i<=h;i++)

{
cout<<"\n";

printLevel(root,i);

void printLevel(BTNode *root, int level)

if(root == NULL)

return;

if(level == 1 )

//

cout<<" "<<root->data;

else if (level>1)

printLevel(root->left,level-1);

printLevel(root->right,level-1);

int height(BTNode *node)

if(node == NULL)

return 0;

else

int lheight = height(node->left);

int rheight = height(node->right);


if(lheight>rheight)

return (lheight+1);

else

return (rheight+1);

};

int main()

int choice;

Book b1;

do

cout<"\n\tMain Menu:";

cout<<"\n1.Create";

cout<<"\n2.Display";

cout<<"\n3.Exit";

cout<<"\nEnter Your Choice:";

cin>>choice;

switch(choice)

case 1: b1.create();break;

case 2: if(b1.root==NULL)

cout<<"\nTree Not Created";

else

b1.printLevelOrder(b1.root);
break;

}while(choice <=2);

return 0; }

You might also like