Question

TkA CHRI - TREE UTH A HEAPOF PRESDNS CENETH CSC 236-Lab 6 (2 programs) trees 1. Given the two binary trees below: 14 18 16) W

please do this lab in Java in given instructions by tomorrow morning.

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

code

solution

Note:please post other questions as a separated

1 // Binary SearchTree.java class Binary SearchTree class Binary_node int nodekey Binary_node node_left, node_right; public Badd_Record (rootnode, nodekey); rootnode /recursive function Binary_node add_Record (Binary_node rootnode, int nodekey) { / cvoid inorderRec (Binary_node rootnode) if (rootnode != null) inorderRec (rootnode.node left); System.out.println(rootnode.nodreturn Nnde; single node parent private int single_ParentHelp (Binary_node rootnode, int count) { if (rootnode=null||(rootnodtree 1.insert (1) tree 1.insert (2); tree 1.insert (4); tree 1.insert (5); tree_1.insert (3); tree_1.insert (6); System.out.pSystem.out.printlnTree 2); tree 2.inordertravarsel ); tree 2.swap (); System.out.println(tree_2 in swapped order); tree 2

//output

D:N>javac Binary_SearchT ree.java DENjava Binary_SearchT ree Tree 1 tree_1 in swapped order single parent in tree 1 Tree 2 :

//copyable code

1

// Binary_SearchTree.java

class Binary_SearchTree

{

     class Binary_node

    {

         int nodekey;

         Binary_node node_left, node_right;

         public Binary_node(int node_item)

        {

             nodekey = node_item;

             node_left = node_right = null;

         }

     }

  

     Binary_node rootnode;

     // Constructor

     Binary_SearchTree()

    {

         rootnode = null;

     }

     // add_Record()

     void insert(int nodekey)

    {

        rootnode = add_Record(rootnode, nodekey);

     }

   

     // recursive function

     Binary_node add_Record(Binary_node rootnode, int nodekey)

    {

         // check the treea

         if (rootnode == null) {

             rootnode = new Binary_node(nodekey);

             return rootnode;

         }

       

         if (nodekey < rootnode.nodekey)

             rootnode.node_left = add_Record(rootnode.node_left, nodekey);

         else if (nodekey > rootnode.nodekey)

             rootnode.node_right = add_Record(rootnode.node_right, nodekey);

      

         return rootnode;

     }

     // method to call the InorderRec()

     void inordertravarsel()

    {

        inorderRec(rootnode);

     }

     void inorderRec(Binary_node rootnode)

    {

         if (rootnode != null)

        {

             inorderRec(rootnode.node_left);

             System.out.println(rootnode.nodekey);

             inorderRec(rootnode.node_right);

         }

     }

  

  

    void swap()

    {

        swap_Sub_trees(rootnode);

     }

    // swap of subtrees

    Binary_node swap_Sub_trees(Binary_node Nnde)

     {

      

         if (Nnde == null)

             return Nnde;

        

         Binary_node node_left = swap_Sub_trees(Nnde.node_left);

         Binary_node node_right = swap_Sub_trees(Nnde.node_right);

        

         Nnde.node_left = node_right;

         Nnde.node_right = node_left;

         return Nnde;

     }

      

    // single node parent

     private int single_ParentHelp(Binary_node rootnode,int count)

    {

         if(rootnode==null||(rootnode.node_left==null&&rootnode.node_right==null))

             return count;

         count = single_ParentHelp(rootnode.node_left, count);

         if(rootnode.node_left==null||rootnode.node_right==null)

             count ++;

         count = single_ParentHelp(rootnode.node_right, count);

         return count;

     }

  

     public int single_Parent()

    {

         return single_ParentHelp(rootnode, 0);

     }

  

     // main method

     public static void main(String[] args)

    {

         Binary_SearchTree tree_1 = new Binary_SearchTree();

        Binary_SearchTree tree_2 = new Binary_SearchTree();

        

         tree_1.insert(1);

         tree_1.insert(2);

         tree_1.insert(4);

         tree_1.insert(5);

         tree_1.insert(3);

         tree_1.insert(6);

       

   

        System.out.println("Tree 1");

         tree_1.inordertravarsel();

        tree_1.swap();

        System.out.println("tree_1 in swapped order");

        tree_1.inordertravarsel();

        System.out.println("single parent in tree_1 : "+tree_1.single_Parent());

           

         tree_2.insert(14);

         tree_2.insert(4);

         tree_2.insert(3);

         tree_2.insert(9);

         tree_2.insert(7);

         tree_2.insert(5);

         tree_2.insert(15);

         tree_2.insert(18);

         tree_2.insert(16);

         tree_2.insert(17);

         tree_2.insert(20);

       

     

        System.out.println("Tree 2");

         tree_2.inordertravarsel();

        tree_2.swap();

        System.out.println("tree_2 in swapped order");

        tree_2.inordertravarsel();

        System.out.println("single parent in tree_2 : "+tree_2.single_Parent());

     }

}

Add a comment
Know the answer?
Add Answer to:
please do this lab in Java in given instructions by tomorrow morning. TkA CHRI - TREE...
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
  • 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of...

    in java ..write all complete program from a- e 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and the resulting trees. Note: To test your algorithm, first create a binary search tree. Write a method called singleParent,...

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

  • In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a...

    In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a basic employee schema within a company. The Employee class will be an abstract class that will contain methods applicable to all employees. You will then create 2 classes called SoftwareEngineer and ProductManager. Both are different employee types based on occupation. You will create an interface called Developer which will consist of specific methods that apply to only Developers (e.g. SoftwareEngineer class will implement this,...

  • in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...

    in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • *****************************In Java***************************************In Java***********************************In Java************************* In this problem, you will implement various algorithms operating on binary search...

    *****************************In Java***************************************In Java***********************************In Java************************* In this problem, you will implement various algorithms operating on binary search trees. We have provided with you a standard implementation of a generic BST in BinarySearchTree.java. Note that this class is an abstract class, which means that some of its methods are not implemented. In previous assignments, you have implemented interfaces which specified methods that you needed to write. Very similarly, an abstract class is a class with some unimplemented methods (it can be thought...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create a "Hello C++! I love CS52" Program 10 points Create a program that simply outputs the text Hello C++!I love CS52" when you run it. This can be done by using cout object in the main function. 2. Create a Class and an Object In the same file as...

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

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
ADVERTISEMENT