2013年新疆维吾尔自治区JAVA最新版本要领
- 格式:rtf
- 大小:76.56 KB
- 文档页数:4
1、后序遍历最后访问根结点,即在递归算法中,根是压在栈底的。
采用后序非递归算法,栈中存放二叉树结点的指针,当访问到某结点时,栈中所有元素均为该结点的祖先。
本题要找p和q 的最近共同祖先结点r ,不失一般性,设p在q的左边。
后序遍历必然先遍历到结点p,栈中元素均为p的祖先。
将栈拷入另一辅助栈中。
再继续遍历到结点q时,将栈中元素从栈顶开始逐个到辅助栈中去匹配,第一个匹配(即相等)的元素就是结点p 和q的最近公共祖先。
typedef struct{BiTree t;int tag;//tag=0 表示结点的左子女已被访问,tag=1表示结点的右子女已被访问}stack;stack s[],s1[];//栈,容量够大BiTree Ancestor(BiTree ROOT,p,q,r)//求二叉树上结点p和q的最近的共同祖先结点r。
{top=0; bt=ROOT;while(bt!=null ||top>0){while(bt!=null && bt!=p && bt!=q) //结点入栈{s[++top].t=bt; s[top].tag=0; bt=bt->lchild;} //沿左分枝向下if(bt==p) //不失一般性,假定p在q的左侧,遇结点p时,栈中元素均为p的祖先结点{for(i=1;i<=top;i++) s1[i]=s[i]; top1=top; }//将栈s的元素转入辅助栈s1 保存if(bt==q) //找到q 结点。
for(i=top;i>0;i--)//;将栈中元素的树结点到s1去匹配{pp=s[i].t;for (j=top1;j>0;j--)if(s1[j].t==pp) {printf(“p 和q的最近共同的祖先已找到”);return (pp);}}while(top!=0 && s[top].tag==1) top--; //退栈if (top!=0){s[top].tag=1;bt=s[top].t->rchild;} //沿右分枝向下遍历}//结束while(bt!=null ||top>0)return(null);//q、p无公共祖先}//结束Ancestor2、数组A和B的元素分别有序,欲将两数组合并到C数组,使C仍有序,应将A和B拷贝到C,只要注意A和B数组指针的使用,以及正确处理一数组读完数据后将另一数组余下元素复制到C中即可。
void union(int A[],B[],C[],m,n)//整型数组A和B各有m和n个元素,前者递增有序,后者递减有序,本算法将A和B归并为递增有序的数组C。
{i=0; j=n-1; k=0;// i,j,k分别是数组A,B和C的下标,因用C描述,下标从0开始while(i<m && j>=0)if(a[i]<b[j]) c[k++]=a[i++] else c[k++]=b[j--];while(i<m) c[k++]=a[i++];while(j>=0) c[k++]=b[j--];}算法结束4、要求二叉树按二叉链表形式存储。
15分(1)写一个建立二叉树的算法。
(2)写一个判别给定的二叉树是否是完全二叉树的算法。
BiTree Creat() //建立二叉树的二叉链表形式的存储结构{ElemType x;BiTree bt;scanf(“%d”,&x); //本题假定结点数据域为整型if(x==0) bt=null;else if(x>0){bt=(BiNode *)malloc(sizeof(BiNode));bt->data=x; bt->lchild=creat(); bt->rchild=creat();}else error(“输入错误”);return(bt);}//结束 BiTreeint JudgeComplete(BiTree bt) //判断二叉树是否是完全二叉树,如是,返回1,否则,返回0{int tag=0; BiTree p=bt, Q[]; // Q是队列,元素是二叉树结点指针,容量足够大if(p==null) return (1);QueueInit(Q); QueueIn(Q,p); //初始化队列,根结点指针入队while (!QueueEmpty(Q)){p=QueueOut(Q); //出队if (p->lchild && !tag) QueueIn(Q,p->lchild); //左子女入队else {if (p->lchild) return 0; //前边已有结点为空,本结点不空else tag=1; //首次出现结点为空if (p->rchild && !tag) QueueIn(Q,p->rchild); //右子女入队else if (p->rchild) return 0; else tag=1;} //whilereturn 1; } //JudgeComplete3、由二叉树的前序遍历和中序遍历序列能确定唯一的一棵二叉树,下面程序的作用是实现由已知某二叉树的前序遍历和中序遍历序列,生成一棵用二叉链表表示的二叉树并打印出后序遍历序列,请写出程序所缺的语句。
#define MAX 100typedef struct Node{char info; struct Node *llink, *rlink; }TNODE;char pred[MAX],inod[MAX];main(int argc,int **argv){ TNODE *root;if(argc<3) exit 0;strcpy(pred,argv[1]); strcpy(inod,argv[2]);root=restore(pred,inod,strlen(pred));postorder(root);}TNODE *restore(char *ppos,char *ipos,int n){ TNODE *ptr; char *rpos; int k;if(n<=0) return NULL;ptr->info=(1)_______;for((2)_______ ; rpos<ipos+n;rpos++) if(*rpos==*ppos) break;k=(3)_______;ptr->llink=restore(ppos+1, (4)_______,k );ptr->rlink=restore ((5)_______+k,rpos+1,n-1-k);return ptr;}postorder(TNODE*ptr){ if(ptr=NULL) return;postorder(ptr->llink); postorder(ptr->rlink); printf(“%c”,ptr->info); }4、约瑟夫环问题(Josephus问题)是指编号为1、2、…,n的n(n>0)个人按顺时针方向围坐成一圈,现从第s个人开始按顺时针方向报数,数到第m个人出列,然后从出列的下一个人重新开始报数,数到第m的人又出列,…,如此重复直到所有的人全部出列为止。
现要求采用循环链表结构设计一个算法,模拟此过程。
#include<stdlib.h>typedef int datatype;typedef struct node{datatype data;struct node *next;}listnode;typedef listnode *linklist;void jose(linklist head,int s,int m){linklist k1,pre,p;int count=1;pre=NULL;k1=head; /*k1为报数的起点*/while (count!=s) /*找初始报数起点*/{pre=k1;k1=k1->next;count++;}while(k1->next!=k1) /*当循环链表中的结点个数大于1时*/{ p=k1; /*从k1开始报数*/count=1;while (count!=m) /*连续数m个结点*/{ pre=p;p=p->next;count++;}pre->next=p->next; /*输出该结点,并删除该结点*/printf("%4d",p->data);free(p);k1=pre->next; /*新的报数起点*/}printf("%4d",k1->data); /*输出最后一个结点*/free(k1);}main(){linklist head,p,r;int n,s,m,i;printf("n=");scanf("%d",&n);printf("s=");scanf("%d",&s);printf("m=",&m);scanf("%d",&m);if (n<1) printf("n<0");else{/*建表*/head=(linklist)malloc(sizeof(listnode)); /*建第一个结点*/ head->data=n;r=head;for (i=n-1;i>0;i--) /*建立剩余n-1个结点*/{ p=(linklist)malloc(sizeof(listnode));p->data=i;p->next=head;head=p;}r->next=head; /*生成循环链表*/jose(head,s,m); /*调用函数*/}}。