Question

c++ Write a program that implements a binary search tree using a node-pointer implementation. Assume all...

c++

Write a program that implements a binary search tree using a node-pointer implementation. Assume all
entries in the tree will be unique.
struct node {
   int data;
   node * left;
   node * right;
};
Requirements:
1) Implement the following functions :
a) void insert(int data) // creates a new node and inserts it in the
correct location in the tree
b) void print_preorder(node * root); // prints the data in a tree using a
preorder traversal
c) void print_postorder(node * root); // prints the data in a tree using
a postorder traversal
d) void print_inorder(node * root); // prints the data in a tree using an
inorder traversal
e) int search(int data) // searches the tree for data. Returns 0 if data
is not found, otherwise, returns the number of nodes visited MUST RUN IN
O(logN) time!!!!!!
2) Insert the following sequence into the tree :
Sequence A :
1, 5, 4, 6, 7, 2, 3
Sequence B : (Cut and paste this sequence into your source code and create a hard - initialized array)
150, 125, 175, 166, 163, 123, 108, 116, 117, 184, 165, 137, 141, 111, 138, 122, 109, 194, 143, 183, 178, 173, 139,
126, 170, 190, 140, 188, 120, 195, 113, 104, 193, 181, 185, 198, 103, 182, 136, 115, 191, 144, 145, 155, 153, 151,
112, 129, 199, 135, 146, 157, 176, 159, 196, 121, 105, 131, 154, 107, 110, 158, 187, 134, 132, 179, 133, 102, 172,
106, 177, 171, 156, 168, 161, 149, 124, 189, 167, 174, 147, 148, 197, 160, 130, 164, 152, 142, 162, 118, 186, 169,
127, 114, 192, 180, 101, 119, 128, 100
3) Search the Sequence A tree for some values.
4) Search the Sequence B tree for values 196, 137, 102, 190, and print the return value.
5) Traverse the tree with each of the three traversal functions above, and generate a sequence of
output for each of the traversals.
6) DRAW the tree for sequence A only, showing the path of traversal and the sequence of processing
for each of the three given traversals.That is, indicate on the path where the processing of each
node occurs.
7) State the time complexity for each of the functions in part 1 and explain your answers.
Turn in :
1) Paper source code listings.
2) Paper output listings demonstrating all program functionality.Use the insertion sequence provided.
Perform all three traversals and include the output from each one.
3) A diagram for each of the three traversals for sequence A only.
4) Your Big O’s for each function.
Documentation standards :
Follow format guidelines in syllabus

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<stdlib.h>

struct node
{
int key;
struct node *left, *right;
};

// A utility function to create a new BST node
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

// A utility function to do inorder traversal of BST
void print_inorder(struct node *root)
{
if (root != NULL)
{
print_inorder(root->left);
printf("%d \n", root->key);
print_inorder(root->right);
}
}
void print_preorder(struct node *root)
{
   if(root!=NULL)
   {
       printf("%d \n",root->key);
       print_preorder(root->left);
       print_preorder(root->right);
   }
}
void print_postorder(struct node *root)
{
   if(root!=NULL)
   {
       print_preorder(root->left);
       print_preorder(root->right);
       printf("%d \n",root->key);
   }
}
/* A utility function to insert a new node with given key in BST */
struct node* insert(struct node* node, int key)
{
/* If the tree is empty, return a new node */
if (node == NULL) return newNode(key);
  
/* Otherwise, recur down the tree */
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
  
/* return the (unchanged) node pointer */
return node;
}
int f=1;
int searchkey(struct node* root, int key)
{
   struct node *temp=root;
   int c=0;
   while(temp!=NULL)
   {
       if(temp->key==key)
       return c;
       else if(temp->key<key)
       {
           c++;
           temp=temp->right;
       }
       else if(temp->key>key)
       {
           c++;
           temp=temp->left;
       }
       else
       return 0;
      
   }
// Base Cases: root is null or key is present at root
/* if (root==NULL || root->key == key)
return 0;
// Key is greater than root's key
if (root->key < key)
{
      
return 1+searchkey(root->right, key);
   }
return 1+searchkey(root->left, key); */
  
}
// Driver Program to test above functions
int main()
{
struct node *root = NULL;
struct node *root1=NULL;
int a[]={1, 5, 4, 6, 7, 2, 3};
int b[]={150, 125, 175, 166, 163, 123, 108, 116, 117, 184, 165, 137, 141, 111, 138, 122, 109, 194, 143, 183, 178, 173, 139,
126, 170, 190, 140, 188, 120, 195, 113, 104, 193, 181, 185, 198, 103, 182, 136, 115, 191, 144, 145, 155, 153, 151,
112, 129, 199, 135, 146, 157, 176, 159, 196, 121, 105, 131, 154, 107, 110, 158, 187, 134, 132, 179, 133, 102, 172,
106, 177, 171, 156, 168, 161, 149, 124, 189, 167, 174, 147, 148, 197, 160, 130, 164, 152, 142, 162, 118, 186, 169,
127, 114, 192, 180, 101, 119, 128, 100};
int n=sizeof(a);
int length=n/sizeof(int);
int n1=sizeof(b);
int len=n1/sizeof(int);
for(int i=0;i<length;i++)
{
       if(i==0)
       root = insert(root, a[0]);
       else
       insert(root, a[i]);
      
   }
   for(int i=0;i<len;i++)
{
       if(i==0)
       root1 = insert(root1, b[0]);
       else
       insert(root1, b[i]);
      
   }
  
// print inoder traversal of the BST
printf("Inorder \n");
print_inorder(root);
    printf("preorder\n");
    print_preorder(root);
    printf("postorder\n");
    print_postorder(root);
    printf("path count is %d\n",searchkey(root,5));
   printf("path count is %d\n",searchkey(root,4));  
   printf("path count is %d\n",searchkey(root,1));
    printf("path count is %d\n",searchkey(root,2));
    printf("path count is %d\n",searchkey(root1,196));
    printf("path count is %d\n",searchkey(root1,137));
   printf("path count is %d\n",searchkey(root1,102));
   printf("path count is %d\n",searchkey(root1,192));
return 0;
}

//Time complexity for travesals

T(n) = 2*T(n/2) + 1

So,time complexities be

print_inorder=O(n)

print_inorder=O(n);

print_inorder=O(n);

Time complexity for searchkey:

bestcase:

O(h)=O(log(n)) where h is height of a tree

worstcase:O(n)

insert:

bestcase:O(log(n))

worstcase:O(n)

Add a comment
Know the answer?
Add Answer to:
c++ Write a program that implements a binary search tree using a node-pointer implementation. Assume all...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT