Question

c++ Write a class that will create a binary tree that can hold values of string...

c++

Write a class that will create a binary tree that can hold values of string data type, define and implement insert, delete, search, functions, and a constructor and a destructor. Demonstrate the insertion, searching, and deletion functions in a program.

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

Binary Search Tree, is a node-based binary tree data structure which has the following properties:

  • The left subtree of a node contains only nodes with keys lesser than the node’s key.
  • The right subtree of a node contains only nodes with keys greater than the node’s key.
  • The left and right subtree each must also be a binary search tree.
    There must be no duplicate nodes.

The above properties of Binary Search Tree provide an ordering among keys so that the operations like search, minimum and maximum can be done fast. If there is no ordering, then we may have to compare every key to search a given key.

Searching a key
To search a given key in Binary Search Tree, we first compare it with root, if the key is present at root, we return root. If key is greater than root’s key, we recur for right subtree of root node. Otherwise we recur for left subtree.

struct node* search(struct node* root, int key)

{

    // Base Cases: root is null or key is present at root

    if (root == NULL || root->key == key)

       return root;

     

    // Key is greater than root's key

    if (root->key < key)

       return search(root->right, key);

  

    // Key is smaller than root's key

    return search(root->left, key);

}

Insertion of a key
A new key is always inserted at leaf. We start searching a key from root till we hit a leaf node. Once a leaf node is found, the new node is added as a child of the leaf node.

#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 inorder(struct node *root)

{

    if (root != NULL)

    {

        inorder(root->left);

        printf("%d \n", root->key);

        inorder(root->right);

    }

}

   

/* 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;

}

Deletion

Node to be deleted is leaf: Simply remove from the tree.

Node to be deleted has only one child: Copy the child to the node and delete the child

Node to be deleted has two children: Find inorder successor of the node. Copy contents of the inorder successor to the node and delete the inorder successor. Note that inorder predecessor can also be used.

#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 inorder(struct node *root)

{

    if (root != NULL)

    {

        inorder(root->left);

        printf("%d ", root->key);

        inorder(root->right);

    }

}

  

/* 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

        node->right = insert(node->right, key);

  

    /* return the (unchanged) node pointer */

    return node;

}

  

/* Given a non-empty binary search tree, return the node with minimum

   key value found in that tree. Note that the entire tree does not

   need to be searched. */

struct node * minValueNode(struct node* node)

{

    struct node* current = node;

  

    /* loop down to find the leftmost leaf */

    while (current && current->left != NULL)

        current = current->left;

  

    return current;

}

  

/* Given a binary search tree and a key, this function deletes the key

   and returns the new root */

struct node* deleteNode(struct node* root, int key)

{

    // base case

    if (root == NULL) return root;

  

    // If the key to be deleted is smaller than the root's key,

    // then it lies in left subtree

    if (key < root->key)

        root->left = deleteNode(root->left, key);

  

    // If the key to be deleted is greater than the root's key,

    // then it lies in right subtree

    else if (key > root->key)

        root->right = deleteNode(root->right, key);

  

    // if key is same as root's key, then This is the node

    // to be deleted

    else

    {

        // node with only one child or no child

        if (root->left == NULL)

        {

            struct node *temp = root->right;

            free(root);

            return temp;

        }

        else if (root->right == NULL)

        {

            struct node *temp = root->left;

            free(root);

            return temp;

        }

  

        // node with two children: Get the inorder successor (smallest

        // in the right subtree)

        struct node* temp = minValueNode(root->right);

  

        // Copy the inorder successor's content to this node

        root->key = temp->key;

  

        // Delete the inorder successor

        root->right = deleteNode(root->right, temp->key);

    }

    return root;

}

Add a comment
Know the answer?
Add Answer to:
c++ Write a class that will create a binary tree that can hold values of string...
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
  • Binary Tree Template Write your own version of a class template that will create a binary...

    Binary Tree Template Write your own version of a class template that will create a binary tree that can hold values of any data type. Demonstrate the class with a driver program. Place your binary tree template in it's own header file, Btree.h. Include methods for the following: inserting new values into the tree removing nodes from the tree searching the tree returning the number of nodes in the tree displaying the contents of the tree using preorder traversal Your...

  • LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display...

    LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display the output using inorder, preorder and postorder tree traversal methods.

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

  • C++ : Implement the ordered list type using a pointer-based binary search tree. The elements of...

    C++ : Implement the ordered list type using a pointer-based binary search tree. The elements of the class's lists are integers, but it should be easy to change this type. The class should implement the following operations on ordered lists of its element type: Initialize a list to be empty (the default constructor). A destructor that deletes the dynamic memory in the tree that represents a list. Re-initialize an existing list to be empty. Insert a value into a list,...

  • in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...

    in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...

  • C++ program Create a class Time having data members HH, MM, SS of type integer. Write...

    C++ program Create a class Time having data members HH, MM, SS of type integer. Write a C++ program to do the following: 1. Use a Constructor to initialize HH, MM, SS to 0. 2. Create two Objects of this class and read the values of HH, MM, SS from the user for both objects (Using proper member functions). 3. Use a binary + operator to add these two objects created (Store & display result using separate object). 4. Use...

  • CSC 430              GRAPH PROJECT Implement a graph class using an adjacency matrix or an adjacency list....

    CSC 430              GRAPH PROJECT Implement a graph class using an adjacency matrix or an adjacency list. The class should have a constructor, a copy constructor, a destructor and all the methods necessary to add/delete vertices, add/delete edges… Write a menu-driven program to THOROUGHLY check your class and all the functions included in your program. You can choose the data type. Allow the user to continue with operations as long as he/she wants to. Your program should check if an operation...

  • Programming in C++ Implement a BST (Binary Search Tree) and test your program. (Linked List based.)...

    Programming in C++ Implement a BST (Binary Search Tree) and test your program. (Linked List based.) You should test at least the three major functions. (Insert, Retrieve, and Delete).

  • This is binary search tree problem. The program reads the text file, and creates a binary...

    This is binary search tree problem. The program reads the text file, and creates a binary search tree based on the words in the file. I can create the tree but I also have to store 'the order of insertion' in each node.   For example, if text includes "the" 3 times and it is the 1st, 5th, and 9th word in the file, in binary search tree, one node should have string value "the" and array list{1, 5, 9}. How...

  • Write a program in C fro Binary Search tree using following functions 1. Insertion operation using...

    Write a program in C fro Binary Search tree using following functions 1. Insertion operation using recursion 2. Deletion operation 3. Minimum/Maximum of a BST 6. Reorganize the tree so that the tree height is minimum 7. Print all the nodes from the node to the path to the root 8. Find the lowest common shared node between two given nodes

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