文章
59
粉丝
0
获赞
331
访问
8.6k
#include <iostream>
#include <map>
using namespace std;
map<int,int> father;//store node's father
typedef struct BTNode {
int value;
struct BTNode* lc, * rc;
}BTNode, * BST;
void InsertTree(BST& T,int temp, int x)
{
if (T == NULL)
{
T = new BTNode;
T->value = x;
father[x]=temp;
T->lc = NULL;
T->rc = NULL;
return;
}
if (T->value == x)
return;
else if (T->value > x)
InsertTree(T->lc,T->value, x);
else
InsertTree(T->rc,T->value, x);
}
void preorder(BST T, string& s)
{
if (T)
{
s += T->value;
preorder(T->lc, s);
preorder(T->rc, s);
}
}
void DeleteTree(BST& T)
{
if (T)
{
DeleteTree(T->lc);
DeleteTree(T->rc);
delete T;
}
}
int main()
{
int n;
while (cin >> n)
{
BST T = NULL;
for (int i = 0; i < n; i++)
{
int x;
cin>>x;
InsertTree(T,-1, x);
cout<<father[x]<<endl;
}...
登录后发布评论
暂无评论,来抢沙发