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

Thank you in advance! I\'m currently stuck making my binary search tree in java.

ID: 3761835 • Letter: T

Question

Thank you in advance! I'm currently stuck making my binary search tree in java. (made in eclipse)

Assisnment details : In the BinarySearchTree class implement 4 methods.

a) Implement the traverseAndAddNode method that will add a node to the Tree by comparing the data of each node. It is a recursive method, for more information please see the comment in the method.

b) Implement 3 different Traversal methods which will be used to traverse the tree.

i. For inOrderTraversal, print the data of each node using in-order traversal. You can use System.out.println() to print it.

ii. For preOrderTraversal, print the data of each node using pre-order traversal. You can use System.out.println() to print it.

iii. For postOrderTraversal, print the data of each node using post-order traversal. You can use System.out.println() to print it.

3. In the TreeTester class, do the following things.

a) Create a BinarySearchTree object.

b) Add 18, 15, 13, 16, 12, 20, 34, 56, 98, 20 to the tree.

c) Use the three different traversal methods to traverse the tree, if you are using In-order traversal you will be able to see the integers are sorted in order.

d) Write in the comment part in your code how each traverse can be useful.

This is what I currently have:

Node Class:

public class Node {

private int data;

private Node leftChild;

private Node rightChild;

   public Node(int data) {

       this.data = data;

   }

   public int getData() {

       return data;

   }

   public Node getLeftChild() {

       return leftChild;

   }

   public void setLeftChild(Node leftChild) {

       this.leftChild = leftChild;

   }

   public Node getRightChild() {

       return rightChild;

   }

   public void setRightChild(Node rightChild) {

       this.rightChild = rightChild;

   }

@Override

   public String toString() {

       String out = new String();

       out = .toString();

       return out;

   }

}

Binary Search Tree Class:

import java.util.Scanner;

public class BinarySearchTree {

   private Node root;

public void add(int data) {

       Node nodeToAdd = new Node(data);

       if (root == null) {

           //always add the root first

           root = nodeToAdd;

       }

       else {          

           traverseAndAddNode(root, nodeToAdd);

       }

   }

   private void traverseAndAddNode(Node node, Node nodeToAdd) {

       //this like almost all methods having to do with trees is recursive

       // if the data of nodeToAdd is less than the data of node, traverse left child of node,

       //else traverse right child

       // we do this until the path we want to take is equal to null then we place the node there.

   }

   public void traverse(Scanner keyboard) {

       if (root != null) {

           System.out

                   .print("Please chose a way to traverse: 1 for preOrder 2 for inOrder 3 for postOrder:");

           int choice = keyboard.nextInt();

           Node nodeToTraverse = root;

           switch (choice) {

           case 1:

               preOrderTraversal(nodeToTraverse);

               break;

           case 2:

               inOrderTraversal(nodeToTraverse);

               break;

           case 3:

               postOrderTraversal(nodeToTraverse);

               break;

           default:

               System.out.println("Wrong Choice!");

               break;

           }

       } else {

           System.out.println("This an empty tree!");

       }

   }

   private void inOrderTraversal(Node node) {

       // implement inOrderTraversal here!

   }

   private void preOrderTraversal(Node node) {

       // implement preOrderTraversal here!

   }

   private void postOrderTraversal(Node node) {

       // implement postOrderTraversal here!

   }

}

Tree Tester Class is currently nothing...just a main:

public class TreeTester {

   public static void main(String[] args) {

  

      

      

   }

}

Explanation / Answer

#include using namespace std; // Node class class Node { int key; Node* left; Node* right; public: Node() { key=-1; left=NULL; right=NULL; }; void setKey(int aKey) { key = aKey; }; void setLeft(Node* aLeft) { left = aLeft; }; void setRight(Node* aRight) { right = aRight; }; int Key() { return key; }; Node* Left() { return left; }; Node* Right() { return right; }; }; // Tree class class Tree { Node* root; public: Tree(); ~Tree(); Node* Root() { return root; }; void addNode(int key); void inOrder(Node* n); void preOrder(Node* n); void postOrder(Node* n); private: void addNode(int key, Node* leaf); void freeNode(Node* leaf); }; // Constructor Tree::Tree() { root = NULL; } // Destructor Tree::~Tree() { freeNode(root); } // Free the node void Tree::freeNode(Node* leaf) { if ( leaf != NULL ) { freeNode(leaf->Left()); freeNode(leaf->Right()); delete leaf; } } // Add a node void Tree::addNode(int key) { // No elements. Add the root if ( root == NULL ) { cout Left()); cout Left()); postOrder(n->Right()); cout addNode(10); tree->addNode(20); tree->addNode(40); tree->addNode(50); cout
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