[英]Binary Search tree and Counter with Observer Pattern
因此,我设计了一个二叉搜索树类。.并且在老师的指导下,该树不是由节点组成,而是由其他二叉树组成的-它的子级是通过插入新的二叉树来创建的。 我们需要创建一个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");
}
}
}
这是一些示例输出...创建新树后,计数器消失了-我想我理解我只是不知道如何解决问题的问题。.我尝试使用Delegated Observer,但感到困惑我甚至更多..任何建议吗?
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);
}
}
据我了解,您正在尝试跟踪主树(起源于主根的树)。 但是您应该知道,某些插入是在此主树的子树上进行的(即,属于大/主树的节点之一)。
您不会“观察”这些节点(子树)。 每次创建一个新节点时,例如:
leftChild = new BinaryTree(newValue);
您还应该为其添加观察者(即Counter实例)。
问题在于,这样一来,您将为每个树有一个观察者,并且每个树都将跟踪不同的size
,即该树的大小实例变量。
您应该在BinaryTree
类中使用类变量size
,并在插入新树时更新该类size
。 这样,所有计数器将跟踪相同大小的var。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.