所以我设计了一个二叉搜索树类。在老师的指导下,树不是由节点组成,而是由其他二叉树组成——它的孩子是通过插入新的二叉树来创建的。我们需要创建一个 Counter 类,它是树大小变化的观察者,我们需要计算“节点”的数量但我不知道如何在每次插入调用时通知...b/ c 如果我通知创建一棵新树——我的“nodeCount”每次在构造函数中都会被重置……有什么想法吗?
这是我的 BST 课
package model;
import java.util.*;
public class BinaryTree extends Observable{
Strategy strategy;
int value;
BinaryTree leftChild;
BinaryTree rightChild;
int size;
BinaryTree(){
value = -1;
leftChild = null;
rightChild = null;
}
public BinaryTree(int newValue){
this.value = newValue;
}
public int getValue(){ return value;}
public Boolean isEmpty(){
Boolean isEmpty;
if(this == null)
isEmpty = true;
else
isEmpty = false;
return isEmpty;
}
public void insert(int newValue){
if(value == -1){
this.value = newValue;
size = 1;
setChanged();
notifyObservers(new Integer(size));
clearChanged();
}
else if(newValue < this.value){
if(leftChild != null){
leftChild.insert(newValue);
} else {
System.out.println("Inserted " + newValue + " to the left of " + value);
leftChild = new BinaryTree(newValue);
size = size + 1;
setChanged();
notifyObservers(new Integer(size));
clearChanged();
}
} else if (newValue >= this.value){
if(rightChild != null){
rightChild.insert(newValue);
} else {
System.out.println("Inserted " + newValue + " to the right of " + value);
rightChild = new BinaryTree(newValue);
size = size + 1;
setChanged();
notifyObservers(new Integer(size));
clearChanged();
}
}
}
}
这是我的柜台课
package model;
import java.util.*;
public class Counter implements Observer{
private int size;
public Counter(){
size = 0;
System.out.println("Counter created: Size is " + size);
}
public void update(Observable BinaryTree, Object size){
if(size instanceof Integer){
size = ((Integer)size).intValue();
System.out.println("Counter : Size changed to " + size);
} else {
System.out.println("Counter: Some other change to the tree");
}
}
}
这是一些示例输出......一旦创建了新树,计数器就会消失 - 我想我理解这个问题我只是不知道如何解决它......我尝试使用委托观察者,但这只是混淆我更……有什么建议吗?
Please enter as many integers as you'd like, hit 'Q' when you are finished.
2
Counter : Size changed to 1
4
Inserted 4 to the right of 2
Counter : Size changed to 2
3
Inserted 3 to the left of 4
5
Inserted 5 to the right of 4
4
Inserted 4 to the left of 5
5
Inserted 5 to the right of 5
这是主要方法
package model;
import java.io.*;
import java.util.*;
public class menu {
public static void main(String[] args){
int integerInput;
int inputOption;
BinaryTree myIntTree;
myIntTree = new BinaryTree();
Counter sizeObs = new Counter();
myIntTree.addObserver(sizeObs);
Scanner userInput = new Scanner(System.in);
do{
System.out.println("=======================================");
System.out.println(" Binary Search Tree Traversal! ");
System.out.println("=======================================");
System.out.println("Options: ");
System.out.println(" 1. Create a new binary search tree ");
System.out.println(" 2. Quit ");
System.out.println("=======================================");
inputOption = KeyIn.inInt("Please select an option from the menu: ");
switch(inputOption){
case 1:
System.out.println("You've selected to create a new binary tree." + "\n");
Scanner scan = new Scanner(System.in);
//String again;
String tempInput;
Boolean repeat = true;
try{
System.out.println("Please enter as many integers as you'd like, hit 'Q' when you are finished." + "\n");
do{
tempInput = scan.next();
if(!tempInput.equals("Q") && !tempInput.equals("q")){
integerInput = Integer.parseInt(tempInput);
myIntTree.insert(integerInput);
repeat = true;
}
else
repeat = false;
}while(repeat);
}catch(InputMismatchException e){}
break;
case 2:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid selection.");
break;
}
}while(inputOption != 2);
}
}