二进制搜索树和计数器与观察者模式

时间:2012-08-05 22:03:15

标签: java design-patterns binary-search-tree

所以我设计了一个二叉搜索树类...并且在我老师的指导下,而不是由节点组成,树由其他二叉树组成 - 它的子节点是通过插入新的二叉树来创建的。我们需要创建一个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);
}

}

1 个答案:

答案 0 :(得分:0)

据我了解,您正在尝试跟踪主树(源自主根的树)。但是你应该知道一些插入是针对这个主树的子树(即属于大/主树的节点之一)。

您不“观察”这些节点(子树)。每次创建新节点时,例如:

 leftChild = new BinaryTree(newValue);

你还应该添加一个观察者(即Counter实例)。

问题在于,通过这种方式,您将为每个树创建一个Observer,并且每个树都将跟踪不同的size,这是该树的大小实例变量。

您应该在size类中使用类变量BinaryTree,在插入新树时应该更新它。这样,所有计数器都将跟踪相同大小的var。