08 2012 档案
摘要:如何能够判断出是否是有环路,及如何找到这个环路的入口呢?解法如下: 当p2按照每次2步,p1每次一步的方式走,发现p2和p1重合,确定了单向链表有环路了接下来,让p2回到链表的头部,重新走,每次步长不是走2了,而是走1,那么当p1和p2再次相遇的时候,就是环路的入口了。这点可以证明的:在p2和p1第一次相遇的时候,假定p1走了n步骤,环路的入口是在p步的时候经过的,那么有p1走的路径: p+c = n; c为p1和p2相交点,距离环路入口的距离p2走的路径: p+c+k*L = 2*n; L为环路的周长,k是整数显然,如果从p+c点开始,p1再走n步骤的话,还可以回到p+c这个点同时...
阅读全文
摘要:二叉排序树插入结点BSTree *InsertBST(BSTree *bst,int key) //插入节点{ BSTree *p,*s,*pre; s=(BSTree*)malloc(sizeof(BSTreeNode)); s->data=key; s->lchild=s->rchild=NULL; if(bst==NULL) { bst=s; return bst; } p=bst; while(p) { pre=p;//指向其父节点 if(p->data==key)return p; else if(p->data>key)p=p->lchil
阅读全文
摘要:删除二叉排序树中值为k的结点用被删结点左子树最右下的结点的值代替被删结点的值,然后删去最右下的结点#include "stdafx.h"#include<iostream>using namespace std;typedef struct BSTreeNode{ int data; struct BSTreeNode *lchild,*rchild;}BSTree;int _tmain(int argc, _TCHAR* argv[]){ return 0;}void Delete(BSTree *bst,int x){ BSTree *f,*p=bst; w
阅读全文
摘要:1.将二叉树转换为顺序存储结构,按完全二叉树形式存储,无元素的结点当做虚结点。按层次顺序遍历二叉树,设根结点编号为0,设置一队列,将结点及其序号入队。出队时将结点及其编号写入顺序存储结构。#include "stdafx.h"#include<iostream>using namespace std;typedef struct BTreeNode{ int data; struct BTreeNode *lchild,*rchild;}BTree;int _tmain(int argc, _TCHAR* argv[]){ return 0;}typedef s
阅读全文
摘要:1.删除以元素值x为根结点的子树,并释放其空间#include "stdafx.h"#include<iostream>using namespace std;typedef struct BTreeNode{ int data; struct BTreeNode *lchild,*rchild;}BTree;int _tmain(int argc, _TCHAR* argv[]){ return 0;}void DeleteXTree (BTree *bt)//删除以bt为根的字树{ DeleteXTree(bt->lchild); DeleteXTre
阅读全文
摘要:1.递归判断相等#include "stdafx.h"#include<iostream>using namespace std;typedef struct BTreeNode{ int data; struct BTreeNode *lchild,*rchild;}BTree;int _tmain(int argc, _TCHAR* argv[]){ return 0;}bool equal(BTree *p,BTree *q){ if(!p&&!q)return true;//若都为空 else if(!p&&q||p&am
阅读全文
摘要:1.非递归,不允许用栈,求前序遍历最后一个结点思想:前序遍历最后一个结点:若有右子树,则为右子树最右下结点,若有左子树,则为左子树最右下结点,否则为根结点。#include "stdafx.h"#include<iostream>using namespace std;typedef struct BTreeNode{ int data; struct BTreeNode *lchild,*rchild;}BTree;int _tmain(int argc, _TCHAR* argv[]){ return 0;}BTree *LastNode(BTree *b)
阅读全文
摘要:1.打印值为x的结点的所有祖先栈从0开始,top初始值-1#include "stdafx.h"#include<iostream>using namespace std;typedef struct BTreeNode{ int data; struct BTreeNode *lchild,*rchild;}BTree;typedef struct stacknode{ BTree *node; int tag;//tag为0表示结点左子女已经访问,为1表示右子女已经访问}stack;int _tmain(int argc, _TCHAR* argv[]){
阅读全文
摘要:2.中序遍历 3.后序遍历 4.使用栈的非递归遍历 非递归实现 根据前序遍历访问的顺序,优先访问根结点,然后再分别访问左孩子和右孩子。即对于任一结点,其可看做是根结点,因此可以直接访问,访问完之后,若其左孩子不为空,按相同规则访问它的左子树;当访问其左子树时,再访问它的右子树。因此其处理过程如下:
阅读全文
摘要:算法1:若无左子女则不应该有右子女 2.求二叉树宽度 3.二叉树k层叶子结点
阅读全文
摘要:3)分别求二叉树的叶结点,度数为1的结点,度数为2的结点。#include "stdafx.h"#include<iostream>using namespace std;typedef struct BTreeNode{ float data; char optr; struct BTreeNode *lchild,*rchild;}BTree;int _tmain(int argc, _TCHAR* argv[]){ return 0;}int n0,n1,n2;//全局变量,用于统计结点数void Count(BTree *t){ if(t)//前序遍历
阅读全文
摘要:1)二叉树仅含二元运算符,存储在二叉树BT中,写出计算该算术表达式的值的算法。#include "stdafx.h"#include<iostream>using namespace std;typedef struct BTreeNode{ float data; char optr; struct BTreeNode *lchild,*rchild;}BiNode,*BTree;int _tmain(int argc, _TCHAR* argv[]){ return 0;}float PostEval(BTree bt)//后序遍历{ float lv,rv
阅读全文
摘要:12)按递增次序输出链表结点#include "stdafx.h"#include<iostream>using namespace std;typedef struct node{ int data; struct node *next;}Listnode,*LinkList;int _tmain(int argc, _TCHAR* argv[]){ return 0;}void reverse(LinkList &La){ while(La->next) { LinkList pre=La; LinkList r=pre;//r为辅助指针记录前
阅读全文
摘要:9)【判断b链表是否a链表的子序列】假设带头结点错误代码:没有考虑重复结点的情况。#include "stdafx.h"#include<iostream>using namespace std;typedef struct node{ int data; struct node *next;}Listnode,*LinkList;int _tmain(int argc, _TCHAR* argv[]){ return 0;}bool isofseries(LinkList a,LinkList b){ LinkList pa=a->next,pb=b-&
阅读全文