查找随机生成的数字插入c ++的二叉树级别

时间:2017-04-29 03:19:47

标签: c++ binary-tree

此代码运行良好且没有错误,但我无法弄清楚如何在不知道每个节点的实际数量的情况下获取二叉树的级别。我目前有一个for循环,我知道这是不正确的,但我不知道如何找出二叉树的级别。我还想知道是否可以找到所有有一个孩子的节点。

#ifndef BINARYTREE_H
#define BINARYTREE_H

class BinaryTree
{
private:
    struct TreeNode
    {
        int value;        
        TreeNode *left;    
        TreeNode *right;   
    }; TreeNode *root;       


    void insert(TreeNode *&, TreeNode *&);
    void destroySubTree(TreeNode *);
    void deleteNode(int, TreeNode *&);
    void makeDeletion(TreeNode *&);
    void displayInOrder(TreeNode *) const;
    void displayPreOrder(TreeNode *) const;
    void displayPostOrder(TreeNode *) const;

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

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

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

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

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

    void displayPostOrder() const
    {
        displayPostOrder(root);
    }
    int countNodes(TreeNode *);
    void count();
    int getLevelTree(TreeNode *, int, int);
    int getLevel(int);
};
#endif

#include <iostream>
#include<cmath>
#include "BinaryTree.h"
using namespace std;

// insert accepts a TreeNode pointer and a pointer to a node. The function inserts the node into 
// the tree pointed to by the TreeNode pointer. This function is called recursively. 
void BinaryTree::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 a new node to hold num as its value, and passes it to the insert function. 
void BinaryTree::insertNode(int num)
{
    TreeNode *newNode;      // Pointer to a new node.

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

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


// destroySubTree is called by the destructor. It deletes all nodes in the tree.   
void BinaryTree::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.  
bool BinaryTree::searchNode(int num)
{
    TreeNode *nodePtr = root;

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


// remove calls deleteNode to delete the node whose value member is the same as num.
void BinaryTree::remove(int num)
{
    deleteNode(num, root);
}


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


// makeDeletion takes a reference to a pointer to the node that is to be deleted. 
// The node is removed and the branches of the tree below the node are reattached. 
void BinaryTree::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;
    }
    // If the node has two children.
    else
    {
        // Move one node the right.
        tempNodePtr = nodePtr->right;
        // Go to the end left node.
        while (tempNodePtr->left)
            tempNodePtr = tempNodePtr->left;
        // Reattach the left subtree.
        tempNodePtr->left = nodePtr->left;
        tempNodePtr = nodePtr;
        // Reattach the right subtree.
        nodePtr = nodePtr->right;
        delete tempNodePtr;
    }
}


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


// The displayPreOrder member function displays the values  
// in the subtree pointed to by nodePtr, via preorder traversal. 
void BinaryTree::displayPreOrder(TreeNode *nodePtr) const
{
    if (nodePtr)
    {
        cout << nodePtr->value << endl;
        displayPreOrder(nodePtr->left);
        displayPreOrder(nodePtr->right);
    }
}


// The displayPostOrder member function displays the values 
// in the subtree pointed to by nodePtr, via postorder traversal.
void BinaryTree::displayPostOrder(TreeNode *nodePtr) const
{
    if (nodePtr)
    {
        displayPostOrder(nodePtr->left);
        displayPostOrder(nodePtr->right);
        cout << nodePtr->value << endl;
    }
}
int BinaryTree::getLevelTree(TreeNode *node, int val, int lev)
{
    if (node == NULL)
    {
        return 0;
    }
    if (node->value == val)
    {
        return lev;
    }
    int downlev = getLevelTree(node->left, val, lev + 1);
    return downlev;
}
int BinaryTree::getLevel(int val)
{
    return getLevelTree(root, val, 1);
}

int BinaryTree::countNodes(TreeNode *root)
{

    if (root == NULL)
    {
        return 0;
    }
    else
    {
        int count = 1;
        count += countNodes(root->left);
        count += countNodes(root->right);
        return count;
    }

}
void BinaryTree::count()
{
    cout<< countNodes(root);
}

#include <iostream>
#include<vector>
#include<algorithm>
#include<cstdlib>
#include<numeric>
#include<ctime>
#include "BinaryTree.h"
using namespace std;
int main()
{
    int randNum;
    vector<int> randData;
    vector<int>::iterator iter;
    size_t size = randData.size();
    srand(time(0));
    BinaryTree tree;
    for (int i = 0; i < 5; i++)
    {
        randNum = rand() % 1000 + 1;
        tree.insertNode(randNum);

    }
    cout << "display : \n";
    tree.displayInOrder();
    cout << endl;
    for(int i=1; i<=5;i++)
    { 
        cout<< "getlevel: "<<tree.getLevel(i);  
    }
    system("pause");
    return 0;
}

0 个答案:

没有答案