package javaapplication43; public class TestBST { public static void main(String[] args) { BST T = new BST(); T.insert(5

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

package javaapplication43; public class TestBST { public static void main(String[] args) { BST T = new BST(); T.insert(5

Post by answerhappygod »

package javaapplication43;
public class TestBST {
public static void main(String[] args) {
BST T = new BST();
T.insert(5);
T.insert(3);
T.insert(9);
T.insert(1);
T.insert(4);
T.insert(6);
System.out.println("The root of Bi-Tree is: " + (T.root()));
System.out.println("In-order traversal sequence :");
T.inOrder();
System.out.println("Pre-order traversal sequence :");
T.preOrder();
System.out.println("Post-order traversal sequence :");
T.postOrder();
System.out.println("Level-order traversal sequence :");
T.levelOrder();
System.out.println("In-order traversal sequence (after mirroring)
:");
T.mirror();
T.inOrder();
}
}
public class BST {
// Root node pointer. Will be null for an empty tree.
private Node root;
/*
* --Node-- The binary tree is built using this nested node class.
Each node
* stores one data element, and has left and right sub-tree pointer
which may be
* null. The node is a "dumb" nested class -- we just use it for
storage; it
* does not have any methods.
*/
public static class Node {
Node left;
Node right;
int data;
Node(int newData) {
left = null;
right = null;
data = newData;
}
}
/**
* Creates an empty binary tree -- a null root pointer.
*/
public void BST() {
root = null;
}
/**
* Inserts the given data into the binary tree. Uses a recursive
helper.
*/
public void insert(int data) {
root = insert(root, data);
}
/**
* Recursive insert -- given a node pointer, recur down and insert
the given
* data into the tree. Returns the new node pointer (the standard
way to
* communicate a changed pointer back to the caller).
*/
private Node insert(Node node, int data) {
if (node == null) {
node = new Node(data);
} else {
if (data <= node.data) {
node.left = insert(node.left, data);
} else {
node.right = insert(node.right, data);
}
}
return (node); // in any case, return the new pointer to the
caller
}
/**
* Returns the number of nodes in the tree. Uses a recursive helper
that recurs
* down the tree and counts the nodes.
*/
public int size() {
return (size(root));
}
private int size(Node node) {
return 0;
// Complete the code here
}
/**
* Returns true if the given target is in the binary tree. Uses a
recursive
* helper.
*/
public boolean lookup(int data) {
return (lookup(root, data));
}
/**
* Recursive lookup -- given a node, recur down searching for the
given data.
*/
private boolean lookup(Node node, int data) {
if (node == null) {
return (false);
}
if (data == node.data) {
return (true);
} else if (data < node.data) {
return (lookup(node.left, data));
} else {
return (lookup(node.right, data));
}
}
/**
* Prints the node values in the "inorder" order. Uses a recursive
helper to do
* the traversal.
*/
public void inOrder() {
inorderTree(root);
System.out.println();
}
private void inorderTree(Node node) {
if (node == null) {
return;
}
// left, node itself, right
inorderTree(node.left);
System.out.print(node.data + " ");
inorderTree(node.right);
}
/**
* Prints the node values in the "preOrder" order. Uses a recursive
helper to do
* the traversal.
*/
public void preOrder() {
preOrder(root);
System.out.println();
}
public void preOrder(Node node) {
// Complete the code here
}
/**
* Prints the node values in the "postOrder" order. Uses a recursive
helper to
* do the traversal.
*/
public void postOrder() {
postOrder(root);
System.out.println();
}
public void postOrder(Node node) {
// Complete the code here
}
/**
* Prints the node values in the "levelOrder" order. Uses a helper
to do the
* traversal.
CS211 34
*/
public void levelOrder() {
levelOrder(root);
System.out.println();
}
public void levelOrder(Node node) {
if (node != null) {
Queue<Node> q = new ArrayDeque<Node>();
q.add(node);
while (q.size() != 0) {
Node currentNode = q.remove();
System.out.print(currentNode.data + " ");
if (currentNode.left != null) {
q.add(currentNode.left);
}
if (currentNode.right != null) {
q.add(currentNode.right);
}
}
}
}
/**
* Changes the tree into its mirror image. Uses a recursive helper
that recurs
* over the tree, swapping the left/right pointers.
*/
public void mirror() {
// write the code here
}
private Node mirror(Node node) {
// write the code here
}
public int root() {
// TODO Auto-generated method stub
return root.data;
}
}
Write the proper java code to implement the
unfinished methods in the previous program.
please write the answer in java
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply