This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Using Obersver Pattern to count nodes in binary search tree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using Obersver Pattern to count nodes in binary search tree" Watch "Using Obersver Pattern to count nodes in binary search tree" New topic
Author

Using Obersver Pattern to count nodes in binary search tree

claudia alvarado
Greenhorn

Joined: Aug 05, 2012
Posts: 1
So I have designed a binary search tree class..and on instruction of my teacher, rather than being composed of nodes, the tree is made up of other Binary Trees--it's children are created by inserting new binary trees. We are needing to create a Counter class that is an observer of the size change of the tree and we need to count the number of 'nodes' But I can't figure out how to notify on just every insert call...b/c if I notify on the creation of a new tree--my 'nodeCount' gets reset everytime in the the constructor...any ideas? Thanks!!

Here is my BST class



and here is my counter class



and here is my output..I lose the counter after 3...

Please enter as many integers as you'd like, hit 'Q' when you are finished.

43
Counter : Size changed to 1
4
Inserted 4 to the left of 43
Counter : Size changed to 2
254
Inserted 254 to the right of 43
Counter : Size changed to 3
3
Inserted 3 to the left of 4
5
Inserted 5 to the right of 4
6
Inserted 6 to the right of 5
78
Inserted 78 to the left of 254
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Your observer is only listening for changes to the root node. It isn't attached to the child nodes, so if these change (left.insert, right.insert) it isn't called. You would need to attach all observers to the child nodes if you create them.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Obersver Pattern to count nodes in binary search tree
 
Similar Threads
lists inside a list :(((
Binary trees problem
cannot make static reference to non-static Integer
Path of TreeSelectionEvent
Tournament Tree help