数据结构实验五
源代码
- #include
- using namespace std;
- typedef struct node
- {
- struct node *leftChild;
- struct node *rightChild;
- char data;
- }BiTreeNode, *BiTree;
- BiTreeNode *createnode(char c)
- {
- BiTreeNode *q=new BiTreeNode;
- q->leftChild=NULL;
- q->rightChild=NULL;
- q->data=c;
- return q;
- }
- void createBiTree(BiTree &T)
- {
- char c;
- cin >> c;
- if(c == '#')
- T = NULL;
- else
- {
- T = new BiTreeNode;
- T->data = c;
- createBiTree(T->leftChild);
- createBiTree(T->rightChild);
- }
- }
- int max(int a, int b )
- {
- return a < b ? b : a;
- }
- int Depth(BiTree T)
- {
- if ( T== NULL)
- return 0;
- int leftDepth = Depth(T->leftChild);
- int rightDepth = Depth(T->rightChild);
- return 1+ max(leftDepth, rightDepth);
- }
- void PrintNodeatlevel(BiTree T,int level)
- {
- if(T==NULL || level<1)
- return;
- if(level==1)
- {
- cout<data<<" ";
- return;
- }
- PrintNodeatlevel(T->leftChild,level-1);
- PrintNodeatlevel(T->rightChild,level-1);
- }
- void LevelTraverse(BiTree T)
- {
- if(T==NULL)
- return;
- int depth=Depth(T);
- for(int i=1;i<=depth;i++)
- {
- PrintNodeatlevel(T,i);
- cout<leftChild==NULL && T->rightChild==NULL)
- {
- cout<data<<" ";
- }
- LeavesTraverse(T->leftChild);
- LeavesTraverse(T->rightChild);
- }
- int getleafnode(BiTree T)
- {
- if(T==NULL)
- return 0;
- if(T->leftChild==NULL && T->rightChild==NULL)
- return 1;
- return getleafnode(T->leftChild)+getleafnode(T->rightChild);
- }
- int getallnode(BiTree T)
- {
- if(T==NULL)
- return 0;
- return 1+getallnode(T->leftChild)+getallnode(T->rightChild);
- }
- BiTreeNode *parent(BiTreeNode *T,char c)
- {
- if(T==NULL)
- return NULL;
- if(T->leftChild!=NULL && T->leftChild->data==c)
- {
- return T;
- }
- if(T->rightChild!=NULL && T->rightChild->data==c)
- {
- return T;
- }
- BiTreeNode *p=parent(T->leftChild,c);
- if(p!=NULL)
- {
- return p;
- }
- p=parent(T->rightChild,c);
- return p;
- }
- BiTreeNode *children(BiTreeNode *T,char c)
- {
- if(T==NULL)
- return NULL;
- if(T->data==c)
- {
- if(T->leftChild!=NULL && T->rightChild!=NULL)
- cout<<"左孩子:"<leftChild->data<rightChild->data<leftChild==NULL && T->rightChild!=NULL)
- cout<<"左孩子为空!"<rightChild->data<leftChild!=NULL && T->rightChild==NULL)
- cout<<"左孩子:"<leftChild->data<leftChild==NULL && T->rightChild==NULL)
- cout<<"没有孩子!"<leftChild,c);
- if(p!=NULL)
- {
- return p;
- }
- p=children(T->rightChild,c);
- return p;
- }
- void preorder(BiTree T)
- {
- if(T==NULL)return;
- if(T!=NULL)
- {
- cout<data<<" ";
- preorder(T->leftChild);
- preorder(T->rightChild);
- }
- }
- void inorder(BiTree T)
- {
- if(T==NULL)return;
- if(T!=NULL)
- {
- inorder(T->leftChild);
- cout<data<<" ";
- inorder(T->rightChild);
- }
- }
- void postorder(BiTree T)
- {
- if(T==NULL)return;
- if(T!=NULL)
- {
- postorder(T->leftChild);
- postorder(T->rightChild);
- cout<data<<" ";
- }
- }
- // -----------------------------------------------------------------------------------------------
- int main()
- {
- BiTree T;
- char x;
- BiTreeNode *p;
- cout<<"输入一棵树:";
- createBiTree(T);
- cout<>x;
- p=parent(T,x);
- if(p!=NULL)
- {
- cout<<"双亲:"<data;
- }
- else cout<<"找不到双亲!";
- cout<>x;
- p=children(T,x);
- cout<
运行结果