#include<iostream> using namespace std; #include<stdio.h> #include<stdlib.h> /* 递归前中后遍历 */ typedef struct node { int data; struct node*left; struct node*right; }BTnode; BTnode* CreateTree(BTnode* root,int x) { if(!root) //如果root结点为空,创建叶子结点 { root = new BTnode; root->data = x; root->left=root->right=NULL; }else { if(root->data>x) root->left = CreateTree(root->left,x); //递归调用左 else if(root->data<x) root->right = CreateTree(root->right,x);//递归调用右 } return root; } void Inorder(BTnode* root) { if(root) { Inorder(root->left); cout<<root->data<<" "; Inorder(root->right); } } int i=0; int * tra(BTnode * root) { int a1[100]; if(root) { tra(root->left); a1[i]=root->data; i++; tra(root->right); } return a1; } int find(BTnode* root,int a,int n) { int *p=tra(root); int j=0; for(int i=0;i<n;i++) { if(a==p[i]) { j=1; } } return j; } int main(void) { BTnode * head = NULL; int x; int n; int i; int a; printf("请输入n="); scanf("%d",&n); printf("请输入二叉树的结点data\n"); for(i=0;i<n;i++) { scanf("%d",&x); head = CreateTree(head,x); } Inorder(head); cin>>a; if(find(head,a,n)==1) cout<<"find"<<endl; }
实验三草稿1
猜你喜欢
转载自www.cnblogs.com/ilovetheworld/p/10838826.html
今日推荐
周排行