logo资料库

所有关于树的函数实现.doc

第1页 / 共3页
第2页 / 共3页
第3页 / 共3页
资料共3页,全文预览结束
//字符串匹配的简单算法//KMP(Knuth-Morris-Pratt)算法 #include #include const int MAX=20; class String {public: String(char c[]); int fastFind ( String pat ) const; void fail (); int simpleFind(String pat); //数据成员 char int curLen; int f[MAX]; ch[MAX]; //用在失效函数里标记传入位置的 k }; String :: String(char c[]) { curLen=strlen(c); for(int i=0;i
for ( int j = 1; j < lengthP; j++ ) { //依次求 f [j] int i = f [j-1]; while ( *(ch+j) != *(ch+i+1) && i >= 0 ) i = f [i]; //递推 if ( *(ch+j) == *(ch+i+1) ) else f [j] = i+1; f [j] = -1; } lengthT = curLen; } int String::simpleFind(String pat) { p= 0, k=t; int t = 0, int lengthP = pat.curLen, while(t < lengthT-lengthP) { if(ch[t]==pat.ch[p]) k=t+1; { p++; while(p < lengthP ) { if(ch[k]==pat.ch[p]){ else { t++; k=t; p++; p=0; break; } k++; } return t; } { t++; p++; k=t;} } } else return -1; } 字符串合并 template void List::Merge(List& hb){ //将当前链表 this 与链表 hb 按逆序合并,结果放在当前链表 this 中 //从 pa 链中摘下 //结果链表初始化 // 检测指针跳过表头结点 ListNode *pa,*pb,*q,*p; pa=first->link;pb=hb.first->link; first->link=NULL; while (pa!=NULL &&pb!=NULL){ //当两个链表都未结束时 if(pa->data<=pb->data) {q=pa;pa=pa->link;} else {q=pb;pb=pb->link;} q->link=first->link;first->link=q; } p=(pa!=NULL)? pa:pb; while(p!=NULL){ q=p; p=p->link; //链入结果链的链头 //从 pb 链中摘下
q->link=first->link; first->link=q; } 火车进站问题 公式:Cn 2n /n+1 int BinarySearchTree::countHeight(BSTNode *t) //返回树*t 的高度 { if(!t) return 0; int hl=countHeight(t->left); int hr=countHeight(t->right); if(hl>hr) return ++hl; else return ++hr; } void BinaryTree::iterativePreOrder() { BSTNode* t=root; Stack*> s(10); //不能以栈空作为判断条件 //s.push(t); while(t) { cout<key<<" if(t->right!=0) "; s.push(t->right); if(t->left!=0) t=t->left; else if(s.pop(t)); else break; } } void BinaryTree::iterativeInOrder() //非递归的中序树确实麻烦,我的程序还很不成熟 { //课件上的,太强了 Stack*> s(10); BSTNode *p=root; while(p||!s.isEmpty()) { if(p){s.push(p);p=p->left;} else{ s.pop(p); cout<key<<" "; p=p->right; } }
分享到:
收藏