Reputation: 87
I was asked to implement an AVL tree , and I did it- now it works for all stress-tests I could think about. now I see we were advised to implement it, s.t it inherits from binary search tree that inherits from a binary tree. I want to it right- I'll be glad to get an advice on which invariants (fields that have to appear) should appear in each of them ( the binary tree, the search and the AVL). thank you! here is my implementation for it:
Upvotes: 0
Views: 1717
Reputation: 25895
I'll give you some tips. When coding object oriented code, I find it useful to design top down - that is, implement the parent class, and then inherit and decide what needs to be added/overriden.
Examples:
binary tree:
All these are general properties of a binary tree. You could come up with more (printing for debug uses etc...). We inherit all these and start with:
binary search tree
Finally, you inherit this to make an AVL tree. Try and consider what can be inherited as is, what needs to be completely overridden, and what needs some work before calling a base class. You can google/wikipedia for common tree operations (namely I didn't mention delete, for example). Good luck.
EDIT
AS davmac mentions, the node of an AVL changes - it needs an additional field! We had two children we inherited, and the data field, but now we also need a balance factor. This is an example of when to add a field.
Upvotes: 2