Question

Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak...

Q. write the algorithm for each function in this code:

void insert(int x, node*&p)
{
//cheak if the pointer is pointing to null.
if (p==NULL)
{
    p = new node;
    p->key=x;
    p->left=NULL;
    p->right=NULL;
}
else
{
    //Cheak if the element to be inserted is smaller than the root.
    if (x < p->key)
    {
      //call the function itself with new parameters.
      insert(x,p->left);
    }
    //cheak if the alement to be inserted is greater then the root
    else if (x>p->key)
    {
      //call the function itself with new parameters.
      insert(x,p->right);
    }
    else
    {
      cout<<"Element already Exits! \n";
    }
}
}

void search (int x, node *&p)
{
node*tempP=p;
if(p==NULL)
{
    cout<<"Tree is Empty \n";
}
else
{
    while(p != NULL)
    {
      if(x < p->key)
      {
        p=p->left;
      }
      else if (x>p->key)
      {
        p=p->right;
      }
      else
      {
        cout<<"Element found\n";
        p= tempP;
        return;
      }
    }
   p= tempP;
   cout<<"Element NOT found \n";
}
}

//traverse from left to root to right
void inorder(node*p)
{
if(p== NULL)
{
    return;
}
else
{
    inorder(p->left);
    cout<<p->key<<"\t";
    inorder(p->right);
}
}

void preorder(node*p)
{
if(p==NULL)
{
    return;
}
else
{
    cout <<p->key <<"\t";
    preorder(p->left);
    preorder(p->right);
}
}

void postorder(node*p)
{
if(p==NULL)
{
    return;
}
else
  {
    postorder(p->left);
    postorder(p->right);
    cout<<p-> key<<"\t";
}
}

void remove(int x, node*&p)
{
   if (p ==NULL)
   {
     cout<<"Element NOT found\n";
   }
   //traverse left or right to reach the node to be //deleted
   else if (x < p->key)
   {
     remove(x,p->left);
   }
   else if (x > p->key)
   {
     remove(x,p->right);
   }
   //case 1: delete leaf node
   else if ((p->left == NULL) && (p->right ==NULL))
   {
     p=NULL;
   }
   // case 2: delete node with one child
   else if (p->left == NULL)
   {
     p=p->right;
   }
   else if (p->right ==NULL)
   {
     p=p->left;
   }
   //case 3: delete node with two children
   else
   {
     p->key=deletemax(p->left);
   }
}

int deletemax(node*&p)
{
   node* temp;
   int num;
   cout<<"Delete max number \n";
   if(p->right == NULL)
   {
     num=p->key;
     p=p->right;
     return num;
   }
   else
   {
   num=deletemax(p->right);
   }
}};

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

//algorithm for insert method
input:x,root
step1)starting from root, if root is empty then create new node with data x and point it to root
step2)if root is not empty, compare the inserting element with root element, if less than root element then recurse left, if greater than root recurse right, if eqaul then print already exist

//algorithm for search method
input:x,root//p
step1)if root is empty print: tree is empty
step2)if root is empty,loop it to: compare the x element with root(p), if less than root then change root to left pointer, if greater than root then change root to right pointer, if eqaul then print Element found and return from method, iterate until root is empty
step3)print Not found

//algorithm for inorder:
step1)traverse left subtree
step2)visit root
step3)traverse right subtree


//algorithm for preorder:
step1)visit root
step2)traverse left subtree
step3)traverse right subtree

//algorithm for postorder:
step1)traverse left subtree
step2)traverse right subtree
step3)visit root

//algorithm for remove method
input: x,root
step1)if root is empty then print :element not found
step2)if root is not empty, then compare x with root element, if x less then root then recurse left, if x greater than root then recurse right
step3)if x equals root, if it has 0 child's then simply delete it, if it has one child then directly update link with child node, if it has two childs then call deletemax

//algorithm for deletemax
step1)recursively traverse right until a leaf node is found
step2)then return//which is max node

Add a comment
Know the answer?
Add Answer to:
Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak...
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
  • Question - modify the code below so that for a node, the value of every node...

    Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method:  InOrder(Node theRoot),  PreOrder(Node theRoot),  PostOrder(Node theRoot),  FindMin(),  FindMax(),  Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public double dData;           // data item   public Node leftChild;         // this node's left child   public Node rightChild;        // this node's right child   public void displayNode()      // display ourself      {      System.out.print('{');      System.out.print(iData);      System.out.print(", ");      System.out.print(dData);      System.out.print("} ");      }   }  // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • Lab 6 Help

    I need help with this lab assignment. I use C++, thank you. Derive a MinHeap class from the BST class of your Lab 4 code.Define/Override only the Search/Insert/Delete methods in the new class.Write a new main for this lab which:Will only be a test program for the MinHeap.Declares and creates a MinHeap object as necessary.Declares and creates the Dollar objects as necessary.Inserts the 20 Dollar objects specified in Lab 4 in the same order.Performs all 4 traversals after the inserting...

  • write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the word...

    write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...

  • Can you take a look at my code that why the maxDepth function is not working?...

    Can you take a look at my code that why the maxDepth function is not working? public class BinaryTree {          class Node{        int key;        Node left,right;               public Node(int item) {            key = item;            left = right = null;        }    }       Node root;       public void BinaryTree(){        root = null;    }           void...

  • Take the following code for a binary source tree and make it include the following operations....

    Take the following code for a binary source tree and make it include the following operations. bool replace(const Comparable & item, const Comparable & replacementItem); int getNumberOfNodes() const; (a) The replace method searches for the node that contains item in a binary search tree, if it is found, it replaces item with replacementItem. The binary tree should remain as a binary search tree after the replacement is done. Add the replace operation to the BinarySearchTree class. Test your replace using...

  • C++ Help with Test #1 (at bottom) to work properly. May adjust code to work or...

    C++ Help with Test #1 (at bottom) to work properly. May adjust code to work or add any additional code. Do not use global variables. #include <iostream> #include <string> using std::endl; using std::cout; using std::cin; using std::string; void pressAnyKeyToContinue() { printf("Press any key to continue\n"); cin.get(); } //This helps with testing, do not modify. bool checkTest(string testName, int whatItShouldBe, int whatItIs) {    if (whatItShouldBe == whatItIs) { cout << "Passed " << testName << endl; return true; } else...

  • C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please...

    C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please help me figure out. Thanks. C++ BST implementation (using a struct) Enter the code below, and then compile and run the program. After the program runs successfully, add the following functions: postorder() This function is similar to the inorder() and preorder() functions, but demonstrates postorder tree traversal. displayParentsWithTwo() This function is similar to the displayParents WithOne() function, but displays nodes having only two children....

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