搜索单个类成员的对象的二叉树

时间:2016-10-15 03:23:30

标签: c++ tree

所以我正在开发一个程序,该程序将使用我创建的二叉树模板类来保存一组EmployeeInfo对象,这是我创建的另一个类。 EmployeeInfo类有两个数据成员,一个用于保存员工的ID号,另一个用于保存员工姓名。在主程序中,我创建了5个employeeinfo对象的二叉树,我现在试图弄清楚如何在二叉树中搜索JUST员工ID号,如果有数字,我必须打印这个号码。与屏幕上的数字相关联的名称。这就是我遇到麻烦的地方。我的搜索功能返回一个bool,因此我不确定如何找到它,以显示与ID号相关联的名称(如果找到)。

这是我的二叉树模板类:

// Specification file for the BinaryTree Class
#ifndef BINARYTREE_H
#define BINARYTREE_H
#include <iostream>
using namespace std;

// BinaryTree template
template <class T>
class BinaryTree
{
private:
    struct TreeNode
    {
        T value;
        TreeNode *left;
        TreeNode *right;
    };

    TreeNode *root;

    // Private 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 findHeight(TreeNode *root);
    int count1(TreeNode *);
    int countLeft(TreeNode *);

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

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

    // Binary Tree Operations
    void insertNode(T);
    bool searchNode(T);
    void remove(T);
    int displayCount()
    {
        return count1(root);
    }
    int displayLeftCount()
    {
        return countLeft(root);
    }
    int displayHeight()
    {
        return findHeight(root);
    }


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

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

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

};

template <class T>
void BinaryTree<T>::insert(TreeNode *&root, TreeNode *&newNode)
{
    if (root == nullptr)
        root = newNode;
    else if (newNode->value < root->value)
        insert(root->left, newNode);
    else
        insert(root->right, newNode);
}

template <class T>
void BinaryTree<T>::insertNode(T item)
{
    TreeNode *newNode = nullptr;

    newNode = new TreeNode;
    newNode->value = item;
    newNode->left = newNode->right = nullptr;

    insert(root, newNode);
}

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;
    }
}

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;
}

template <class T>
void BinaryTree<T>::remove(T item)
{
    deleteNode(item, root);
}

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
        makeDeleteion(nodePtr);
}

template <class T>
void BinaryTree<T>::makeDeletion(TreeNode *&nodePtr)
{
    TreeNode *tempNodePtr = nullptr;

    if (nodePtr == nullptr)
        cout << "Cannot delete empty node." << endl;
    else if (nodePtr->right == nullptr)
    {
        tempNodePtr = nodePtr;
        nodePtr = nodePtr->left;
        delete tempNodePtr;
    }
    else if (nodePtr->left == nullptr)
    {
        tempNodePtr = nodePtr;
        nodePtr = nodePtr->right;
        delete tempNodePtr;
    }
    else
    {
        tempNodePtr = nodePtr->right;
        while (tempNodePtr->left)
            tempNodePtr = tempNodePtr->left;
        tempNodePtr->left = nodePtr->left;
        tempNodePtr = nodePtr;
        nodePtr = nodePtr->right;
        delete tempNodePtr;
    }
}

template <class T>
void BinaryTree<T>::displayInOrder(TreeNode *nodePtr) const
{
    if (nodePtr)
    {
        displayInOrder(nodePtr->left);
        cout << nodePtr->value << endl;
        displayInOrder(nodePtr->right);
    }
}

template <class T>
void BinaryTree<T>::displayPreOrder(TreeNode *nodePtr) const
{
    if (nodePtr)
    {
        cout << nodePtr->value << endl;
        displayPreOrder(nodePtr->left);
        displayPreOrder(nodePtr->right);
    }
}

template <class T>
void BinaryTree<T>::displayPostOrder(TreeNode *nodePtr) const
{
    if (nodePtr)
    {
        displayPostOrder(nodePtr->left);
        displayPostOrder(nodePtr->right);
        cout << nodePtr->value << endl;
    }
}

template <class T>
int BinaryTree<T>::count1(TreeNode *root)
{
    int count = 1;
    if (root->left != nullptr)
    {
        count += count1(root->left);
    }
    if (root->right != nullptr)
    {
        count += count1(root->right);
    }
    return count;
}

template <class T>
int BinaryTree<T>::countLeft(TreeNode *root)
{
    int count = 1;
    if (root->left != nullptr)
        count += countLeft(root->left);
    return count;
}

template <class T>
int BinaryTree<T>::findHeight(TreeNode *nodePtr)
{
    if (nodePtr == NULL)
    {
        return 0;
    }

    int left = findHeight(nodePtr->left);
    int right = findHeight(nodePtr->right);

    if (left > right)
        return 1 + left;
    else
        return 1 + right;

}

#endif

这是我的EmployeeInfo类标题和cpp:

/* Specification file for the EmployeeInfo class which will hold two private data members, one which is an integer called empID and another which will
be a string called empName.*/
#include "BinaryTree.h"
#include <iostream>
#include <string>
#ifndef EMPLOYEEINFO_H
#define EMPLOYEEINFO_H

class EmployeeInfo
{
private:
    int empID;
    string empName;
public:
    // Default Constructor
    EmployeeInfo();

    // Constructor
    EmployeeInfo(int, string);

    // Mutator functions
    void setEmpID(int);
    void setEmpName(string);

    // Accessor functions
    int getEmpID() const;
    string getEmpName() const;

    // Overloaded operators
    bool operator < (const EmployeeInfo &right);
    bool operator > (const EmployeeInfo &right);
    bool operator == (const EmployeeInfo &right);
};
#endif

// Declaration file for the EmployeeInfo class
#include "BinaryTree.h"
#include "EmployeeInfo.h"
#include <iostream>
#include <string>

// Default Constructor
EmployeeInfo::EmployeeInfo()
{
    empID = 0;
    empName = "";
}

// Constructor
EmployeeInfo::EmployeeInfo(int i, string n)
{
    empID = i;
    empName = n;
}

// Mutators
void EmployeeInfo::setEmpID(int i)
{
    empID = i;
}

void EmployeeInfo::setEmpName(string n)
{
    empName = n;
}

// Accessors
int EmployeeInfo::getEmpID() const
{
    return empID;
}

string EmployeeInfo::getEmpName() const
{
    return empName;
}

// Overloaded operators
bool EmployeeInfo::operator > (const EmployeeInfo &right)
{
    bool status;

    if (empID > right.empID)
        status = true;
    else
        status = false;

    return status;
}

bool EmployeeInfo::operator < (const EmployeeInfo &right)
{
    bool status;

    if (empID < right.empID)
        status = true;
    else
        status = false;

    return status;
}

bool EmployeeInfo::operator == (const EmployeeInfo &right)
{
    bool status;

    if (empID == right.empID)
        status = true;
    else
        status = false;

    return status;
}

以下是主要计划:

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

const int NUM_EMPS = 5;

int main()
{
    BinaryTree<EmployeeInfo> staff;
    int ID;
    string name;

    // Fill up the tree
    for (int count = 0; count < NUM_EMPS; count++)
    {
        cout << "Enter the ID number for Employee " << count + 1 << ": ";
        cin >> ID;
        cout << "Name: ";
        cin >> name;
        EmployeeInfo newEmp(ID, name);
        staff.insertNode(newEmp);
    }

    // NEED HELP WITH THIS PART!!!
    do
    {
        int search;
        cout << "Enter the ID number you'd like to search for: ";
        cin >> search;


    system("pause");
    return 0;
}

我能够用对象填满树,但是当我搜索树时,我迷失了!任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

使用二叉树类显示信息。

在二叉树类中编辑searchNode函数

template <class T>
bool BinaryTree<T>::searchNode(T item) {
    TreeNode *nodePtr = root;

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

然后创建一个EmployeeInfo对象,为其分配要搜索的ID,并搜索您的树:

int search;
cout << "Enter the ID number you'd like to search for: ";
cin >> search;

EmployeeInfo tempEmp;
tempEmp.setEmpID(search);

if (!staff.searchNode(tempEmp))
        cout << "Employee ID not found.\n";

// else if staff.searchNode(tempEmp) == true, employee info will 
// be displayed from Binary Tree searchNode method