#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct TreeNode{
int data;
TreeNode *leftchild;
TreeNode *rightchild;
};
TreeNode* Insert(TreeNode* root, int x, int father){
if(root == NULL){
root = new TreeNode();
root->data = x;
root->leftchild = NULL;
root->rightchild = NULL;
printf("%d\n",father);
}else if(x < root->data){//插入左子树
root->leftchild = Insert(root->leftchild,x,root->data);//左孩子的父节点
}else{//插入右子树
root->rightchild = Insert(root->rightchild,x,root->data);
}
return root;
}
int main(){
int n;
while(scanf("%d",&n) != EOF){
TreeNode* root = NULL;
while(n--){
int x;
scanf("%d",&x);
root = Insert(root,x,-1);
}
}
return 0;
}
排序树建立并输出父节点
最新推荐文章于 2023-10-01 10:41:33 发布