我试图让BST采用我自己数据的DataType,然后以正确的顺序吐出。问题是它似乎吐出了我添加的最后一个节点,它没有正确排序。我不知道为什么,我会查看我的代码,并且我正在绘制关于可能导致此问题的空白。任何和所有的帮助将不胜感激。
这是我的主要课程
public class BinarySearchTree {
NodeClass root;
public void addNode(DataType data) {
NodeClass newNode = new NodeClass(data);
if (root == null) {
root = newNode;
} else {
NodeClass helperNode = root;
NodeClass parent;
while (true) {
parent = helperNode;
if (data.studentID < helperNode.data.studentID) {
helperNode = helperNode.leftChild;
if (helperNode == null) {
parent.leftChild = newNode;
return;
}
} else {
helperNode = helperNode.rightChild;
if (helperNode == null) {
parent.rightChild = newNode;
return; // All Done
}
}
}
}
}
public void inOrderTraverseTree(NodeClass helperNode) {
if (helperNode != null) {
inOrderTraverseTree(helperNode.leftChild);
System.out.println(helperNode);
inOrderTraverseTree(helperNode.rightChild);
}
}
public void preorderTraverseTree(NodeClass helperNode) {
if (helperNode != null) {
System.out.println(helperNode);
preorderTraverseTree(helperNode.leftChild);
preorderTraverseTree(helperNode.rightChild);
}
}
public void postOrderTraverseTree(NodeClass helperNode) {
if (helperNode != null) {
postOrderTraverseTree(helperNode.leftChild);
postOrderTraverseTree(helperNode.rightChild);
System.out.println(helperNode);
}
}
public NodeClass findNode(int ID) {
NodeClass helperNode = root;
while (helperNode.data.studentID != ID) {
if (ID < helperNode.data.studentID) {
helperNode = helperNode.leftChild;
} else {
helperNode = helperNode.rightChild;
}
// The node wasn't found
if (helperNode == null)
return null;
}
return helperNode;
}
public static void main(String[] args) {
DataType[] data = new DataType[10];
data[0] = new DataType();
data[1] = new DataType();
data[2] = new DataType();
data[3] = new DataType();
data[4] = new DataType();
data[5] = new DataType();
data[6] = new DataType();
data[7] = new DataType();
data[8] = new DataType();
data[9] = new DataType();
BinarySearchTree theTree = new BinarySearchTree();
data[0].studentID = 77;
data[0].letterID = 'M';
data[0].name = "Homer";
data[0].GPA = 3.5;
data[1].studentID = 87;
data[1].letterID = 'W';
data[1].name = "Bart";
data[1].GPA = 2.0;
data[2].studentID = 69;
data[2].letterID = 'E';
data[2].name = "Lisa";
data[2].GPA = 0.5;
data[3].studentID = 79;
data[3].letterID = 'O';
data[3].name = "Marge";
data[3].GPA = 2.3;
data[4].studentID = 81;
data[4].letterID = 'Q';
data[4].name = "Ned";
data[4].GPA = 1.6;
data[5].studentID = 73;
data[5].letterID = 'I';
data[5].name = "Burns";
data[5].GPA = 1.9;
data[6].studentID = 84;
data[6].letterID = 'T';
data[6].name = "Apu";
data[6].GPA = 2.2;
data[7].studentID = 71;
data[7].letterID = 'G';
data[7].name = "Selma";
data[7].GPA = 2.7;
data[8].studentID = 80;
data[8].letterID = 'P';
data[8].name = "Max";
data[8].GPA = 3.5;
data[9].studentID = 75;
data[9].letterID = 'K';
data[9].name = "Nelson";
data[9].GPA = 3.6;
theTree.addNode((data[0]));
theTree.addNode((data[1]));
theTree.addNode(data[2]);
theTree.addNode((data[3]));
theTree.addNode((data[4]));
theTree.addNode(data[5]);
theTree.addNode((data[6]));
theTree.addNode((data[7]));
theTree.addNode(data[8]);
theTree.addNode((data[9]));
// Different ways to traverse binary trees
//theTree.inOrderTraverseTree(theTree.root);
// theTree.preorderTraverseTree(theTree.root);
// theTree.postOrderTraverseTree(theTree.root);
// Find the node with key 75
// System.out.println("\nStudent with the ID 77");
// System.out.println(theTree.findNode(69));
}
}
这是它应该采用的DataType
public class DataType {
public static int studentID;
public char letterID;
public static String name;
public double GPA;
public DataType ()
{
studentID = studentID;
letterID = letterID;
name = name;
GPA = GPA;
}
public String toString () //otherwise we get refrences
{
return "(studentID | " + studentID + "| letterID | " + letterID + "| name | " + name + "GPA" + GPA + "|)";
}
}
这是我必须创建的NodeClass
public class NodeClass
{
public DataType data; //our data to pass in
public NodeClass leftChild;
public NodeClass rightChild;
public NodeClass(DataType dataClass) //our constructor
{
data = dataClass;
leftChild = null;
rightChild = null;
}
public String toString() {
// return DataType.name + " has the key " + DataType.studentID;
return DataType.name + " has the ID " + DataType.studentID + "\nLeft Child: " + leftChild +
"\nRight Child: " + rightChild + "\n";
}
}
答案 0 :(得分:1)
您的问题是,所有<div class="ui accordion">
<div class="active title">
<i class="dropdown icon"></i>
What is a dog?
</div>
<div class="active content">
<p>A dog is a type of domesticated animal. Known for its loyalty and faithfulness, it can be found as a welcome guest in many households across the world.</p>
</div>
<div class="title">
<i class="dropdown icon"></i>
What kinds of dogs are there?
</div>
<div class="content">
<p>There are many breeds of dogs. Each breed varies in size and temperament. Owners often select a breed of dog that they find to be compatible with their own lifestyle and desires from a companion.</p>
</div>
<div class="title">
<i class="dropdown icon"></i>
How do you acquire a dog?
</div>
<div class="content">
<p>Three common ways for a prospective owner to acquire a dog is from pet shops, private owners, or shelters.</p>
<p>A pet shop may be the most convenient way to buy a dog. Buying a dog from a private owner allows you to assess the pedigree and upbringing of your dog before choosing to take it home. Lastly, finding your dog from a shelter, helps give a good home to a dog who may not find one so readily.</p>
</div>
</div>
个实例共享相同的DataType
和studentID
。删除name
关键字。
更改
static
到
public class DataType {
public static int studentID;
public char letterID;
public static String name;
public double GPA;
}