您的位置:首页 > 其它

字符串原始匹配和二叉树相等算法

2009-09-14 19:05 316 查看
1.原始字符串匹配,修改

#include "stdafx.h"

#include "iostream"
using namespace std;

int Index(char S[],int Slen,char T[],int Tlen,int pos);

int main(int argc, char* argv[])
{
	char ss[]="helloworld";
	char tt[]="wor";
	int slen=sizeof(ss);
	int tlen=sizeof(tt);
	int a=0;
	a=Index(ss,slen,tt,tlen,0);
	cout<<a<<endl;//将1修改为0
	
	return 0;
}

int Index(char S[],int Slen,char T[],int Tlen,int pos)
{
	int i=pos;
	int j=0;
	while(i<=Slen && j<=Tlen&&T[j]!='/0')
	{
		if(S[i]==T[j])
		{
			//++i;
			//++j;
			i++;
			j++;
		}
		else
		{
			//i=i-j+2;//这句是什么意思啊?
			i++;
			//j=1;
			j=0;
		}
	}
	// 	if(j>Tlen) 
	// 		return i-Tlen;
	// 	else 
	// 		return 0;
	//	cout<<i-j<<endl;
	return i-j;
}




//修改后二叉树

// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

// int main(int argc, char* argv[])

// }// {
// 	printf("Hello World!/n");
// 	return 0;

#include <stdio.h>
#include <malloc.h>
#include <iostream.h>

typedef struct node //二叉树的链式存储  
{  
	char c;    
	struct node *leftchild, *rightchild;    
} BinTNode,*BinTree;   

int CompTree(BinTree tree1, BinTree tree2)   
{
	//判断两棵二叉树是否相等,用递归算法   
	BinTree RootA=tree1;    
	BinTree RootB=tree2;    
//	while(RootA && RootB)   
	if (RootA&&RootB)
	{   
		if(RootA->c==RootB->c)   
		{   
// 			RootA=RootA->leftchild;   
// 			RootB=RootB->leftchild;   
			CompTree(RootA->leftchild, RootB->leftchild);   

			//继续判断右子树		
// 			RootA=RootA->rightchild;   
// 			RootB=RootB->rightchild;

			CompTree(RootA->rightchild, RootB->rightchild);  

		}
		else  
			return -1;  
	}   
	if (RootA!=NULL || RootB!=NULL)    
		return -1;   
	return 0;   
}   
void main(void)   
{   
	//手动建立root1,三个节点,a b c   
	BinTree root1 =(BinTNode*)malloc(sizeof(BinTNode));   
	BinTNode *lch1 =(BinTNode*)malloc(sizeof(BinTNode));   
	BinTNode *rch1 =(BinTNode*)malloc(sizeof(BinTNode));   
	root1->c='a';   
	root1->leftchild=lch1;   
	root1->rightchild=rch1;   
	lch1->c='b';   
	lch1->leftchild=NULL;   
	lch1->rightchild=NULL;   
	rch1->c='c';   
	rch1->leftchild=NULL;   
	rch1->rightchild=NULL;   
    
	//手动建立root2,三个节点,a b c   
	BinTree root2 =(BinTNode*)malloc(sizeof(BinTNode));   
	BinTNode *lch2 =(BinTNode*)malloc(sizeof(BinTNode));   
	BinTNode *rch2 =(BinTNode*)malloc(sizeof(BinTNode));   
	root2->c='a';   
	root2->leftchild=lch2;   
	root2->rightchild=rch2;   
	lch2->c='b';   
	lch2->leftchild=NULL;   
	lch2->rightchild=NULL;   
	rch2->c='c';   
	rch2->leftchild=NULL;   
	rch2->rightchild=NULL;   
    
	cout<<CompTree(root1, root2)<<endl;
}






//--------------------------------------------xfz

int Index(char S[],int Slen,char T[],int Tlen,int pos)
{
int i=pos;
int j=0;
while(i<=Slen && j<=Tlen)
{
if(S[i]==T[j])
{
++i;
++j;
}
else
{
i=i-j+2;
j=1;
}
}
if(j>Tlen) 
return i-Tlen;
else 
return 0;
}
 
void main()
{
char ss[]="helloworld";
char tt[]="wor";
int slen=sizeof(ss);
int tlen=sizeof(tt);
cout<<Index(ss,slen,tt,tlen,1);
}




//-----------------------------xfz

typedef struct node { //二叉树的链式存储
char c; 
struct node *leftchild, *rightchild; 
} BinTNode,*BinTree;
 
int CompTree(BinTree tree1, BinTree tree2)
{//判断两棵二叉树是否相等,用递归算法
BinTree RootA=tree1; 
BinTree RootB=tree2; 
while(RootA && RootB)
{
if(RootA->c==RootB->c)
{
RootA=RootA->leftchild;
RootB=RootB->leftchild;
CompTree(RootA, RootB);
RootA=RootA->rightchild;
RootB=RootB->rightchild;
CompTree(RootA, RootB);
}
else
return -1;
}
if (RootA!=NULL || RootB!=NULL) 
return -1;
return 0;
}
void main(void)
{
手动建立root1,三个节点,a b c
BinTree root1 =(BinTNode*)malloc(sizeof(BinTNode));
BinTNode *lch1 =(BinTNode*)malloc(sizeof(BinTNode));
BinTNode *rch1 =(BinTNode*)malloc(sizeof(BinTNode));
root1->c='a';
root1->leftchild=lch1;
root1->rightchild=rch1;
lch1->c='b';
lch1->leftchild=NULL;
lch1->rightchild=NULL;
rch1->c='c';
rch1->leftchild=NULL;
rch1->rightchild=NULL;
 
手动建立root2,三个节点,a b c
BinTree root2 =(BinTNode*)malloc(sizeof(BinTNode));
BinTNode *lch2 =(BinTNode*)malloc(sizeof(BinTNode));
BinTNode *rch2 =(BinTNode*)malloc(sizeof(BinTNode));
root2->c='a';
root2->leftchild=lch2;
root2->rightchild=rch2;
lch2->c='b';
lch2->leftchild=NULL;
lch2->rightchild=NULL;
rch2->c='c';
rch2->leftchild=NULL;
rch2->rightchild=NULL;
 
cout<<CompTree(root1, root2);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: