Question
How would I implement the preOrder(), given the following class? public class IntTree { private class Node { private int data; private Node firstChild; private
How would I implement the preOrder(), given the following class?
public class IntTree {
private class Node {
private int data;
private Node firstChild;
private Node sibling;
private Node parent;
private Node (int d, Node f, Node s, Node p) {
data = d;
firstChild = f;
sibling = s;
parent = p;
}
}
private Node root;
public IntTree(int d) {
//create a one node tree
root = addNode(root, d);
}
private Node addNode(Node currNode, int value) {
//Add a Node to the tree
if(currNode == null) {
return new Node(value, null, null, null);
}
if(value < currNode.data) {
currNode.firstChild = addNode(currNode.firstChild, value);
}
else if(value > currNode.data) {
currNode.sibling = addNode(currNode.sibling, value);
}
return currNode;
}
//Implement PreOrder Method
public String preorder() { //return a string of the ints in the tree in preorder //separate the ints with commas //the implementation must be recursive
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered 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