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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!