Question

Summarize the following data structures and explain how a node can be inserted into each data...

Summarize the following data structures and explain how a node can be inserted into each data structure and how a node can be deleted from each structure. (C++)

Binary Tree

Stacked, Linked List

Sorted, Linked List - ascending order

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

A binary tree is essentially that of an upside down tree. It starts from the root tree and contains some value. Root has two children left and right child respectively. Left child contains the smallest value and the right child contains the largest value. We construct the child in the form of a node using a link list data structure in C++. Below is the demonstration of BST tree in C++ with insert, delete, display, search operation on binary search tree by implementing all these functions.

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;

void insert(int,int );
void delte(int);
void display(int);
int search(int);
int search1(int,int);
int tree[40],t=1,s,x,i;
// Below main() function call the different operation as per user choice and it display the menu to the user.
main()
{
   int ch,y;
   for(i=1;i<40;i++)
   tree[i]=-1;
   while(1)
   {
cout <<"1.INSERT\n2.DELETE\n3.DISPLAY\n4.SEARCH\n5.EXIT\nEnter your choice:";
       cin >> ch;
       switch(ch)
       {
       case 1:
           cout <<"enter the element to insert";
           cin >> ch;
           insert(1,ch);
           break;
       case 2:
           cout <<"enter the element to delete";
           cin >>x;
           y=search(1);
           if(y!=-1) delte(y);
           else cout<<"no such element in tree";
           break;
       case 3:
           display(1);
           cout<<"\n";
           for(int i=0;i<=32;i++)
           cout <<i;
           cout <<"\n";
           break;
case 4:
           cout <<"enter the element to search:";
           cin >> x;
           y=search(1);
           if(y == -1) cout <<"no such element in tree";
           else cout <<x << "is in " << y <<"position";
           break;
       case 5:
           exit(0);
       }
   }
}
//We had taken the array tree[ ] for the storage of the elements when user press option 1 for insertion element will inserted into the array.
void insert(int s,int ch )
{
   int x;
   if(t==1)
   {
       tree[t++]=ch;
       return;
   }
   x=search1(s,ch);
   if(tree[x]>ch)
       tree[2*x]=ch;
   else
       tree[2*x+1]=ch;
   t++;
}

//below delete function is used for delete the specific element from the array as per user choice if tree[ ] = -1 means there is no element in the tree.
void delte(int x)
{
   if( tree[2*x]==-1 && tree[2*x+1]==-1)
       tree[x]=-1;
   else if(tree[2*x]==-1)
   {   tree[x]=tree[2*x+1];
       tree[2*x+1]=-1;
   }
   else if(tree[2*x+1]==-1)
   {   tree[x]=tree[2*x];
       tree[2*x]=-1;
   }
   else
   {
   tree[x]=tree[2*x];
   delte(2*x);
   }
   t--;
}
//below search function is used to search the element in the tree it will return the element with its position in the tree
int search(int s)
{
if(t==1)
{
cout <<"no element in tree";
return -1;
}
if(tree[s]==-1)
return tree[s];
if(tree[s]>x)
search(2*s);
else if(tree[s]<x)
search(2*s+1);
else
return s;
}
//display function is called to print the all element of the tree
void display(int s)
{
if(t==1)
{cout <<"no element in tree:";
return;}
for(int i=1;i<40;i++)
if(tree[i]==-1)
cout <<" ";
else cout <<tree[i];
return ;
}

int search1(int s,int ch)
{
if(t==1)
{
cout <<"no element in tree";
return -1;
}
if(tree[s]==-1)
return s/2;
if(tree[s] > ch)
search1(2*s,ch);
else search1(2*s+1,ch);
}

Further , we can also implemet the BST using the link list also below is the demonstration of BST using Link list

#include <iostream>

using namespace std;

// Below we create a new node using the struct and it has three fields one is for root value and two pointer left and right for left and right child respectively.


struct node{
   int value;
   node *left;
   node *right;
};

class btree{
public:
   btree();
   ~btree();

   void insert(int key);
   node *search(int key);
   void destroy_tree();
   void inorder_print();
   void postorder_print();
   void preorder_print();

private:
   void destroy_tree(node *leaf);
   void insert(int key, node *leaf);
   node *search(int key, node *leaf);
   void inorder_print(node *leaf);
   void postorder_print(node *leaf);
   void preorder_print(node *leaf);

   node *root;
};


btree::btree(){
   root = NULL;
}

btree::~btree(){
   destroy_tree();
}

void btree::destroy_tree(node *leaf){
   if(leaf != NULL){
       destroy_tree(leaf->left);
       destroy_tree(leaf->right);
       delete leaf;
   }
}

void btree::insert(int key, node *leaf){

   if(key < leaf->value){
       if(leaf->left != NULL){
           insert(key, leaf->left);
       }else{
           leaf->left = new node;
           leaf->left->value = key;
           leaf->left->left = NULL;
           leaf->left->right = NULL;
       }
   }else if(key >= leaf->value){
       if(leaf->right != NULL){
           insert(key, leaf->right);
       }else{
           leaf->right = new node;
           leaf->right->value = key;
           leaf->right->right = NULL;
           leaf->right->left = NULL;
       }
   }

}

void btree::insert(int key){
   if(root != NULL){
       insert(key, root);
   }else{
       root = new node;
       root->value = key;
       root->left = NULL;
       root->right = NULL;
   }
}

node *btree::search(int key, node *leaf){
   if(leaf != NULL){
       if(key == leaf->value){
           return leaf;
       }
       if(key < leaf->value){
           return search(key, leaf->left);
       }else{
           return search(key, leaf->right);
       }
   }else{
       return NULL;
   }
}

node *btree::search(int key){
   return search(key, root);
}

void btree::destroy_tree(){
   destroy_tree(root);
}

void btree::inorder_print(){
   inorder_print(root);
   cout << "\n";
}

void btree::inorder_print(node *leaf){
   if(leaf != NULL){
       inorder_print(leaf->left);
       cout << leaf->value << ",";
       inorder_print(leaf->right);
   }
}

void btree::postorder_print(){
   postorder_print(root);
   cout << "\n";
}

void btree::postorder_print(node *leaf){
   if(leaf != NULL){
       inorder_print(leaf->left);
       inorder_print(leaf->right);
       cout << leaf->value << ",";
   }
}

void btree::preorder_print(){
   preorder_print(root);
   cout << "\n";
}

void btree::preorder_print(node *leaf){
   if(leaf != NULL){
       cout << leaf->value << ",";
       inorder_print(leaf->left);
       inorder_print(leaf->right);
   }
}

int main(){

   //btree tree;
   btree *tree = new btree();

   tree->insert(10);
   tree->insert(6);
   tree->insert(14);
   tree->insert(5);
   tree->insert(8);
   tree->insert(11);
   tree->insert(18);

   tree->preorder_print();
   tree->inorder_print();
   tree->postorder_print();

   delete tree;

}

Add a comment
Know the answer?
Add Answer to:
Summarize the following data structures and explain how a node can be inserted into each data...
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
  • By definition, the height of a node in a binary tree is the number of edges...

    By definition, the height of a node in a binary tree is the number of edges along the longest path from the node to any leaf. Assume the following node structure struct TreeNode! int data; node Type right; // points to right child node Type "left; // points to left child }; Write a recursive function that takes a pointer to a node in a binary tree and returns its height. Note: the height of a leaf node is o...

  • Question 2 (10 marks) Linked Data Structures: Answer both parts below. Part A (5 marks): The...

    Question 2 (10 marks) Linked Data Structures: Answer both parts below. Part A (5 marks): The in-order traversal of a binary tree processes the nodes in the order ABCD. The post-order traversal of the same binary tree processes the nodes in the order ACDB. Draw the tree below. Part B (5 marks) Inserting one element at a time, what is the best case time complexity to add n elements to an initially empty linked list so that the stored values...

  • 13. Given the following structure struct node { struct node *next; int id; }; Write a...

    13. Given the following structure struct node { struct node *next; int id; }; Write a code to insert a new node into the linked list as the last node of the linked list struct node *insertLast(stuct node **head, int newId) { } 14. Given the following structure struct node { struct node *next; int id; }; Write a code to insert a new node into the linked list after a node with the same id as the input parameter...

  • 8:08 33. Examine the following binary search tree and answer the question. The numbers on the...

    8:08 33. Examine the following binary search tree and answer the question. The numbers on the circles labels so that we can talk about the nodes, they are NOT values in the key members of the binary tree. (a). If an item is to be inserted into the tree whose key data member is less than the key data member in node 1 but greater than the key data member in node 5, where would it be inserted? (b) If...

  • In C++ Given a pointer to the root of a binary search tree (has left, right,...

    In C++ Given a pointer to the root of a binary search tree (has left, right, and parent pointers as well as a data section ) write a function (or functions) which will return an STL list (you should not define this class, it’s already included) with all of the values from the tree in sorted order. Your code should run in theta(N) time. for the second part,.given a pointer to the first node of a linked list, you are...

  • QUESTION 1 In a tree, a ____ is a node with successor nodes. root child descendent...

    QUESTION 1 In a tree, a ____ is a node with successor nodes. root child descendent parent sibling QUESTION 2 In a tree, the ____ is a measure of the distance from a node to the root. count degree branch height level QUESTION 3 Which of the following is not a characteristic of a binary search tree? Each node has zero, one, or two successors. The preorder traversal processes the node first, then the left subtree, and then the right...

  • Interfaces 1. What is inside an interface definition? What does a class do to an interface...

    Interfaces 1. What is inside an interface definition? What does a class do to an interface and what keyword is involved? How does a class do this to an interface (what should we find in the class)? Can an interface have a generic parameter? How do you instantiate an interface as an object? What methods can’t you use on an interface type? Abstract Data Types 2. What does an ADT define? Does an ADT specify programming language and/or data structures...

  • Course: Data Structures A) Draw the Binary Search Tree if the following data is added to...

    Course: Data Structures A) Draw the Binary Search Tree if the following data is added to a tree in the following order: 40,20, 10, 30, 60, 50, 70, 80. B) How would the tree look like if you remove "40" from the tree? Explain why the tree changes the way you are drawing it.

  • Could someone please summarize the following for my programming class? They are study questions for java...

    Could someone please summarize the following for my programming class? They are study questions for java What an association list is. How to test if an association list is empty. How to find the value associated with a key in an association list. How to add a key-value pair to an association list. How to delete a key-value pair from an association list. How efficient an association list is (using O notation). What a circular list is. What a circular...

  • The goal of this assignment is to reinforce the tree data structure in C++. Specifically, the...

    The goal of this assignment is to reinforce the tree data structure in C++. Specifically, the assignment is to do the following: Binary search trees have their best performance when they are balanced, which means that at each node, n, the height of the left subtree of n is within one of the height of the right subtree of n. Write a function (and a test program) which takes a sorted list of entries and produces a balanced binary search...

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
ADVERTISEMENT