Question

In: Computer Science

in JAVA: implement a class called tree (from scratch) please be simple so i can understand...

in JAVA: implement a class called tree (from scratch) please be simple so i can understand thanks!

tree(root)

node(value, leftchild,rightchild)

method: insert(value)

Solutions

Expert Solution

Program :

class Tree{ 
  
    /* Class containing left and right child of current node and key value*/
    class Node { 
        int key; 
        Node left, right; 
  
        public Node(int item) { 
            key = item; 
            left = right = null; 
        } 
    } 
  
    // Root of tree 
    Node root; 
  
    // Constructor 
    Tree() {  
        root = null;  
    } 
  
    // This method mainly calls insertRec() 
    void insert(int key) { 
       root = insertRec(root, key); 
    } 
      
    /* A recursive function to insert a new key in BST */
    Node insertRec(Node root, int key) { 
  
        /* If the tree is empty, return a new node */
        if (root == null) { 
            root = new Node(key); 
            return root; 
        } 
  
        /* Otherwise, recur down the tree */
        if (key < root.key) 
            root.left = insertRec(root.left, key); 
        else if (key > root.key) 
            root.right = insertRec(root.right, key); 
  
        /* return the (unchanged) node pointer */
        return root; 
    } 
  
    // This method mainly calls InorderRec() 
    void print()  { 
       traversal(root); 
    } 
  
    // A utility function to do inorder traversal of tree 
    void traversal(Node root) { 
        if (root != null) { 
            traversal(root.left); 
            System.out.print(root.key +" "); 
            traversal(root.right); 
        } 
    } 
    //Main method
    public static void main(String[] args) { 
        Tree tree = new Tree(); 
  
        tree.insert(55); 
        tree.insert(67); 
        tree.insert(89); 
        tree.insert(90); 
        tree.insert(23); 
        tree.insert(56); 
        tree.insert(80); 
        System.out.print("Output is :");
        tree.print(); 
    } 
} 

Output :

Thank You Have a great Day !!! Please do like.


Related Solutions

I was trying to implement a simple binary search tree using this given class of bst...
I was trying to implement a simple binary search tree using this given class of bst in c++ public: BST(); ~BST(); void insertKey(int newKey); bool hasKey(int searchKey); std::vector<int> inOrder(); int getHeight(); however; i am still required to use another class for the nodes as a pointer and i need to manage memory leak. in main we should ask for the numbers we need to insert in the binary search tree and also let the user end it with a letter...
In C++ and pls comment every line so I UNDERSTAND Implement a class named DynamicArray that...
In C++ and pls comment every line so I UNDERSTAND Implement a class named DynamicArray that has the following members: A pointer to hold a dynamically allocated array, of type int. A member variable to hold the size of the array. A default constructor, which will allocate an array of size 10 A parameterized constructor, which takes a size and use the size to allocate array. A copy constructor, which performs deep copy. A copy assignment operator, which performs deep...
(Java) Implement a RightThreadTree class as an extension of a BinarySearchTree A right-threaded tree is a...
(Java) Implement a RightThreadTree class as an extension of a BinarySearchTree A right-threaded tree is a binary search tree in which each right link that would normally be null is a "thread" that links that node to its inorder successor. The thread enables nonrecursive algorithms to be written for search and inorder traversals that are more efficient than recursive ones. Implement a RightThreadTree class as an extension of a BinarySearchTree. You will also need an RTNode that extends the Node...
Please use simple terms so I can understand better. thanks! Why do cells need to multiply?...
Please use simple terms so I can understand better. thanks! Why do cells need to multiply? Discuss a few reasons. Try to do this without using the word “reproduction” or “reproduce”. Define homologous chromosomes Discuss the chromosomal number of cells before mitosis and after mitosis Is mitosis involved in asexual reproduction? What are telomeres and why are they important? How does cytokinesis (cytoplasmic division) differ in animal cells vs plant cells? Define oncogene, proto-oncogene, neoplasm, tumor, and metastasis
I am in beginners java course so using the most simple/basic JAVA please complete the following...
I am in beginners java course so using the most simple/basic JAVA please complete the following CODE SEGMENTS. (2 parts) FIRST PART: Using ITERATIVE create a METHOD MAX that returns the max element in an ArrayList of ints and prints all of the elements. *you have to use an iterative in the method.* (just need to write the METHOD ONLY not a whole program(will do in 2nd part), assume you are given any numbers/integers. SECOND PART: Now write a class...
In C++, implement a class called setOper that provides several simple set operations. The class only...
In C++, implement a class called setOper that provides several simple set operations. The class only needs to deal with sets that are closed intervals specified by two real numbers; for example, the pair (2.5, 4.5) represent the interval [2.5, 4.5]. The following operations should be supported: - Check if the value x belongs to the given interval. - Check if the value x belongs to the intersection of two intervals. - Check if the value x belongs to the...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
Can you please write this in python and comment as well so I can understand what...
Can you please write this in python and comment as well so I can understand what yo are doing. Thank you. 1)Loop through list A, at each iteration, show the square root result of that element. Add proper text to your print function. A = [-4, 1, -16, 36, -49, 64, -128] 2)Create a counter variable, name it i and initialize it to 0. Using a for loop, count how many numbers are divisible by 3 in range of 1...
JAVA - Design and implement a class called Flight that represents an airline flight. It should...
JAVA - Design and implement a class called Flight that represents an airline flight. It should contain instance data that represent the airline name, the flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight...
Implement Java source code for a class called Temperature that does the following: a. Prompts the...
Implement Java source code for a class called Temperature that does the following: a. Prompts the user to input a temperature b. Prints “Too Hot!” when the input temperature is above 75 c. Prints “Too Cold!” when the input temperature is below 40 d. Prints “Just Right!” when the input temperature is 40 to 75 e. Be precise, include comments, prologue, etc. if needed.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT