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...
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 - 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...
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...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.
Please show work - trying to understand how to do this so I can apply it...
Please show work - trying to understand how to do this so I can apply it to other problems. Both Professor X and Professor Y agree that students who study for their examinations do better than those who do not. Both professors teach the same class, take a careful four level ranking of how hard their students studied and give a very similar examination. This year the average score in Professor X’s class and the average score in Professor Y’s...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT