Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a menu driven program that will allow the user to add, delete, and print a binary tree. There should be two orders ( LNR
Write a menu driven program that will allow the user to add, delete, and print a binary tree. There should be two orders LNR and RNL for printing. Binary Tree:
A binary tree is either empty or consists of a single vertex and two disjoint subsets each
of which is also a binary tree.
Structure of a Binary Tree:int data;
struct treenode leftright;
binarytree;Declaring a binary tree:
binarytree root;
Initialize a binary tree:
use pass by reference
set root to NULL
Checking for empty tree:
use pass by value
if root is NULL, then tree is empty
Checking for full tree:
use pass by value
can we allocate memory the size of a treenode structure?
Adding to a tree:
assuming tree is NOT full
use pass by reference
if current position is empty
allocate memory the size of a tree node structure
put data in it
set both left and right children to NULL
otherwise keep looking for empty spot
if data to add is current node's data
use recursive call to add to left subtree... add &left, datatoadd
otherwise
use recursive call to add to right subtree assuming tree is NOT empty and user has specified data to be removedif not empty if no children set t to NULL if left child only move t to left child if right child only move t to right child if two children set temp to t right move temp to left subtree move temp back to t free temp set temp to tleft move temp to right subtree move temp back to t free temp if data we are looking for is current data otherwiseotherwise Printing the contents of the binary tree:
Assuming you've already checked to make sure the tree isn't empty or printed the
appropriate message in main, you will pass the tree using passbyvalue since no changes
will be made.
LNR Left Node, Right Ascending Order
if the tree is not empty
call LNR recursively sending the left subtree
print the data portion of the current node
call LNR recursively sending the right subtree
RNL Right Node, Left Descending Order
if the tree is not empty
call RNL recursively sending the right subtree
print the data portion of the current node
call RNL recursively sending the left subtree Testcase for Binary Tree:
Delete empty
LNR empty
RNL empty
Add
Add
Add
Add
Add
LNR
Delete testing no children
LNR
Add
Add
Add
Delete not found
Add
Add
Add
Invalid Menu Option error message
Add
Add
LNR
Delete testing left child only
Delete testing right child only
Delete two children
LNR
Quit
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started