2014-02-27 59 views
0

谁能告诉我为什么我的最后一个功能height()导致系统崩溃?我测试了每一个函数,他们都工作,但是当我编写这个函数并从主函数调用它时,会导致程序崩溃。它的构建没有任何错误。BinaryTree模板崩溃

#ifndef BINARYTREE_H 
#define BINARYTREE_H 
#include <iostream> 
using namespace std; 

// This class is a template class that creates a binary 
// tree that can hold values of any data type. It has 
// functions to insert a node, delete a node, display the 
// tree In Order, Pre Order and Post Order, search for a 
// value, count the number of total nodes, left nodes, 
// and a function to determine the height of the tree. 

template <class T> 
class BinaryTree 
{ 
private: 
    struct TreeNode 
    { 
     T value;   // The value in the node 
     TreeNode *left;  // Pointer to left child node 
     TreeNode *right; // Pointer to right child node 
    }; 

    TreeNode *root;   // Pointer to the root node 

    // Private member functions 
    void insert(TreeNode *&, TreeNode *&); 
    void destroySubTree(TreeNode *); 
    void deleteNode(T, TreeNode *&); 
    void makeDeletion(TreeNode *&); 
    void displayInOrder(TreeNode *) const; 
    void displayPreOrder(TreeNode *) const; 
    void displayPostOrder(TreeNode *) const; 
    int counter(TreeNode *); 
    int leafCounter(TreeNode *); 
    int height(TreeNode *); 

public: 
    // Constructor 
    BinaryTree() 
    { root = NULL; } 

    // Destructor 
    ~BinaryTree() 
    { destroySubTree(root); } 

    // Binary tree operations 
    void insertNode(T); 
    bool searchNode(T); 
    void remove(T); 

    void displayPreOrder() const 
    { displayPreOrder(root); } 

    void displayInOrder() const 
    { displayInOrder(root); } 

    void displayPostOrder() const 
    { displayPostOrder(root); } 

    // Node counter 
    int counter() 
    { 
     int n = counter(root); 
     return n; 
    } 

    // Leaf counter 
    int leafCounter() 
    { 
     int leaf = leafCounter(root); 
     return leaf; 
    } 

    // Height of the tree 
    int height() 
    { 
     int h = height(root); 
     return h; 
    } 
}; 

//********************************************************* 
// insert function accepts a TreeNode pointer and a  * 
// pointer to a node. The function inserts the node into * 
// the tree pointer to by the TreeNode pointer. This  * 
// function is call recursively.       * 
//********************************************************* 
template <class T> 
void BinaryTree<T>::insert(TreeNode *&nodePtr, TreeNode *&newNode) 
{ 
    if (nodePtr == NULL) 
     nodePtr = newNode;    // Insert the node 
    else if (newNode->value < nodePtr->value) 
     insert(nodePtr->left, newNode); // Search the left branch 
    else 
     insert(nodePtr->right, newNode);// Search the right branch 
} 

//********************************************************* 
// insertNode creates anew node to hold num as its value * 
// and passes it to the insert function.     * 
//********************************************************* 
template <class T> 
void BinaryTree<T>::insertNode(T item) 
{ 
    TreeNode *newNode;  // Pointer to a new node 

    // Create anew node and store num in it 
    newNode = new TreeNode; 
    newNode->value = item; 
    newNode->left = newNode->right = NULL; 

    // Insert the node 
    insert(root, newNode); 
} 

//********************************************************** 
// destroySubTree is called by the destructor. It deletes * 
// all nodes in the tree.         * 
//********************************************************** 
template <class T> 
void BinaryTree<T>::destroySubTree(TreeNode *nodePtr) 
{ 
    if (nodePtr) 
    { 
     if (nodePtr->left) 
      destroySubTree(nodePtr->left); 
     if (nodePtr->right) 
      destroySubTree(nodePtr->right); 
     delete nodePtr; 
    } 
} 

//********************************************************** 
// searchNode determines if a value is present in the tree.* 
// If so, the function returns true. Otherwise it returns * 
// false. 
//********************************************************** 
template <class T> 
bool BinaryTree<T>::searchNode(T item) 
{ 
    TreeNode *nodePtr = root; 

    while (nodePtr) 
    { 
     if (nodePtr->value == item) 
      return true; 
     else if (item < nodePtr->value) 
      nodePtr = nodePtr->left; 
     else 
      nodePtr = nodePtr->right; 
    } 
    return false; 
} 

//********************************************************* 
// remove calls deleteNode to delete the node whode value * 
// member is the same as num        * 
//********************************************************* 
template <class T> 
void BinaryTree<T>::remove(T item) 
{ 
    deleteNode(item, root); 
} 

//********************************************************* 
// deleteNode deletes the node whose value member is the * 
// same as num           * 
//********************************************************* 
template <class T> 
void BinaryTree<T>::deleteNode(T item, TreeNode *&nodePtr) 
{ 
    if (item < nodePtr->value) 
     deleteNode(item, nodePtr->left); 
    else if (item > nodePtr->value) 
     deleteNode(item, nodePtr->right); 
    else 
     makeDeletion(nodePtr); 
} 

//********************************************************* 
// makeDeletion takes a reference to apointer to the node * 
// that is to be deleted. The node is removed and the  * 
// branches of the tree below the node are reattached  * 
//********************************************************* 
template <class T> 
void BinaryTree<T>::makeDeletion(TreeNode *&nodePtr) 
{ 
    // Define a temporary pointer to use in reattaching 
    // the left subtree 
    TreeNode *tempNodePtr; 

    if (nodePtr == NULL) 
     cout << "Cannot delete empty node.\n"; 
    else if (nodePtr->right == NULL) 
    { 
     tempNodePtr = nodePtr; 
     nodePtr = nodePtr->left; // Reattach the left child 
     delete tempNodePtr; 
    } 
    else if (nodePtr->left == NULL) 
    { 
     tempNodePtr = nodePtr; 
     nodePtr = nodePtr->right; // Reattach the right child 
     delete tempNodePtr; 
    } 

} 
//********************************************************* 
// The displayInOrder function displays the values in the * 
// subtree pointed to by nodePtr, via inorder traversal * 
//********************************************************* 
template <class T> 
void BinaryTree<T>::displayInOrder(TreeNode *nodePtr) const 
{ 
    if (nodePtr) 
    { 
     displayInOrder(nodePtr->left); 
     cout << nodePtr->value << endl; 
     displayInOrder(nodePtr->right); 
    } 
} 
//********************************************************* 
// The displayPreOrder function displays the values in the* 
// subtree pointed to by nodePtr, via Preorder traversal * 
//********************************************************* 
template <class T> 
void BinaryTree<T>::displayPreOrder(TreeNode *nodePtr) const 
{ 
    if (nodePtr) 
    { 
     cout << nodePtr->value << endl; 
     displayInOrder(nodePtr->left); 
     displayInOrder(nodePtr->right); 
    } 
} 
//********************************************************* 
// displayPostOrder function displays the values in the * 
// subtree pointed to by nodePtr, via Postorder traversal * 
//********************************************************* 
template <class T> 
void BinaryTree<T>::displayPostOrder(TreeNode *nodePtr) const 
{ 
    if (nodePtr) 
    { 
     displayInOrder(nodePtr->left); 
     displayInOrder(nodePtr->right); 
     cout << nodePtr->value << endl; 
    } 
} 

//********************************************************* 
// counter counts the number of nodes the tree has  * 
//********************************************************* 
template <class T> 
int BinaryTree<T>::counter(TreeNode *nodePtr) 
{ 
    if (nodePtr == NULL) 
     return 0; 
    else 
     return counter(nodePtr->left) +1+ counter(nodePtr->right); 
} 

//********************************************************* 
// leafCounter counts the number of leaf nodes in the tree* 
//********************************************************* 
template <class T> 
int BinaryTree<T>::leafCounter(TreeNode *nodePtr) 
{ 
    if (nodePtr == NULL) 
     return 0; 
    else if (nodePtr->left == NULL && nodePtr->right == NULL) 
     return 1; 
    else 
     return leafCounter(nodePtr->left) + leafCounter(nodePtr->right); 
} 

//********************************************************* 
// height returns the height of the tree     * 
//********************************************************* 
template <class T> 
int BinaryTree<T>::height(TreeNode *nodePtr) 
{ 

    if(nodePtr = NULL) 
     return -1; 
    if (height(nodePtr->left) <= height(nodePtr->right)) 
     return (height(nodePtr->right) +1); 
    else 
     return (height(nodePtr->left) +1); 

} 
#endif 

主要

// This program demonstrates that the functions of 
// BinaryTree works correctly. 
#include "BinaryTree.h" 
#include <iostream> 
using namespace std; 

int main() 
{ 
    // Create a BinaryTree object 
    BinaryTree<int> tree; 

    // Insert some nodes 
    cout << "Inserting nodes...\n"; 
    tree.insertNode(5); 
    tree.insertNode(10); 
    tree.insertNode(3); 
    tree.insertNode(1); 
    tree.insertNode(13); 

    // Display the nodes InOrder 
    cout << "\nDisplaying the nodes InOrder...\n"; 
    tree.displayInOrder(); 

    // Display the nodes PreOrder 
    cout << "\nDisplaying the nodes PreOrder...\n"; 
    tree.displayPreOrder(); 

    // Display the nodes PostOrder 
    cout << "\nDisplaying the nodes PostOrder...\n"; 
    tree.displayPostOrder(); 

    // Delete a node 
    cout << "\nDeleting node 3...\n"; 
    tree.remove(3); 

    // Display the nodes after deletion 
    cout << "\nHere are the nodes InOrder after deletion:\n"; 
    tree.displayInOrder(); 

    // Search the nodes for the value 10 
    cout << "\nSearching the nodes for the value 10...\n"; 
    if (tree.searchNode(10)) 
     cout << "Value was found.\n"; 
    else 
     cout << "Value was not found.\n"; 

    // Search for the deleted node 3 
    cout << "\nSearching for the deleted node 3...\n"; 
    if (tree.searchNode(3)) 
     cout << "Value was found.\n"; 
    else 
     cout << "Value was not found.\n"; 

    // Count how many nodes are in the tree 
    cout << "\nThere are " << tree.counter() << " nodes" 
     << " in the tree.\n"; 

    // Count how many leafs are in the tree 
    cout << "\nThere are " << tree.leafCounter() 
     << " leaves in the tree.\n"; 

    // Get the height of the tree 
    cout << "\nThe height of the tree is " << tree.height(); 
    cout << endl; 

    return 0; 
} 
+0

请关闭。我得到了我的答案。我叫自己看着这个小小的错误。得到它了! – Lilspree

回答

0
if(nodePtr = NULL) 

应该是:

if(nodePtr == NULL) 

第一个设置nodePtr为NULL,则隐含测试,如果结果不为NULL(这始终是假的)。第二个测试nodePtr是否为NULL。