搜索⼆叉树反汇编的实现前⾔:第⼀次写⼆叉树,真的脑壳痛
参考⽂章:
通过⼆叉树来进⾏遍历怪物的名称
实现代码如下:
#include "StdAfx.h"
#include<stdio.h>
#include<Windows.h>
class Monster
{
public:
int ID;
int Level;
char Name[20];acquire名词
public:
Monster(){
}
Monster(int ID, int Level, char* Name)
{
this->ID = ID;
this->Level = Level;
memcpy(&this->Name, Name, strlen(Name) + 1);
}
};
template<class T>
class TreeNode{
public:
T element; //当前节点存储的数据
TreeNode<T>* pLeft; //指向左⼦节点的指针
TreeNode<T>* pRight; //指向右⼦节点的指针
TreeNode(T& ele){
//初始化Node节点
memt(&element, 0, sizeof(TreeNode));
//为元素赋值
memcpy(&element, &ele, sizeof(T));
pLeft = pRight = NULL;
}
};
template<class T>
class BSortTree{
public:
BSortTree(); //构造函数
~BSortTree(); //析构函数
public:
void InOrderTraver(TreeNode<T>* pNode); //中序遍历
void PreOrderTraver(TreeNode<T>* pNode); //前序遍历
void PostOrderTraver(TreeNode<T>* pNode); //后序遍历
TreeNode<T>* GetRoot(); //返回根节点
int GetDepth(TreeNode<T>* pNode); //返回某个节点的⾼度/深度private:
void Init();
void Clear();
private:
TreeNode<T>* m_pRoot; //根结点指针
int size; //树中元素总个数
};
template<class T>
愁苦
BSortTree<T>::BSortTree()
{
Init();
}
template<class T>
BSortTree<T>::~BSortTree(){
//释放所以节点空间
this->Clear();
}
template<class T>
void BSortTree<T>::Clear()
{
if(this->m_pRoot != NULL){
this->m_pRoot = this->m_pRoot->pLeft;
this->Clear();
this->m_pRoot = this->m_pRoot->pLeft;
this->Clear();
}el{
delete this->m_pRoot;
printf("%s",((T*)&this->m_pRoot->element)->Name);
this->m_pRoot = NULL;
}
}
comparetemplate<class T>
void BSortTree<T>::Init()
{
Monster m1(1, 1, "刺猬");
Monster m2(2, 2, "野狼");
Monster m3(3, 3, "野猪");
Monster m4(4, 4, "⼠兵");
Monster m5(5, 5, "⽕龙");
Monster m6(6, 6, "独⾓兽");
Monster m7(7, 7, "江湖⼤盗");
人人都恨克里斯/
/每个节点中都存储相关怪物的信息
TreeNode<Monster>* n1 = new TreeNode<Monster>(m1); TreeNode<Monster>* n2 = new TreeNode<Monster>(m2); TreeNode<Monster>* n3 = new TreeNode<Monster>(m3); TreeNode<Monster>* n4 = new TreeNode<Monster>(m4); TreeNode<Monster>* n5 = new TreeNode<Monster>(m5); TreeNode<Monster>* n6 = new TreeNode<Monster>(m6); TreeNode<Monster>* n7 = new TreeNode<Monster>(m7);
//构建⼆叉树节点
m_pRoot = n5;
n5->pLeft = n4;
n5->pRight = n6;
n4->pLeft = n1;
n1->pRight = n2;
n6->pLeft = n3;
n3->pRight = n7;
size = 7;
disdain/*
5
4 6
1 3
2 7
*/
}
template<class T>
TreeNode<T>* BSortTree<T>::GetRoot()
{
return m_pRoot;
}
template<class T>
int BSortTree<T>::GetDepth(TreeNode<T>* pNode)
{
if (pNode == NULL)
{
return 0;
}
el
{
//递归深度
int m = GetDepth(pNode->pLeft);
int n = GetDepth(pNode->pRight);
return (m > n) ? (m + 1) : (n + 1);
}
}
template<class T>
void BSortTree<T>::PreOrderTraver(TreeNode<T>* pNode)
{
//前序遍历所有怪物,列出怪的名字
if(pNode != NULL){
printf("%s %d\n",((T*)&pNode->element)->Name,((T*)&pNode->element)->ID); PreOrderTraver(pNode->pLeft);
PreOrderTraver(pNode->pRight);
}
//if(pNode->pLeft != NULL){
//InOrderTraver(pNode->pRight);
//}
}
template<class T>
void BSortTree<T>::InOrderTraver(TreeNode<T>* pNode)
{
//中序遍历所有怪物,列出怪的名字
if(pNode != NULL){
InOrderTraver(pNode->pLeft); //
printf("%s %d\n",((T*)&pNode->element)->Name,((T*)&pNode->element)->ID); InOrderTraver(pNode->pRight); //
}
}
template<class T>
void BSortTree<T>::PostOrderTraver(TreeNode<T>* pNode)
{
//后序遍历所有怪物,列出怪的名字
if(pNode != NULL){
PostOrderTraver(pNode->pLeft); //
PostOrderTraver(pNode->pRight); //
printf("%s %d\n",((T*)&pNode->element)->Name,((T*)&pNode->element)->ID); }
}
int main(){
BSortTree<Monster>* myTree = new BSortTree<Monster>();
myTree->PreOrderTraver(myTree->GetRoot());
printf("=========================================\n");
myTree->InOrderTraver(myTree->GetRoot());
printf("=========================================\n");
myTree->PostOrderTraver(myTree->GetRoot());
matrix是什么意思
printf("=========================================\n");
printf("%d",myTree->GetDepth(myTree->GetRoot()));
return 0;
}
搜索⼆叉树来进⾏增加/删除/搜索相应的节点
实现代码如下:
#include "StdAfx.h"
#define SUCCESS 1 // 执⾏成功
#define ERROR -1 // 执⾏失败
template<class T>
class TreeNode{
imac是什么public:
T element; //当前节点存储的数据
TreeNode<T>* pLeft; //指向左⼦节点的指针
TreeNode<T>* pRight; //指向右⼦节点的指针
TreeNode<T>* pParent; //指向⽗结点的指针
TreeNode(T& ele){
//初始化Node节点
memt(&element,0,sizeof(TreeNode));
//为元素赋值
memcpy(&element,&ele,sizeof(T));
pLeft = pRight = pParent = NULL;
}
//重载== ⽐较两结点是否相等
bool operator==(TreeNode<T>* node){
return node->element == element?true:fal;
}
/*
TreeNode<T> operator=(TreeNode<T>* node){
this->element = node->element;
return *this;
}
*/
};
template<class T>loveroot
class BSortTree{
public:
英语 学习 网站BSortTree(); //构造函数
~BSortTree(); //析构函数
public: //判断树是否为空
bool IsEmpty(); //新增节点
DWORD Inrt(T element); //删除节点
void Delete(T element);
TreeNode<T>* Search(T element); //查找节点
void InOrderTraver(TreeNode<T>* pNode); //中序遍历
void PreOrderTraver(TreeNode<T>* pNode); //前序遍历
void PostOrderTraver(TreeNode<T>* pNode); //后序遍历
private:
cet 4
TreeNode<T>* GetMaxNode(TreeNode<T>* pNode); //获取以pNode为根的最⼤节点
TreeNode<T>* GetMinNode(TreeNode<T>* pNode); //获取以pNode为根的最⼩节点
TreeNode<T>* SearchNode(TreeNode<T>* pNode,T element); //获取以pNode为根的最⼩节点 DWORD InrtNode(T element, TreeNode<T>* pNode); //新增节点
void DeleteNode(T element, TreeNode<T>* pNode); //删除节点
void Clear(TreeNode<T>* pNode); //清空所有节点