Question
I neeeed hellp to complete this assignment I supposed to write a code that will return a inorder recursive and postorder recursive node by createing
I neeeed hellp to complete this assignment I supposed to write a code that will return a inorder recursive and postorder recursive node by createing the binary search tree for the treetester class can some one please help me thank you
A postorder traversal of a binary tree uses the following recursive pattern to visit all the nodes in a tree n:
Postorder(n)
1. Visit the left subtree of n
2. Visit the right subtree of n
3. Visit the root of n
Similarly, an inorder traversal of a binary tree uses the following recursive pattern to visit all the nodes in a tree n:
Inorder(n)
1. Visit the left subtree of n
2. Visit the root of n
3. Visit the right subtree of n
In this lab, you will complete the code in the Node class for
1. ThepostorderPrintNodemethod that implements a postorder traversal of nodes, and
2. The postorderPrintNode method so that each node in the tree is visited in postorder.
After providing the code for these methods, use the TreeTester main method to create a binary search tree and visit the nodes in postorder and inorder, printing the objects referenced by each node. Each process starts when the respectivepostorderPrint/inorderPrint method in BinarySearchTree is first invoked. Notice that these methods invokes recursive calls to their respectivepostorderPrintNode/postorderPrintNodemethods in the Node class:
public void postorderPrint()
{
root.preorderPrintNode(root);
}
public void inorderPrint()
{
root.inorderPrintNode(root);
}
The code for the BinarySeachTree and TreeTester classes is as follows:
public class BinarySearchTree
{
private Node root;
/**
Constructs an empty tree.
*/
public BinarySearchTree()
{
root = null;
}
/**
Inserts a new node into the tree.
@param obj the object to insert
*/
public void add(Comparable obj)
{
Node newNode = new Node();
newNode.data = obj;
newNode.left = null;
newNode.right = null;
if (root == null) { root = newNode; }
else { root.addNode(newNode); }
}
public void postorderPrint()
{
root.postorderPrintNode(root);
}
public void inorderPrint()
{
root.inorderPrintNode(root);
}
/**
A node of a tree stores a data item and references
to the left and right child nodes.
*/
class Node
{
public Comparable data;
public Node left;
public Node right;
/**
Inserts a new node as a descendant of this node.
@param newNode the node to insert
*/
public void addNode(Node newNode)
{
int comp = newNode.data.compareTo(data);
if (comp < 0)
{
if (left == null) { left = newNode; }
else { left.addNode(newNode); }
}
else if (comp > 0)
{
if (right == null) { right = newNode; }
else { right.addNode(newNode); }
}
}
public void postorderPrintNode(Node n)
{
// your code goes here
}
public void inorderPrintNode(Node n)
{
// your code goes here
}
}
}
------------------------------------------------
/**
This program tests the binary search tree class.
*/
public class TreeTester
{
public static void main(String[] args)
{
BinarySearchTree tree = new BinarySearchTree();
tree.add("D");
tree.add("B");
tree.add("A");
tree.add("C");
tree.add("F");
tree.add("E");
tree.add("I");
tree.add("G");
tree.add("H");
tree.add("J");
System.out.println("Post Order");
tree.postorderPrint();
System.out.println("In Order");
tree.inorderPrint();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started