#include<iostream>
using namespace std;
typedef struct BTNode
{
char data;
struct BTNode *lchild;
struct BTNode *rchild;
}BTNode;
void insert(BTNode *&p)
{
char x;
cin>>x;
if(x!='#')
{
p=(BTNode*)malloc(sizeof(BTNode));
p->lchild=NULL;
p->rchild=NULL;
p->data=x;
insert(p->lchild);
insert(p->rchild);
}
}
/*求树的深度*/
int getDepth(BTNode *p)
{
int LD,RD;
if(p==NULL)
return 0;
else
{
LD=getDepth(p->lchild);
RD=getDepth(p->rchild);
return (LD>RD?LD:RD)+1;
}
}
/*查找某一节点是否存在*/
void search(BTNode *p,BTNode *&q,char key)
{
if(p!=NULL)
{
if(p->data==key)
q=p;
else
{
search(p->lchild,q,key);
search(p->rchild,q,key);
}
}
}
/*输出先序遍历序列中第K个结点的值,假设K不大于总的记得点数*/
int n=0;
void trave(BTNode *p,int k)
{
if(p!=NULL)
{
++n;
if(k==n)
{
cout<<p->data<<endl;
return;
}
trave(p->lchild,k);
trave(p->rchild,k);
}
}
/*层次遍历*/
void level(BTNode *p)
{
int front,rear;
BTNode *que[maxSize];
front=rear=0;
BTNode *q;
if(p!=NULL)
{
rear=(rear+1)%maxSize;
que[rear]=p;
while(front!=rear)
{
front=(front+1)%maxSize;
q=que[front];
visit(q);
if(q->lchild!=NULL)
{
rear=(rear+1)%maxSize;
que[rear]=q->lchild;
}
if(q->rchilde!=NULL)
{
rear=(rear+1)%maxSize;
que[rear]=q->rchild;
}
}
}
}
int main()
{
BTNode *p;
insert(p);
cout<<getDepth(p)<<endl;
char key;
cin>>key;
BTNode *q=NULL;
search(p,q,key);
if(q!=NULL)
cout<<q->data<<endl;
else
cout<<"未找到数值"<<endl;
return 1;
}
posted on 2012-08-25 23:02
yyj 阅读(76)
评论(0) 编辑 收藏 引用