Question

In: Computer Science

Create a binary search tree of stacks where each node contains a key and a stack.

IN C++
 
Create a binary search tree of stacks where each node contains a key and a stack. In this binary search tree, when adding a new element to the tree it is inserted based off of its key value; if that key value already exist then the element is pushed into the stack at that location, if the key value does not exist a node element is added to the tree to reflect that key, and a empty stack is created where you will push the element to be inserted. When removing elements from the tree, you will pop elements at that specific key until the stack is empty; when the stack becomes empty that node will be removed from the tree and one of its decedents need to take its place in the tree.

Solutions

Expert Solution

// C++ function to search a given key in a given BST

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);

}

// C++ program to demonstrate insertion

// in a BST recursively.

#include
using namespace std;

class BST
{
   int data;
   BST *left, *right;

   public:
  
   // Default constructor.
   BST();
  
   // Parameterized constructor.
   BST(int);
  
   // Insert function.
   BST* Insert(BST *, int);
  
   // Inorder traversal.
   void Inorder(BST *);
};

// Default Constructor definition.
BST :: BST() : data(0), left(NULL), right(NULL){}

// Parameterized Constructor definition.
BST :: BST(int value)
{
   data = value;
   left = right = NULL;
}

// Insert function definition.
BST* BST :: Insert(BST *root, int value)
{
   if(!root)
   {
       // Insert the first node, if root is NULL.
       return new BST(value);
   }

   // Insert data.
   if(value > root->data)
   {
       // Insert right node data, if the 'value'
       // to be inserted is greater than 'root' node data.
      
       // Process right nodes.
       root->right = Insert(root->right, value);
   }
   else
   {
       // Insert left node data, if the 'value'
       // to be inserted is greater than 'root' node data.
      
       // Process left nodes.
       root->left = Insert(root->left, value);
   }
  
   // Return 'root' node, after insertion.
   return root;
}

// Inorder traversal function.
// This gives data in sorted order.
void BST :: Inorder(BST *root)
{
   if(!root)
   {
       return;
   }
   Inorder(root->left);
   cout << root->data << endl;
   Inorder(root->right);
}

// Driver code
int main()
{
   BST b, *root = NULL;
   root = b.Insert(root, 50);
   b.Insert(root, 30);
   b.Insert(root, 20);
   b.Insert(root, 40);
   b.Insert(root, 70);
   b.Insert(root, 60);
   b.Insert(root, 80);

   b.Inorder(root);
   return 0;
}

 


Related Solutions

In java, create a binary search tree (without using additional libraries) of stacks where each node...
In java, create a binary search tree (without using additional libraries) of stacks where each node contains a key and a stack. In this binary search tree, when adding a new element to the tree it is inserted based off of its key value; if that key value already exist then the element is pushed into the stack at that location, if the key value does not exist a node element is added to the tree to reflect that key,...
Question 3: Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
Question 3: Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree. a) Discuss and justify your approach to address each possible case. b) Is the new tree (with the deleted node removed) unique? Discuss your answer. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree. a) Discuss and justify your approach to address each possible case. b) Is the new tree (with the deleted node removed) unique? Discuss your answer. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree. a) Discuss and justify your approach to address each possible case. b) Is the new tree (with the deleted node removed) unique? Discuss your answer. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or...
Question - 1 Using the structural node and methods discussed in Binary Search Tree lectures, create...
Question - 1 Using the structural node and methods discussed in Binary Search Tree lectures, create a method for the Binary Search Tree that takes an unsorted input list and constructs a Binary Search Tree based on its values. Any duplicate value will only appear once on the tree. This method outputs a Binary Search Tree structure (not an enumeration of the tree). Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary...
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary Tree Node structure class Node: # Constructor to create a new node def __init__(self, data): self.data = data self.left = None self.right = None class BSTree(): def __init__(self, rootdata): self.root = Node(rootdata)    def insert(self, data, cur_node): if data < cur_node.data: if cur_node.left == None: cur_node.left = Node(data) else: self.insert(data, cur_node.left)    elif data > cur_node.data: if cur_node.right == None: cur_node.right = Node(data) else:...
1. Draw a binary search tree as a single root node holding a string as the...
1. Draw a binary search tree as a single root node holding a string as the data element. Each string inserted into a node in the tree will be for a character in a game. Then, draw a new tree each time you insert a new node into the tree holding a string Insert 4 nodes total, including the root. This means the new nodes will need to be inserted at the correct child to maintain the BST property.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT