In JAVA I am using Data Structure and algoritms in Java Second Edition Program M
ID: 3725506 • Letter: I
Question
In JAVA
I am using Data Structure and algoritms in Java Second Edition
Program Must Compile and run!
Write a program that does an inorder traverse of a 2-3-4 tree. It should display
all the items in order. In text please
MUST COMPILE AND RUN ,PLEASE DISPLAY OUTPUT With Comments.
Please include main method below with program
public static void main(String[] args) throws IOException
{
long value;
Tree234 theTree = new Tree234();
theTree.insert(90);
theTree.insert(80);
theTree.insert(70);
theTree.insert(60);
theTree.insert(50);
theTree.insert(40);
theTree.insert(30);
theTree.insert(20);
theTree.insert(10);
theTree.insert(5);
theTree.insert(15);
theTree.insert(25);
theTree.insert(35);
theTree.insert(45);
theTree.insert(55);
theTree.insert(65);
theTree.insert(75);
theTree.insert(85);
theTree.insert(95);
theTree.insert(1);
theTree.insert(2);
theTree.insert(3);
theTree.insert(4);
theTree.insert(6);
theTree.insert(7);
theTree.insert(8);
theTree.insert(9);
while(true)
{
System.out.print("Enter first letter of ");
System.out.print("show, insert, find or traverse: ");
char choice = getChar();
switch(choice)
{
case 's':
theTree.displayTree();
break;
case 'i':
System.out.print("Enter value to insert: ");
value = getInt();
theTree.insert(value);
break;
case 'f':
System.out.print("Enter value to find: ");
value = getInt();
int found = theTree.find(value);
if(found != -1)
System.out.println("Found "+value);
else
System.out.println("Could not find "+value);
break;
case 't':
theTree.traverse();
break;
default:
System.out.print("Invalid entry ");
} // end switch
} // end while
} // end main()
Explanation / Answer
Utilizing Stack is the undeniable method to cross tree without recursion. The following is a calculation for navigating paired tree utilizing stack. See this for step astute advance execution of the calculation.
1) Create an unfilled stack S.
2) Initialize current hub as root
3) Push the present hub to S and set current = current->left until the point when current is NULL
4) If current is NULL and stack isn't vacant at that point
a) Pop the best thing from stack.
b) Print the popped thing, set current = popped_item->right
c) Go to stage 3.
5) If current is NULL and stack is unfilled then we are finished.
Give us a chance to consider the underneath tree for instance
1
/
2 3
/
4 5
Stage 1 Creates a vacant stack: S = NULL
Stage 2 sets present as address of root: current - > 1
Stage 3 Pushes the present hub and set current = current->left until the point that present is NULL
current - > 1
push 1: Stack S - > 1
current - > 2
push 2: Stack S - > 2, 1
current - > 4
push 4: Stack S - > 4, 2, 1
current = NULL
Stage 4 flies from S
a) Pop 4: Stack S - > 2, 1
b) print "4"
c) current = NULL/*right of 4 */and go to stage 3
Since current is NULL stage 3 doesn't do anything.
Stage 4 pops once more.
a) Pop 2: Stack S - > 1
b) print "2"
c) current - > 5/*right of 2 */and go to stage 3
Stage 3 pushes 5 to stack and makes current NULL
Stack S - > 5, 1
current = NULL
Stage 4 flies from S
a) Pop 5: Stack S - > 1
b) print "5"
c) current = NULL/*right of 5 */and go to stage 3
Since current is NULL stage 3 doesn't do anything
Stage 4 pops once more.
a) Pop 1: Stack S - > NULL
b) print "1"
c) current - > 3/*right of 5 */
Stage 3 pushes 3 to stack and makes current NULL
Stack S - > 3
current = NULL
Stage 4 flies from S
a) Pop 3: Stack S - > NULL
b) print "3"
c) current = NULL/*right of 3 */
Traversal is done now as stack S is unfilled and current is NULL.
Java Program
import java.util.Stack;
class Node {
int data;
Node left, right;
public Node(int item) {
data = item;
left = right = null;
}
}
class BinaryTree {
Node root;
void inorder() {
if (root == null) {
return;
}
Stack<Node> stack = new Stack<Node>();
Node node = root;
while (node != null) {
stack.push(node);
node = node.left;
}
while (stack.size() > 0) {
node = stack.pop();
System.out.print(node.data + " ");
if (node.right != null) {
node = node.right;
while (node != null) {
stack.push(node);
node = node.left;
}
}
}
}
public static void main(String args[]) {
/* creating a binary tree and entering
the nodes */
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.inorder();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.