Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Trees sure are neat. Let’s take this slow and simple and make a tree of integers

ID: 3673923 • Letter: T

Question

Trees sure are neat. Let’s take this slow and simple and make a tree of integers. Download this file, the driver file, and fill in the code to create a tree of integers.

The class IntBSTree is given fill in the following methods

insert: This method returns nothing and takes in an integer value that is then placed as a new node in the tree based on the binary tree properties. A reminder values greater than the parent go to the right subtree and values smaller go to the left subtree. Also it may be a good idea to use a recursive method in order to place these values.

printInorder: This method which returns nothing and has no parameters prints the in order traversal of the tree. For in order traversal each left subtree must be visited, then the value is printed out, and finally each of the right subtrees is visited. It is a good idea to make a recursive method to assist with this. Also if done correctly in-order traversals print out each of the integers in ascending order.

getDepth: The depth of a node is the number of edges from the root to that number. This method returns nothing and takes in a parameter corresponding to the integer value of a node whose depth is returned. If the value is not in the tree a -1 should be returned. Again a recursive helper method may be useful to solve this.

Correct Print Out:

Int BST Tester!

Creating tree

Populating Tree with values

Testing insertion by in-order traversal

1

2

3

4

5

6

7

8

9

10

Getting depth of 6

4

Getting depth of 14

-1

DRIVER FILE:

FIRST FILE:

Explanation / Answer

Try this, I hope this will help to you and you can understand better..............

public class IntBSTree{

        private class Node

        {

                private int data;

                private Node leftChild;

                private Node rightChild;

                public Node(int aData)

                {

                        this.data = aData;

                        this.leftChild = null;

                        this.rightChild = null;

                }

        }

        private Node root;

        public IntBSTree()

        {

                root = null;

        }

       

        public void insert(int data)

        {

public void insertData(int data)

    {

        Node node=new Node(data,null,null);

        insert(node,this.root);

    }

    private Node insert(Comparable<Node> node,Node root1)

    {

            if(root1==null)

            {

                root1=new Node(((Node)node).getData(),null,null);

                if(this.root==null)

                {

                    this.root=root1;

                }

            }

            else if(node.compareTo(root1) <0)

            {

                root1.setLeftChild(insert(node,root1.getLeftChild()));

            }

            else if(node.compareTo(root1) >0)

            {

                root1.setLeftChild(insert(node,root1.getRightChild()));

            }

}

}

print void inOrder (Node root)

{

if(root == null) return;

inOrder( root.leftNode() );

root.printNodeValue();

inOrder( root.rightNode() );

}

int currentDepth, depth;

public int depth(BinaryNode n) {

    if (n != null) {

        currentDepth++;

         

        // record total depth if current depth is greater than total depth

        if (currentDepth > depth) {

            depth = currentDepth;

        }

        // recursively traverse entire tree

        depth(n.left);

        depth(n.right);

         

        // decrement as we traverse up the binary tree

        currentDepth--;

    }

    return depth;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote