2012-08-05 116 views
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); 
} 

}

回答

0

據我瞭解,你想跟蹤主樹(在主要根源之一始發)的。但是您應該知道,某些插入是針對此主樹的子樹(即屬於大/主樹的節點之一)進行的。

你不「觀察」這些節點(子樹)。每次創建新節點時,例如:

leftChild = new BinaryTree(newValue); 

您還應該添加一個觀察者(即Counter實例)。

問題是,通過這種方式,您將爲每棵樹擁有一個Observer,並且它們中的每一個都將跟蹤不同的size,它是該樹的大小實例變量。

您應該在BinaryTree類中使用類變量size,在插入新樹時應更新該類。這樣所有的計數器將跟蹤相同的大小變種。

+0

我覺得我很困惑......我給兩個Binary Tree構造函數添加了一個大小增量,但是在達到3之後我仍然失去了我的計數器......我誤解了你的建議?我以爲我的櫃檯已經注意到尺碼的變化了? – 2012-08-05 22:33:47

+0

我說的是實例變量而不是類變量。抱歉。 你可以顯示你添加觀察者到observable的代碼嗎? – Razvan 2012-08-05 22:41:49

+0

這是否意味着我在我的主要位置聲明瞭大小?我做了一些快速搜索的例子,我已經設法混淆了自己...這是我的主要方法增加了一切的地方 – 2012-08-05 22:47:35