0

それで、私は二分探索木のクラスを設計しました..そして先生の指示により、木はノードで構成されているのではなく、他の二分木で構成されています-その子は新しい二分木を挿入することによって作成されます。ツリーのサイズ変更のオブザーバーである 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);
}

}

4

1 に答える 1

0

私が理解している限り、あなたはメイン ツリー (メイン ルートに由来するもの) を追跡しようとしています。しかし、いくつかの挿入はこのメイン ツリーのサブツリー (つまり、ビッグ/メイン ツリーに属するノードの 1 つ) に対して行われることに注意してください。

これらのノード (サブツリー) を「観察」しません。新しいノードを作成するたびに、例えば:

 leftChild = new BinaryTree(newValue);

オブザーバー (つまり、カウンター インスタンス) も追加する必要があります。

問題は、この方法では、ツリーごとにオブザーバーがあり、それぞれがsizeそのツリーのサイズ インスタンス変数である異なるものを追跡することです。

新しいツリーを挿入するときに更新するクラスでsizeクラス変数を使用する必要があります。BinaryTreeこのようにして、すべてのカウンターが同じサイズの変数を追跡します。

于 2012-08-05T22:11:49.543 に答える