#include "stdafx.h"
#include<iostream>
using namespace std;
class Tree
{
public:
Tree(int size,int *root);
~Tree();
bool AddNode(int index,int derection,int *node);
void Traverse();
private:
int *m_pTree;
int m_iSize;
};
Tree::Tree(int size,int *root)
{
m_iSize=size;
m_pTree=new int[size];
for(int i=0;i<m_iSize;i++)
{
m_pTree[i]=0;
}
m_pTree[0]=*root;
}
Tree::~Tree()
{
delete []m_pTree;
m_pTree=NULL;
}
bool Tree::AddNode(int index, int derection, int *node)
{
if(index<0||index>m_iSize)
{
return false;
}
if(index==0)
{
return false;
}
if(derection==0)
{
m_pTree[index*2+1]=*node;
}
if(derection==1)
{
m_pTree[index*2+2]=*node;
}
return true;
}
void Tree::Traverse()
{
for(int i=0;i<m_iSize;i++)
{
cout<<m_pTree[i]<<" ";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int root=3;
Tree *pTree=new Tree(10,&root);
int node1=5;
int node2=8;
pTree->AddNode(0,0,&node1);
pTree->AddNode(0,1,&node2);
pTree->Traverse();
delete pTree;
system("pause");
return 0;
}