新建二叉树

/*

struct BTreeNode
{
T val;
BTreeNode* parent;
BTreeNode* left;
BTreeNode* right;

};

*/

void create(BTreeNode<int>* p, int f, int end)
{
int data;
BTreeNode<int>* ptemp = new BTreeNode<int>;
cin >> data;
if(data != end)
{
ptemp->val = data;
ptemp->left = NULL;
ptemp->right = NULL;
ptemp->parent = NULL;


if(f == 1)
{
p->left = ptemp;
ptemp->parent = p;
}
if(f == 2)
{
p->right = ptemp;
ptemp->parent = p;
}


create(ptemp, 1, end);
create(ptemp, 2, end);
}


}
 BTreeNode<int>* CreateBTree(int end)
{
int data;
BTreeNode<int>* root = new BTreeNode<int>;
cin >> data;
if(data == end)
return root;
root->val = data;
root->left = NULL;
root->right = NULL;
root->parent = NULL;


create(root, 1, end);
create(root, 2, end);


return root;
}
阅读更多

没有更多推荐了,返回首页