簡體   English   中英

二進制搜索樹和帶有觀察者模式的計數器

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM