home *** CD-ROM | disk | FTP | other *** search
- #ifndef __RWBINTREE_H__
- #define __RWBINTREE_H__
- pragma push_align_members(64);
-
- /*
- * Binary tree of pointers to RWCollectable objects
- *
- * $Header: E:/vcs/rw/bintree.h_v 1.6 01 Apr 1992 16:38:30 KEFFER $
- *
- ****************************************************************************
- *
- * Rogue Wave
- * P.O. Box 2328
- * Corvallis, OR 97339
- * Voice: (503) 754-3010 FAX: (503) 757-6650
- *
- * Copyright (C) 1989, 1990, 1991. This software is subject to copyright
- * protection under the laws of the United States and other countries.
- *
- ***************************************************************************
- *
- * $Log: E:/vcs/rw/bintree.h_v $
- *
- * Rev 1.6 01 Apr 1992 16:38:30 KEFFER
- * Removed private (and undefined) declaration for constructFrom().
- *
- * Rev 1.5 04 Mar 1992 09:02:38 KEFFER
- * nil changed to rwnil
- *
- * Rev 1.4 18 Feb 1992 09:54:02 KEFFER
- *
- * Rev 1.3 29 Oct 1991 13:56:14 keffer
- *
- * Rev 1.2 28 Oct 1991 09:08:06 keffer
- * Changed inclusions to <rw/xxx.h>
- *
- */
-
- #include "rw/colclass.h"
- #include "rw/iterator.h"
- #include "rw/mempool.h"
- STARTWRAP
- #include <stddef.h>
- ENDWRAP
-
- // Node in binary tree:
- class RWExport RWTreeNode : public RWMemoryPool {
- private:
- friend class RWExport RWBinaryTree;
- friend class RWExport RWBinaryTreeIterator;
- RWTreeNode* right; // Pointer to right node.
- RWTreeNode* left; // Pointer to left node.
- RWCollectable* e; // Pointer to RWCollectable object.
- private:
- // Private constructor:
- RWTreeNode(RWCollectable* a, RWTreeNode* p=rwnil, RWTreeNode* n=rwnil)
- { e = a; left = p; right = n; }
- };
-
- #include "rw/gqueue.h"
- declare(GQueue, RWCollectable)
-
- class RWExport RWBinaryTree : public RWCollection {
- friend class RWExport RWBinaryTreeIterator;
- RWTreeNode* root; // root = top-level item in tree
- protected:
- void applyChildren(const RWTreeNode*, RWapplyCollectable, void*);
- void balanceUnique();
- RWTreeNode* balanceChildren(unsigned, GQueue(RWCollectable)&);
- void countChildren(const RWTreeNode*, unsigned&) const;
- void deleteChildren(RWTreeNode*);
- RWCollectable* deleteNode(RWTreeNode* victim, RWTreeNode* parent);
- void insertChildrenOf(const RWTreeNode*);
- public:
- RWBinaryTree();
- RWBinaryTree(const RWBinaryTree&);
- ~RWBinaryTree();
-
- void operator=(const RWBinaryTree&);
- RWBoolean operator<=(const RWBinaryTree& bt) const; // Subset of bt
- RWBoolean operator==(const RWBinaryTree& bt) const;
-
- // Special member function to balance a binary tree:
- void balance();
-
- /******** Standard Member Functions for Collection Classes ********/
- virtual void apply(RWapplyCollectable, void*);
- virtual void clear();
- //virtual void clearAndDestroy();
- //virtual RWBoolean contains(const RWCollectable*) const;
- virtual unsigned entries() const; // Total entries
- virtual RWCollectable* find(const RWCollectable*) const; // First occurrence
- virtual RWCollectable* insert(RWCollectable*);
- virtual ClassID isA() const {return __RWBINARYTREE;}
- virtual RWBoolean isEmpty() const {return root==rwnil;}
- virtual RWBoolean isEqual(const RWCollectable*) const;
- virtual RWCollectable* newSpecies() const;
- virtual unsigned occurrencesOf(const RWCollectable*) const;
- virtual RWCollectable* remove(const RWCollectable*); // Remove first occurrence
- //virtual void removeAndDestroy(const RWCollectable*);
- virtual void restoreGuts(RWvistream&);
- virtual void restoreGuts(RWFile&);
-
- #ifdef RDEBUG
- void printChildren(const RWTreeNode*, ostream&, unsigned&, char) const;
- friend ostream& operator<<(ostream&, const RWBinaryTree& bt);
- #endif
- };
-
- #include "rw/gstack.h"
- declare(GStack, RWTreeNode)
-
- class RWExport RWBinaryTreeIterator : public RWIterator {
- protected:
- const RWBinaryTree* tree; // Binary tree for this iterator.
- const RWTreeNode* here; // Current node.
- GStack(RWTreeNode) stack;
- private:
- void descendLeft();
- public:
- RWBinaryTreeIterator(const RWBinaryTree&);
-
- /*********** Virtual functions inherited from class RWIterator ***********/
- virtual RWCollectable* findNext(const RWCollectable*); // Find next matching item
- virtual RWCollectable* key() const; // Return current value
- virtual RWCollectable* operator()(); // Advance iterator
- virtual void reset();
- };
-
- pragma pop_align_members();
- #endif /* __RWBINTREE_H__ */
-