text cover

Data Abstraction and Problem Solving with C++

Walls and Mirrors

by Frank M. Carrano

Addison Wesley Logo

BinaryTree.h

Go to the documentation of this file.
00001 
00016 #include "TreeException.h"
00017 #include "TreeNode.h" // contains definitions for TreeNode
00018                       // and TreeItemType
00019 
00020 typedef void (*FunctionType)(TreeItemType& anItem);
00021 
00024 class BinaryTree
00025 {
00026 public:
00027 // constructors and destructor:
00028    BinaryTree();
00029    BinaryTree(const TreeItemType& rootItem) throw(TreeException);
00030    BinaryTree(const TreeItemType& rootItem,
00031               BinaryTree& leftTree,
00032               BinaryTree& rightTree) throw(TreeException);
00033    BinaryTree(const BinaryTree& tree) throw(TreeException);
00034    virtual ~BinaryTree();
00035 
00036 // binary tree operations:
00037    virtual bool isEmpty() const;
00038 
00039    virtual TreeItemType getRootData() const
00040       throw(TreeException);
00041    virtual void setRootData(const TreeItemType& newItem)
00042       throw(TreeException);
00043 
00044    virtual void attachLeft(const TreeItemType& newItem)
00045       throw(TreeException);
00046    virtual void attachRight(const TreeItemType& newItem)
00047       throw(TreeException);
00048 
00049    virtual void attachLeftSubtree(BinaryTree& leftTree)
00050       throw(TreeException);
00051    virtual void attachRightSubtree(BinaryTree& rightTree)
00052       throw(TreeException);
00053 
00054    virtual void detachLeftSubtree(BinaryTree& leftTree)
00055       throw(TreeException);
00056    virtual void detachRightSubtree(BinaryTree& rightTree)
00057       throw(TreeException);
00058 
00059    virtual BinaryTree getLeftSubtree() const
00060       throw(TreeException);
00061    virtual BinaryTree getRightSubtree() const
00062       throw(TreeException);
00063 
00064    virtual void preorderTraverse(FunctionType visit);
00065    virtual void inorderTraverse(FunctionType visit);
00066    virtual void postorderTraverse(FunctionType visit);
00067 
00068 // overloaded operator:
00069    virtual BinaryTree& operator=(const BinaryTree& rhs)
00070       throw(TreeException);
00071 
00072 protected:
00073    BinaryTree(TreeNode *nodePtr);  // constructor
00074 
00079    void copyTree(TreeNode *treePtr,
00080                  TreeNode *& newTreePtr) const throw(TreeException);
00081 
00083    void destroyTree(TreeNode *& treePtr);
00084 
00085    // The next two methods retrieve and set the value
00086    // of the private data member root.
00087 
00088    TreeNode *rootPtr() const;
00089    void setRootPtr(TreeNode *newRoot);
00090 
00091    // The next two methods retrieve and set the values
00092    // of the left and right child pointers of a tree node.
00093 
00094    void getChildPtrs(TreeNode *nodePtr,
00095                      TreeNode *& leftChildPtr,
00096                      TreeNode *& rightChildPtr) const;
00097    void setChildPtrs(TreeNode *nodePtr,
00098                      TreeNode *leftChildPtr,
00099                      TreeNode *rightChildPtr);
00100 
00101    void preorder(TreeNode *treePtr, FunctionType visit);
00102    void inorder(TreeNode *treePtr, FunctionType visit);
00103    void postorder(TreeNode *treePtr, FunctionType visit);
00104 
00105 private:
00107    TreeNode *root;
00108 }; // end BinaryTree
00109 // End of header file.

Generated on Sun Aug 27 21:29:16 2006 for AWLogo by  doxygen 1.4.6