题目内容

请完成下列二叉树的递归遍历算法#include #include #define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define OVERFLOW -2typedef int status;/*二叉树的二叉链表存储表示*/typedef struct BiNode{char Data;struct BiNode* lChild;struct BiNode* rChild; //左右孩子指针}BiNode,*pBiNode;BiNode *pRoot=NULL;status Visit(char Data){printf("%c",Data);return OK;}/*建立二叉链表*/status CreateTree(BiNode** pTree){//按先序次序输入二叉树中结点值,空格表示空树//构造二叉链表表示的二叉树char ch;scanf("%c",&ch);if(ch==' '){(*pTree)=NULL;}else{(*pTree)=(BiNode*)malloc(sizeof(BiNode));(*pTree)->Data=ch;CreateTree(&((*pTree)->lChild));CreateTree(&((*pTree)->rChild));}return OK;}/*先序遍历二叉树,递归算法*/status PreOrderTraval(BiNode* pTree){if(pTree){if(Visit(pTree->Data)){if(PreOrderTraval(pTree->lChild)){if(PreOrderTraval(pTree->rChild)){return OK;}}}return ERROR;}else{return OK;}}/*中序遍历二叉树,递归算法*/status InOrderTraval(BiNode* pTree){if(pTree){if(InOrderTraval(1)){if(Visit(2)){if(InOrderTraval(3)){return OK;}}}return ERROR;}else{return OK;}}/*后序遍历二叉树,递归算法*/status PostOrderTraval(BiNode* pTree){if(pTree){if(4){if(PostOrderTraval(pTree->rChild)){if(Visit(pTree->Data)){return OK;}}}return ERROR;}else{return OK;}}main(){system("cls");CreateTree(&pRoot);printf("\nPreOrder:");PreOrderTraval(pRoot);printf("\n");printf("\nInOrder:");InOrderTraval(pRoot);printf("\n");printf("\nPostOrder:");PostOrderTraval(pRoot);printf("\n");getch();}

查看答案
更多问题

链表插入操作:在线性表的第i-1个数据元素与第i个数据元素之间插入一个由符号e表示的数据元素。int ListInsert_L(LinkList L, int i, ElemType e) {//向链表中第i位插入数据estruct LNode *s,*p;int j;p = 1 ; j = 1;//p指针指向线性链表的头结点while (p && j <2) //循环查找第i-1位作为数据e的插入位置{ p =3 ; ++j; } //p指针向前移动if (!p || j > i-1)return ERROR;s =(LNode*)malloc(sizeof(LNode)); //见下图1)if ( s == NULL) return ERROR;s->4= e; //将插入数据e的值赋值给s结点的数据域1)5 =p->next ; 6=s; //旧链断开,新链产生2)3)return OK;}

删除链表中第i个结点:ElemType ListDelete_L(LinkList L, int i) {LinkList p,q;int j;ElemType e;p = L; j = 0;while (p->next && j < i-1) { p = p->next; ++j; }if (!(p->next) || j > i-1)return ERROR;q =1; 2=q->next;e = q->data; free(3);return e;}

SElemType Pop(struct SqStack *S)//出栈函数{SElemType e;if(1 ==2 ) {//判断栈是否为空printf("the stack is empty");return ERROR;}3 ;//将栈顶元素赋值给e,栈顶指针下移return e;}

填空以实现有序表的二分查找法(折半查找法)#include int Data[20]={12,16,19,22,25,32,39,48,55,57,58,63,68,69,70,78,84,88,91,97};int Binary_Search(int source[],int left,int right,int key){int middle;while(left<=right){middle=(1)/2;if(keysource[middle]){3=middle+1;}else if(key==source[middle]){printf("source[%d]=%d\n",middle,source[middle]);return 1;}}return 0;}

答案查题题库