7. A binary search tree (BST) whose root is pointed by a BinNode pointer root, w
ID: 3770599 • Letter: 7
Question
7. A binary search tree (BST) whose root is pointed by a BinNode pointer root, where the BST node is defined below
class BinNode{
public:
double data;
BinNode * left, *right;
}
(a) Write a recursive function to insert an integer item in this BST
void insert( BinNode* & root, int item)
{
}
(b) Write a function to print the integers stored in all the nodes of a binary search tree (BST) in increasing order,
void printBST( BinNode * root)
{
}
8. Answer the following questions
a) How to write int exp(int numb, int power) recursively?
b) Write a recursive function that counts the number of zero digits in an integer.
For example: zeros(10200) returns 3
c) Write a recursive function to count the number of internal nodes in a binary tree.
int count(TreeNode* node)
{
}
Explanation / Answer
7a)
class BinNode{
public:
double data;
BinNode* left, *right;
}
BinNode* bst=NULL;
void insert( BinNode* & root, int item)
{
BinNode* temp;
if ((*root) == NULL){
temp = (BinNode*) malloc(sizeof(temp));
temp->data =item;
temp->left = NULL;
temp->right = NULL;
root = temp;
}
else if (item < (*root)->left){
insert((*root)->left, item);
}
else if (item >(*root)->right){
insert((*root)->right);
}
return;
}
7b:
void printBST( BinNode * root)
{
if (root == NULL) return;
print(root->left);
printf("%d ", root->data);
print(root->right);
return;
}
8a)
int exp(int numb,int power)
{
if(power==0)
return 1;
return numb*exp(numb,power-1); // returns the value
}
8b)
int zeros(int numb){
if(numb==0) // numb is zero
return 1; // The bottom out
else if(numb < 10 && numb > -10)
return 0;
else // The recursion
return zeros(numb/10) + zeros(numb%10);}
8c)
int count(TreeNode* node)
{
if(node == NULL)
return 0;
if(node->left == NULL && node->right==NULL)
return 1;
else
return count(node->left)+count(node->right);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.