⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 binarytree.cpp

📁 Data Abstraction & Problem Solving with C++源码
💻 CPP
字号:
// ********************************************************// Implementation file BinaryTree.cpp for the ADT binary // tree.// ********************************************************#include "BinaryTree.h"      // header file#include <cstddef>  // definition of NULL#include <cassert>  // for assert()BinaryTree::BinaryTree() : root(NULL){}  // end default constructorBinaryTree::BinaryTree(const TreeItemType& rootItem){   root = new TreeNode(rootItem, NULL, NULL);}  // end constructorBinaryTree::BinaryTree(const TreeItemType& rootItem,                        BinaryTree& leftTree,                        BinaryTree& rightTree){   root = new TreeNode(rootItem, NULL, NULL);   attachLeftSubtree(leftTree);   attachRightSubtree(rightTree);}  // end constructorBinaryTree::BinaryTree(const BinaryTree& tree){   copyTree(tree.root, root);}  // end copy constructorBinaryTree::BinaryTree(TreeNode *nodePtr):    root(nodePtr){}  // end protected constructorBinaryTree::~BinaryTree(){   destroyTree(root);}  // end destructorbool BinaryTree::isEmpty() const{   return (root == NULL);}  // end isEmptyTreeItemType BinaryTree::getRootData() const                 throw(TreeException){   if (isEmpty())       throw TreeException("TreeException: Empty tree");   return root->item;}  // end getRootDatavoid BinaryTree::setRootData(const TreeItemType& newItem){   if (!isEmpty())      root->item = newItem;   else   {      root = new TreeNode(newItem, NULL, NULL);    }  // end if}  // end setRootDatavoid BinaryTree::attachLeft(const TreeItemType& newItem)                 throw(TreeException){   if (isEmpty())      throw TreeException("TreeException: Empty tree");   else if (root->leftChildPtr != NULL)      throw TreeException("TreeException: Cannot overwrite left subtree");   else  // Assertion: nonempty tree; no left child   {      root->leftChildPtr = new TreeNode(newItem, NULL, NULL);   }  // end if}  // end attachLeftvoid BinaryTree::attachRight(const TreeItemType& newItem)                 throw(TreeException){   if (isEmpty())      throw TreeException("TreeException: Empty tree");   else if (root->rightChildPtr != NULL)      throw TreeException("TreeException: Cannot overwrite right subtree");   else  // Assertion: nonempty tree; no right child   {      root->rightChildPtr = new TreeNode(newItem, NULL, NULL);   }  // end if}  // end attachRightvoid BinaryTree::attachLeftSubtree(BinaryTree& leftTree)                 throw(TreeException){   if (isEmpty())      throw TreeException("TreeException: Empty tree");   else if (root->leftChildPtr != NULL)      throw TreeException("TreeException: Cannot overwrite left subtree");   else  // Assertion: nonempty tree; no left child   {  root->leftChildPtr = leftTree.root;      leftTree.root = NULL;   }}  // end attachLeftSubtreevoid BinaryTree::attachRightSubtree(BinaryTree& rightTree)                 throw(TreeException){   if (isEmpty())      throw TreeException("TreeException: Empty tree");   else if (root->rightChildPtr != NULL)      throw TreeException("TreeException: Cannot overwrite right subtree");   else  // Assertion: nonempty tree; no right child   {  root->rightChildPtr = rightTree.root;      rightTree.root = NULL;   }  // end if}  // end attachRightSubtreevoid BinaryTree::detachLeftSubtree(BinaryTree& leftTree)                  throw(TreeException){   if (isEmpty())      throw TreeException("TreeException: Empty tree");   else   {  leftTree = BinaryTree(root->leftChildPtr);      root->leftChildPtr = NULL;   }  // end if}  // end detachLeftSubtreevoid BinaryTree::detachRightSubtree(BinaryTree& rightTree)                 throw(TreeException){   if (isEmpty())      throw TreeException("TreeException: Empty tree");   else   {  rightTree = BinaryTree(root->rightChildPtr);      root->rightChildPtr = NULL;   }  // end if}  // end detachRightSubtreeBinaryTree BinaryTree::getLeftSubtree() const{   TreeNode *subTreePtr;   if (isEmpty())      return BinaryTree();   else   {  copyTree(root->leftChildPtr, subTreePtr);      return BinaryTree(subTreePtr);   }  // end if}  // end getLeftSubtreeBinaryTree BinaryTree::getRightSubtree() const{   TreeNode *subTreePtr;   if (isEmpty())      return BinaryTree();   else   {  copyTree(root->rightChildPtr, subTreePtr);      return BinaryTree(subTreePtr);   }  // end if}  // end getRightSubtreevoid BinaryTree::preorderTraverse(FunctionType visit){   preorder(root, visit);}  // end preorderTraversevoid BinaryTree::inorderTraverse(FunctionType visit){   inorder(root, visit);}  // end inorderTraversevoid BinaryTree::postorderTraverse(FunctionType visit){   postorder(root, visit);}  // end postorderTraverseBinaryTree& BinaryTree::operator=(const BinaryTree& rhs){   if (this != &rhs)   {  destroyTree(root);  // deallocate left-hand side      copyTree(rhs.root, root);  // copy right-hand side   }  // end if   return *this;}  // end operator=void BinaryTree::copyTree(TreeNode *treePtr,                           TreeNode *& newTreePtr) const{   // preorder traversal   if (treePtr != NULL)   {  // copy node      newTreePtr = new TreeNode(treePtr->item, NULL, NULL);      copyTree(treePtr->leftChildPtr, newTreePtr->leftChildPtr);      copyTree(treePtr->rightChildPtr, newTreePtr->rightChildPtr);   }     else      newTreePtr = NULL;  // copy empty tree}  // end copyTreevoid BinaryTree::destroyTree(TreeNode *& treePtr){   // postorder traversal   if (treePtr != NULL)   {  destroyTree(treePtr->leftChildPtr);      destroyTree(treePtr->rightChildPtr);      delete treePtr;      treePtr = NULL;   }  // end if}  // end destroyTreeTreeNode *BinaryTree::rootPtr() const{   return root;}  // end rootPtrvoid BinaryTree::setRootPtr(TreeNode *newRoot){   root = newRoot;}  // end setRootvoid BinaryTree::getChildPtrs(TreeNode *nodePtr,                               TreeNode *& leftPtr,                               TreeNode *& rightPtr) const{   leftPtr = nodePtr->leftChildPtr;   rightPtr = nodePtr->rightChildPtr;}  // end getChildPtrsvoid BinaryTree::setChildPtrs(TreeNode *nodePtr,                               TreeNode *leftPtr,                               TreeNode *rightPtr){   nodePtr->leftChildPtr = leftPtr;   nodePtr->rightChildPtr = rightPtr;}  // end setChildPtrsvoid BinaryTree::preorder(TreeNode *treePtr,                           FunctionType visit){   if (treePtr != NULL)   {  visit(treePtr->item);      preorder(treePtr->leftChildPtr, visit);      preorder(treePtr->rightChildPtr, visit);   } // end if}  // end preordervoid BinaryTree::inorder(TreeNode *treePtr,                          FunctionType visit){   if (treePtr != NULL)   {  inorder(treePtr->leftChildPtr, visit);      visit(treePtr->item);      inorder(treePtr->rightChildPtr, visit);   } // end if}  // end inordervoid BinaryTree::postorder(TreeNode *treePtr,                            FunctionType visit){   if (treePtr != NULL)   {  postorder(treePtr->leftChildPtr, visit);      postorder(treePtr->rightChildPtr, visit);      visit(treePtr->item);   } // end if}  // end postorder// End of implementation file.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -