Question

suppose you decide to change our binary Search Tree ADT to not allowing a duplicate element....

suppose you decide to change our binary Search Tree ADT to not allowing a duplicate element. how would you have to change the ADT specifications

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

#include <iostream>
#include <vector>
#include <sstream>
#include <queue>
using namespace std;

// Defines a structure to create a Node
struct Node
{
// Data member to store data
int data;
// Pointer to left child
Node *left;
// Pointer to right child
Node *right;
};// End of structure Node

// Class BST definition
class BST
{
// Creates an object of structure for root node
Node *root;
public:
// Default constructor
BST()
{
root = NULL;
}// End of default constructor

// Function to insert a node
void insert(int data)
{
// Create a new node
Node *newptr = new Node;
// Stores the data in node
newptr->data = data;
// Set the left and right pointer of the node to null
newptr->left = NULL;
newptr->right = NULL;

// Checks if root is null then it is the first node
if (root == NULL)
{
// Set the root to new node
root = newptr;
}// End of if condition
// Otherwise
else
{
// Create a temporary pointer of type node and point to root
Node *p = root;
// Create a parent pointer pointer of type node and assign null to it
Node *parent = NULL;
// Find the right location for the new node
// Loops till leaf node and node pointing to data is not equals to the inserted number
while ((p != NULL) && (p->data != data))
{
// Checks if the number to insert is less than current node data
if (data < p->data)
{
// Set the current node as parent
parent = p;
// Current node is pointing to its left
p = p->left;
}// End of if condition
// Otherwise number is greater than the current node data
else if (data > p -> data)
{
// Set the current node as parent
parent = p;
// Current node is pointing to its right
p = p->right;
}// End of else
else
cout<<"\n Duplicate element not allowed.";
}// End of while

// If the element is not in the BST
if (p == NULL)
{
// Checks if the number to insert is less than current node data
if (data < parent->data)
// Parent node left is pointing to new node
parent->left = newptr;
// Otherwise number is greater than the current node data
else if (data > parent->data)
// Parent node right is pointing to new node
parent->right = newptr;
else
cout<<"\n Duplicate element not allowed.";
}// End of if

// Otherwise node is already available
else
cout<<"\n Duplicate element not allowed."<<endl;
}// End of else
}// End of function

// Function for pre order traversal
void preOrderHelper(Node *p)
{
// Traverse: Root, Left, Right
// Checks if current node is not null
if (p != NULL)
{
// Displays the root node value
cout << p->data << " ";
// Moves towards left
preOrderHelper(p->left);
// Moves towards right
preOrderHelper(p->right);
}// End of if
}// End of function

// Function to call the function by passing root node to it
// For pre order traversal
void preOrder()
{
preOrderHelper(root);
}// End of function
};// End of class

// main function definition
int main()
{
// Creates a pointer of class BST
BST *t1 = new BST();

int n, num;

// Accepts number of elements require to insert
cout<<"\n Enter number of elements required to insert: ";
cin >> n;

// Loops till number of elements
for (int i = 0; i < n; i++)
{
// Accepts each element
cout<<"\n Enter the number to insert: ";
cin>>num;
// Calls the function to insert
t1->insert(num);
}// End of for loop
// Calls the function to display pre order
t1->preOrder();
return 0;
}// End of main function

Sample Output:

Enter number of elements required to insert: 6

Enter the number to insert: 10

Enter the number to insert: 22

Enter the number to insert: 33

Enter the number to insert: 10

Duplicate element not allowed.

Enter the number to insert: 55

Enter the number to insert: 66
10 22 33 55 66

Add a comment
Know the answer?
Add Answer to:
suppose you decide to change our binary Search Tree ADT to not allowing a duplicate element....
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
  • This is for java programming. We need a public method for our Binary Search Tree ADT...

    This is for java programming. We need a public method for our Binary Search Tree ADT that returns a reference to the information in the node with the "smallest" value in the tree. The signature of the method is public T min() a. Design an iterative version of the method. b. Design a recursive version of the method. c. Which approach is better? Explain.

  • 1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the...

    1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...

  • Q1: How many levels your binary search tree has (including level 0)? Is the binary search tree you created height balanc...

    Q1: How many levels your binary search tree has (including level 0)? Is the binary search tree you created height balanced? 2.1 Click the animations “Binary Search Tree”. Click “Insert” button to insert the following elements in the sequence, “50, 20, 30, 70, 90, 80, 40, 10, 5, 60, 85, 95”. http://algoanim.ide.sk/index.php?page=showanim&id=44 Q2: What is the insertion process of the binary search tree? The new identical element is inserted as left or right child of the existing same value? 2.3...

  • Although disallowing duplicate search keys in the ADT dictionary is reasonable for some applications, it is...

    Although disallowing duplicate search keys in the ADT dictionary is reasonable for some applications, it is just as reasonable to have an application that will allow duplicates. What are the implications of adding entries that are not identical but have the same search key? Specifically, what would the implementations of add, remove, and getValue do?

  • Suppose you started with an empty binary search tree. We've seen previously that inserting the keys...

    Suppose you started with an empty binary search tree. We've seen previously that inserting the keys 1, 2, 3, 4, 5, 6, 7 (in that order) would lead to a binary search tree whose shape we called degenerate. Propose a second ordering of the same keys that would also lead to a degenerate-shaped binary search tree. If possible, propose a third ordering of the same keys that would also lead to a degenerate-shaped binary search tree. If there are no...

  • Think about the DELETE(x) operation for a Binary Search Tree with no duplicate elements. Define the...

    Think about the DELETE(x) operation for a Binary Search Tree with no duplicate elements. Define the predecessor of a node x as the node whose value immediately precedes x if you sort all the node values in the tree smallest to largest. Define the successor of a node x similarly -- the one "just after" x. If x has a left child, is the predecessor of x always in x's left sub-tree? If x has a right child, is the...

  • Q1: How many levels your binary search tree has (including level 0)? Is the binary search...

    Q1: How many levels your binary search tree has (including level 0)? Is the binary search tree you created height balanced? 2.1 Click the animations “Binary Search Tree”. Click “Insert” button to insert the following elements in the sequence, “50, 20, 30, 70, 90, 80, 40, 10, 5, 60, 85, 95”. http://algoanim.ide.sk/index.php?page=showanim&id=44 Q2: What is the insertion process of the binary search tree? The new identical element is inserted as left or right child of the existing same value? 2.3...

  • Suppose you are given a balanced binary search tree with 15 nodes in it, containing the even numb...

    Suppose you are given a balanced binary search tree with 15 nodes in it, containing the even numbers from 2 to 30 inclusive. (a) (5 points) Draw this tree. (b) (3 points) Explain how you would check if the number 18 is in this tree, and state the number of operations this would take. (c) (2 points) Explain how you would insert the number 27 into this tree, and state the number of operations this should take.

  • 13inary Search Tree Using a binary search tree, you are tasked with building a dictionary program...

    13inary Search Tree Using a binary search tree, you are tasked with building a dictionary program which you can store a word with its definition. Each node of the tree will contain the word and definition. The word is what will be used as the key to sort our data. The dictionary should allow you to search for a word. If the word exist then the definition will display. You can also add words to the dictionary. For testing you...

  • Suppose you have two binary search trees P and Q. Let P and Q be the...

    Suppose you have two binary search trees P and Q. Let P and Q be the number of elements inP and Q, and let hp and ho be the heights of P and Q. Assume that that is, hp ho < P IQ and A. Give a destructive algorithm for creating a binary search tree containing the union PUQ that runs in time O(|P2) in the worst case. B. Assume now that it is known that the largest element of...

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