【逆向】二叉树的实现

#include 
#include 
using namespace std;

class Monster
{
private:
    string name;
    DWORD id;
    DWORD level;

public:
    BOOL operator<(Monster& m);
    BOOL operator>(Monster& m);
    BOOL operator==(Monster& m);
    VOID operator=(Monster& m);
    friend ostream& operator<<(ostream& out, const Monster& m);
    Monster(DWORD id, string name, DWORD level);
    Monster();
};

VOID Monster:: operator=(Monster& m)
{
    this->id = m.id;
    this->level = m.level;
    this->name = m.name;
}

Monster::Monster()
{
    this->id = 0;
    this->level = 0;
}

Monster::Monster(DWORD id, string name, DWORD level)
{
    this->id = id;
    this->level = level;
    this->name = name;
}

ostream& operator<<(ostream& out, const Monster& m)
{
    out << "怪物的name是:" << m.name << endl;
    out << "怪物的id是:" << m.id << endl;
    out << "怪物的level是:" << m.level << endl;
    out << "************************************************************" << endl;
    return out;
}

BOOL Monster::operator<(Monster& m)
{
    if (this->id < m.id)
    {
        return true;
    }
    else
        return false;
}

BOOL Monster::operator>(Monster& m)
{
    if (this->id > m.id)
    {
        return true;
    }
    else
        return false;
}

BOOL Monster::operator==(Monster& m)
{
    if (this->id == m.id)
    {
        return true;
    }
    else
        return false;
}

template 
class TreeNode
{
public:
    T element;
    TreeNode* pleft;
    TreeNode* pright;
    TreeNode* p_parent;
    TreeNode(T& element);
    TreeNode();
};

template 
TreeNode::TreeNode(T& element)
{
    this->element = element;
    this->pleft = nullptr;
    this->pright = nullptr;
    this->p_parent = nullptr;
}

template 
TreeNode::TreeNode()
{
    this->pleft = nullptr;
    this->pright = nullptr;
    this->p_parent = nullptr;
}

template 
class BsortTree
{
public:
    BsortTree();
   // ~BsortTree();
    VOID InOrderTraverse(TreeNode* pNode); //中序遍历
    VOID PreOrderTraverse(TreeNode* pNode);//前序遍历
    VOID PostOrderTraverse(TreeNode* pNode);//后序遍历
    TreeNode* GetRoot();//返回根节点
    int GetDepth(TreeNode* Node);// 返回当前节点高度
    BOOL IsEmpty();//判断树是否为空
    DWORD Insert(T element);//新增节点
    VOID Delete(T element);//删除节点

private:
    VOID Init();//初始化一些数值
    VOID clear(TreeNode* pNode);//删除所有节点
    DWORD InsertNode(TreeNode* pNode, T element);
    VOID DeleteNode(T element, TreeNode* pNode);
    TreeNode* GetMinNode(TreeNode* pNode);//获取以pNode为根的最小节点,注意传入的是要查询的结点的左子树
    TreeNode* GetMaxNode(TreeNode* pNode);//获取以pNode为根的最大节点,注意传入的是要查询结点的右子树
private:
    TreeNode* pRootNode; //根节点指针
    int size;//树中元素总数
};

template 
TreeNode* BsortTree::GetMinNode(TreeNode* pNode)//获取以pNode为根的最小节点
{
    if (pNode==nullptr)
    {
        cout << "没有最小的结点!" << endl;
        return 0;
    }
    else if (pNode->pright == nullptr)
    {
        return pNode;
    }
    GetMinNode(pNode->pright);
}

template 
TreeNode* BsortTree::GetMaxNode(TreeNode* pNode)
{
    if (pNode == nullptr)
    {
        cout << "没有最小的结点!" << endl;
        return 0;
    }
    else if (pNode->pleft == nullptr)
    {
        return pNode;
    }
    GetMaxNode(pNode->pleft);
}

template 
VOID BsortTree::Delete(T element)//删除节点
{
    if (this->pRootNode == nullptr)
    {
        cout << "没有元素可以删除!" << endl;
    }
    else
    {
        DeleteNode(element, this->GetRoot());
    }
}

template 
VOID BsortTree::DeleteNode(T element, TreeNode* pNode)
{
    if (pNode->element == element)
    {
        if (pNode->pleft == nullptr && pNode->pright == nullptr)
        {
            TreeNode* temp_ptr = pNode->p_parent;
            temp_ptr->pleft = nullptr;
            delete pNode;
         
        }
        
        else if (pNode->pleft == nullptr && pNode->pright != nullptr)
        {
            TreeNode* temp_ptr = pNode->p_parent;
            temp_ptr->pright = pNode->pright;
            pNode->pright = nullptr;
            delete pNode->pright;
  
        }

        else if (pNode->pleft != nullptr && pNode->pright == nullptr)
        {
            TreeNode* temp_ptr = pNode->p_parent;
            temp_ptr->pleft = pNode->pleft;
            pNode->pleft = nullptr;
            delete pNode->pleft;
        
        }

        else if (pNode->pleft != nullptr && pNode->pright != nullptr)
        {
            TreeNode* temp_ptr = GetMaxNode(pNode->pleft);
            T temp_element = temp_ptr->element;
            pNode->element = temp_element;
            pNode->pleft = nullptr;
            DeleteNode(temp_element, temp_ptr);
        }
    }
    else
    {
        if (element > pNode->element)
        {
            DeleteNode(element, pNode->pright);
        }
        else
        {
            DeleteNode(element, pNode->pleft);
        }
    }
}


template 
DWORD BsortTree::Insert(T element)//新增节点
{
    if (this->GetRoot() == nullptr)
    {
        this->pRootNode = new TreeNode(element);
    }
    else
        return InsertNode(this->pRootNode,element);
}

template 
DWORD BsortTree::InsertNode(TreeNode* pNode, T element)
{
    if (pNode->element == element)
    {
        return 0;
    }

    else if (element < pNode->element && pNode->pleft == nullptr)
    {
        pNode->pleft = new TreeNode(element);
        pNode->pleft->p_parent = pNode;
    }

    else if (element > pNode->element && pNode->pright == nullptr)
    {
        pNode->pright = new TreeNode(element);
        pNode->pright->p_parent = pNode;
    }

    else
    {
        if (element < pNode->element)
        {
            InsertNode(pNode->pleft, element);
        }
        
        else
        {
            InsertNode(pNode->pright, element);
        }
    }
}


template 
BsortTree::BsortTree()
{
    Init();
}

template 
VOID BsortTree::Init()
{
	
	Monster m1(4, "混世魔王", 50);
	Monster m2(1, "金斯尔", 100);
	Monster m3(2, "游离", 74);
	Monster m4(3, "尔康", 56);
	Monster m5(5, "二讹", 88);
	Monster m6(6, "传奇", 999);
	TreeNode* n1 = new TreeNode(m1);
	TreeNode* n2 = new TreeNode(m2);
	TreeNode* n3 = new TreeNode(m3);
	TreeNode* n4 = new TreeNode(m4);
	TreeNode* n5 = new TreeNode(m5);
	TreeNode* n6 = new TreeNode(m6);

	this->pRootNode = n3;
	n3->pleft = n2;
    n2->p_parent = n3;
	n3->pright = n1;
    n1->p_parent = n3;
	n1->pleft = n4;
    n4->p_parent = n1;
	n1->pright = n5;
    n5->p_parent = n1;
	n5->pright = n6;
    n6->p_parent = n5;
	this->size = 6;
	/*
	   2
  1        4
	    3     5
			     6
	*/   //顺序是 2 3 4 1 5 6   21 32 43 14 55 66
}

template 
VOID BsortTree::InOrderTraverse(TreeNode* pNode)
{
    if (pNode == nullptr)
    {
        return ;
    }
    if (pNode != nullptr)
    {
        InOrderTraverse(pNode->pleft);
        cout << pNode->element << endl;
        InOrderTraverse(pNode->pright);
    }
}

template 
VOID BsortTree::PreOrderTraverse(TreeNode* pNode)
{
    if (pNode != nullptr)
    {
        cout << pNode->element << endl;
        PreOrderTraverse(pNode->pleft);
        PreOrderTraverse(pNode->pright);
    }
}

template 
VOID BsortTree::PostOrderTraverse(TreeNode* pNode)
{
    if (pNode != nullptr)
    {
       
        PostOrderTraverse(pNode->pleft);
        PostOrderTraverse(pNode->pright);
        cout << pNode->element << endl;
    }

}

template
TreeNode* BsortTree::GetRoot() {
    return pRootNode;
}

template// 返回当前节点高度,Node
int BsortTree::GetDepth(TreeNode* Node) {
    if (Node == nullptr) {
        return 0;
    }
    else
    {
        int Left = GetDepth(Node->pleft);
        int Right = GetDepth(Node->pright);
        return (Left > Right) ? (Left + 1) : (Right + 1);
    }

}

template 
BOOL BsortTree::IsEmpty()
{
    if (this->GetRoot() == nullptr)
    {
        return true;
    }
    else
        return false;
}



int main()
{
    BsortTree tree;
    //tree.InOrderTraverse(tree.GetRoot());
    //tree.PostOrderTraverse(tree.GetRoot());
    //tree.PreOrderTraverse(tree.GetRoot());
    cout << tree.GetDepth(tree.GetRoot()) << endl;
    Monster m(5, "二讹", 88);
    tree.Insert(m);
  //  tree.InOrderTraverse(tree.GetRoot());
    tree.Delete(m);
    tree.InOrderTraverse(tree.GetRoot());
    return 0;
}

你可能感兴趣的:(滴水逆向,逆向,算法)