二叉排序树 北邮 机试 静态实现

二叉排序树 北邮 机试 静态实现

#include<bits/stdc++.h>
using namespace std;
//二叉树的静态实现 
struct node{
    int l,r,data;
}Node[105];
int inde=0;
int newnode(int x){
	Node[inde].l=Node[inde].r=-1;
	Node[inde].data=x;
	return inde++;
 } 
 void insert(int &root,int x)//注意引用符 
 {
 	if(root==-1){
 		root=newnode(x);
 		return;
	 }
	 if(x<Node[root].data){
	 	if(Node[root].l==-1) cout<<Node[root].data<<endl;
	 	insert(Node[root].l,x);
	 }
	 else{//注意左右 
	 	if(Node[root].r==-1) cout<<Node[root].data<<endl;
	 	insert(Node[root].r,x);
	 }
 }
int main()
{
	int n,x;
	cin>>n;
	cout<<-1<<endl;//输出根节点 
	int rooot=-1;//永远的根节点,根节点设为-1才能跟insert函数匹配 
	for(int i=0;i<n;i++){
		cin>>x;//节点表是从0开始 
		insert(rooot,x);//在插入时输出父节点的值  
//******必须要用 rooot而不能用-1,因为insert函数中引用root,传的是地址不是数值 
	}	
	return 0;
 } 

二叉排序树 北邮 机试 静态实现