Question

In: Computer Science

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, 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

import java.util.Stack;

// class Node.

class Node {

    int key;

    Stack<Integer> stack;

    Node left, right;

    // constructor

    Node(int value) {

        key = value;

        stack = new Stack<Integer>();

        left = right = null;

    }

}

public class BinaryTree {

    Node root;

    BinaryTree() {

        root = null;

    }

    void insert(int key) {

        root = recInsert(root, key);

    }

    public Node recInsert(Node root, int key) {

        if (root == null) {

            root = new Node(key);

            return root;

        }

        if (root.key == key) {

            root.stack.add(key);

            return root;

        }

        if (key < root.key) {

            root.left = recInsert(root.left, key);

        }

        else if (key > root.key) {

            root.right = recInsert(root.right, key);

        }

        return root;

    }

    void deleteKey(int key) {

        root = checkDelete(root, key);

    }

    Node checkDelete(Node root, int key) {

        if (root == null) {

            return root;

        }

        if (key < root.key)

            root.left = recDelete(root.left, key);

        else if (key > root.key)

            root.right = recDelete(root.right, key);

        else {

            if (!root.stack.isEmpty()) {

                root.stack.pop();

                return root;

            }

            else {

                root = recDelete(this.root, key);

            }

        }

        return root;

    }

    Node recDelete(Node root, int key) {

        if (root == null) {

            return root;

        }

        if (key < root.key)

            root.left = recDelete(root.left, key);

        else if (key > root.key)

            root.right = recDelete(root.right, key);

        else {

            if (root.left == null)

                return root.right;

            else if (root.right == null)

                return root.left;

            root.key = findMin(root.right);

            root.right = recDelete(root.right, root.key);

        }

        return root;

    }

    int findMin(Node root) {

        int min = root.key;

        while (root.left != null) {

            min = root.left.key;

            root = root.left;

        }

        return min;

    }

    void inorder() {

        recInorder(root);

        System.out.println();

    }

    void recInorder(Node root) {

        if (root != null) {

            recInorder(root.left);

            System.out.print(root.key + " ");

            recInorder(root.right);

        }

    }

    public static void main(String[] args) {

        BinaryTree bt = new BinaryTree();

        bt.insert(4);

        bt.insert(8);

        bt.insert(4);

        bt.insert(4);

        bt.insert(2);

        bt.insert(10);

        bt.insert(6);

        bt.insert(12);

        bt.inorder();

        bt.deleteKey(4);

        bt.inorder();

        bt.deleteKey(4);

        bt.inorder();

        bt.deleteKey(4);

        bt.inorder();

    }

}

PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT SECTION


Related Solutions

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...
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.
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...
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:...
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition. If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode *find(int item) { //implement code here return nullptr; } int main() {    root = new...
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...
Binary Tree Create a binary search tree using the given numbers in the order they’re presented....
Binary Tree Create a binary search tree using the given numbers in the order they’re presented. State if the resulting tree is FULL and/or BALANCED. 37, 20, 18, 56, 40, 42, 12, 5, 6, 77, 20, 54
IN JAVA Given a binary search tree, extract min operation finds the node with minimum key...
IN JAVA Given a binary search tree, extract min operation finds the node with minimum key and then takes it out of the tree. The program can be C++/Java or C-style pseudocode. Do not use function call to either Min or delete done in the class. Write this from scratch. (No need to ensure AVL properties, just show it for a generic BST) Node * extract min(Node * x) { }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT