老师能上传一下你的源代码吗?
老师能上传一下你的源代码吗?
老师能上传一下你的源代码吗?
2019-08-15
#include <iostream>
#include "Tree.h"
using namespace std;
Tree::Tree(int size)
{
m_iSize=size;
m_pTree=new int[size];
for(int i=0; i<size; i++)
{
m_pTree[i]=0;
}
}
Tree::~Tree()
{
delete []m_pTree;
m_pTree=NULL;
}
int *Tree::SearchNode(int nodeIndex)
{
if(nodeIndex<0||nodeIndex>=m_iSize)
{
return NULL;
}
if(m_pTree[nodeIndex]==0)
{
return NULL;
}
return &m_pTree[nodeIndex];
}
bool Tree::AddNode(int nodeIndex,int direction,int *pNode)
{
if(nodeIndex<0||nodeIndex>=m_iSize)
{
return false;
}
if(m_pTree[nodeIndex]==0)
{
return false;
}
if(direction==0)
{
if(nodeIndex*2+1<0||nodeIndex*2+1>=m_iSize)
{
return false;
}
if(m_pTree[nodeIndex*2+1]!=0)
{
return false;
}
m_pTree[nodeIndex*2+1]=*pNode;
}
if(direction==1)
{
if(nodeIndex*2+2<0||nodeIndex*2+2>=m_iSize)
{
return false;
}
if(m_pTree[nodeIndex*2+2]!=0)
{
return false;
}
m_pTree[nodeIndex*2+2]=*pNode;
}
return true;
}
bool Tree::DeleteNode(int nodeIndex,int *pNode)
{
if(nodeIndex<0||nodeIndex>=m_iSize)
{
return false;
}
if(m_pTree[nodeIndex]==0)
{
return false;
}
*pNode=m_pTree[nodeIndex];
m_pTree[nodeIndex]=0;
return true;
}
void Tree::TreeTraverse()
{
for(int i=0;i<m_iSize;i++)
{
cout<<m_pTree[i]<<" ";
}
}
#ifndef TREE_H
#define TREE_H
#ifdef __cplusplus
class Tree
{
public:
Tree(int size);
~Tree();
int *SearchNode(int nodeIndex);
bool AddNode(int nodeIndex,int direction,int *pNode);
bool DeleteNode(int nodeIndex,int *pNode);
void TreeTraverse();
private:
int *m_pTree;
int m_iSize;
};
#endif // TREE_H
#endif // TREE_H
举报