核心:完全二叉树第i个节点的左节点,对应的下标一定是2*i,右节点一定是2*i+1。
用num表示这个节点应该有的下标(虽然不用数组储存,以1开始),如果以构建完全二叉树的方式去构建树。若建树过程中,num大于节点数n,则不是完全二叉搜索树,否则是;
- 搜索树的构建:
int n;
int cnt = 0;
int num = 1;
array<int, 100>das;
typedef struct node* POS;
struct node {
int d;
int id;
POS l,r;
};
bool _yes = 1;
void creat(POS& tree, int x, int num)
{
//注意tree传入递归以后,只是增加了节点,tree任然是头节点
if ( num > n )
//大于表明不是完全二叉树
_yes = 0;
if ( !tree )
{
tree = new node;
tree->d = x;
tree->l = tree->r = nullptr;
return;
}
if ( x > tree->d )
creat(tree->l, x, num * 2);//左节点
else creat(tree->r, x, num * 2 + 1);//右节点
}
- 层次遍历
void ord(POS&a)
{
if ( !a ) return;
queue<POS> en;
en.push(a);
bool once = 1;
while ( !en.empty() )
{
POS te = en.front();
en.pop();
if ( once )
{
cout << te->d;
once = 0;
}
else {
cout << " " << te->d;
}
if ( te->l )
{
en.push(te->l);
}
if ( te->r )
{
en.push(te->l);
}
}
}
- 完整源码(指针变量以&传参,与指针value传参的区别)
int n;
int cnt = 0;
int num = 1;
array<int, 100>das;
typedef struct node* POS;
struct node {
int d;
int id;
POS l,r;
};
bool _yes = 1;
//传参引用
void creat(POS& tree, int x, int num)
{
if ( num > n )
_yes = 0;
if ( !tree )
{
//由于是引用相当于tree对应的实参 被重新赋值了new node的地址
//如果不是ref 语义,则会导致tree指向的地址改变,不在指向原有的实参的地址
tree = new node;
tree->d = x;
tree->l = tree->r = nullptr;
return;
}
if ( x > tree->d )
creat(tree->l, x, num * 2);
else creat(tree->r, x, num * 2 + 1);
}
//非ref语义creat函数
POS creat(POS tree, int x, int num)
{
if ( num > n )
_yes = 0;
if ( !tree )
{
POS te = new node;
te->d = x;
te->l = te->r = nullptr;
return te;
}
if ( x > tree->d )
tree->l=creat(tree->l, x, num * 2);
else tree->r=creat(tree->r, x, num * 2 + 1);
return tree;
}
/*main code haved changed;
ifor(i, 1, n)
{
int x;
cin >> x;
h=creat(h, x, 1);
}*/
void ord(POS&a)
{
if ( !a ) return;
queue<POS> en;
en.push(a);
bool once = 1;
while ( !en.empty() )
{
POS te = en.front();
en.pop();
if ( once )
{
cout << te->d;
once = 0;
}
else {
cout << " " << te->d;
}
if ( te->l )
{
en.push(te->l);
}
if ( te->r )
{
en.push(te->l);
}
}
}
int main(int args, char** argv)
{
/*ios::sync_with_stdio(false);
cin.tie(nullptr);
cin.tie(nullptr);*/
cin >> n;
POS h = nullptr;
ifor(i, 2, n)
{
int x;
cin >> x;
creat(h, x, 1);
}
ord(h);
coute;
if ( _yes ) cout << "YES";
else cout << "NO";
return 0;
}