MUST BE IN C! A binary tree is a data structure where each node has at most two
ID: 3650006 • Letter: M
Question
MUST BE IN C!
A binary tree is a data structure where each node has at most two child nodes. The following defines a binary tree where each node stores a book record. The left-child of a node stores a book with an earlier publishing year while the right-child of a node stores a book with a later publishing year, Implement the following C functions BTree* addBook(BTree* nodeP, char* name, int year): It creates a new BTree if it is empty. Otherwise, it adds the book record to the correct position in the BTree. void freeBTree(BTree* books). It frees the memory space being allocated for the passed BTree argument. void printBooks(BTree* books). It prints the books stored in the passed BTree argument in year. Use the following 4 books to test your code: The C Programming Language, 1990. JavaScript: The Good Parts, 2008. Accelerated C++: Practical Programming by Example, 2000. Scala for the Impatient, 2012Explanation / Answer
#include #include enum {TRUE = 1,FALSE = 0}; typedef struct _node { int info; struct _node *left; struct _node *right; }node; class btree { private: node *root; void InsertLeft(node* & ,node*); void InsertRight(node* &,node*); void deltree(node*); public: btree(); ~btree(); node* GetRoot(void); void MakeTree(void); void DeleteNode(node *,node *,int); int Searchnode(node*,int); void DisplayTree(node*,int); void Inorder(node* ); void Preorder(node*); void Postorder(node*); }; btree::btree() { char create='Y'; root = new node; coutleft = NULL; root->right = NULL; } btree::~btree() { deltree(root); } void btree::deltree(node *root) { if(root) { deltree(root->left); deltree(root->right); delete root; } } node* btree::GetRoot(void) { return(root); } void btree::MakeTree(void) { node *newnode; char create; do { newnode = new node; coutleft=NULL; newnode->right=NULL; if(newnode->info info) InsertLeft(newnode,root); else InsertRight(newnode,root); coutleft=newnode; else { current = current->left; if(newnode->info info) InsertLeft(newnode,current); else InsertRight(newnode,current); } } void btree::InsertRight(node* &newnode,node* current) { if(current->right==NULL) current->right=newnode; else { current = current->right; if(newnode->info info) InsertLeft(newnode,current); else InsertRight(newnode,current); } } void btree::DeleteNode(node *current,node *parent,int delnode) { if(delnode info) DeleteNode(current->left,current,delnode); else if(delnode > current->info) DeleteNode(current->right,current,delnode); else if(delnode == current->info) { if(current->left == NULL) { if(parent->info > current->info) parent->left = current->right; else parent->right = current->right; } else if(current->right == NULL) { if(parent->info > current->info) parent->left = current->left; else parent->right = current->left; } else { node *temp; int flag = 0; parent = current; current = current->left; temp = current; while(current->right != NULL) { current = current->right; if(flag == 0) flag = 1; else temp = temp->right; } parent->info = current->info; if(flag == 0) // No right child parent->left = current->left; else temp->right = current->left; } delete current; } } int btree::Searchnode(node *current,int num) { if(numinfo && current->left!=NULL) Searchnode(current->left,num); else if(num>current->info && current->right!=NULL) Searchnode(current->right,num); else { if(num==current->info) return TRUE; else return FALSE; } return FALSE; } void btree::DisplayTree(node *current,int Level) { if(current) { DisplayTree(current->right,Level+1); coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.