注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

分享,态度 ·~~

—— 十年太长,五年;如果可以回到五年前,你最想对那时候的自己说什么?

 
 
 

日志

 
 

字符串快速查找(C版,调试版)  

2010-01-19 11:27:34|  分类: C/C++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

#define fArrayListNum 26
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>


struct  StructBinaryNode
{
    int fChildNum;
    char fData;
    struct  StructBinaryNode* fChildPointer[fArrayListNum];

};

typedef struct StructBinaryNode BinaryNode;

struct fBinaryTree
{
 
  BinaryNode DataArray [150];
  int DataArrayCount ;
  BinaryNode TempNode;
  BinaryNode fHeadNode;
  char InputString[80];
  int Size;

};
typedef struct fBinaryTree BinaryTree;

void InitialChildPointer(BinaryNode* Node);


void  InitialBinaryNode(char Data,BinaryNode* Node)
{
   static int i =0;
    Node->fChildNum = 0;
    InitialChildPointer(Node);
    Node->fData = Data;
    printf(" BInaryNode.fChildNum :%d  \n",Node->fChildNum);
    getch();
}

void InitialChildPointer(BinaryNode* Node)
{
int i;
for (i = 0; i < fArrayListNum; i++)
    {
        Node->fChildPointer[i] = NULL;
    }
}


/***************************************
FuncName : GetChildBinaryNode
Description:
****************************************/
BinaryNode* GetChildBinaryNode(int index,BinaryNode* Node)
{
return Node->fChildPointer[index];
}

/************************************************
Insert Data at the right address
*************************************************/
void InsertAt(char Info, int index,BinaryNode* Node)
{
    if( index >=0 && index < fArrayListNum)
    {
        BinaryNode TempNode;
        InitialBinaryNode(Info,&TempNode);
        printf("InsertAt  TempNode.fData :%c  And fChildNum  : %d\n",TempNode.fData,TempNode.fChildNum);
        Node->fChildPointer[index] = &TempNode;

    }
}

void AddBinaryNodePointer(BinaryNode* fNode,BinaryNode* ParentNode)
{
   ParentNode->fChildPointer[ParentNode->fChildNum++] = fNode;
}

void RemoveAt(int index,BinaryNode* ParentNode)
 {
      ParentNode->fChildPointer[index] = NULL;
 }

void (*VisitFunc)();

void InitialBinaryTree(BinaryTree* Tree)
{
  int i;
  InitialBinaryNode('',&Tree->TempNode);
  Tree->Size =150;
  for ( i= 0; i < Tree->Size; i++)
  {
   InitialBinaryNode('',&Tree->DataArray[i]);
   }
}

void BinaryNodeCopy(BinaryNode* Input,BinaryNode* Output)
    {
          int i ;
          Output->fChildNum = Input->fChildNum;
          Output->fData     = Input->fData;
          for(i=0;i<fArrayListNum;i++)
          {
            Output->fChildPointer[i] = Input->fChildPointer[i];
          }
          printf("BinaryNodeCopy Input->fChildNum : %d  \n", Input->fChildNum);
          printf("BinaryNodeCopy Input->fData  : %c  \n\n\nEnd \n",Input->fData);

   }

void InsertStringByChar(char* InsertString,BinaryTree* Tree)
{
int Index;
int i;
int count = strlen(InsertString);
char DataArray[50];
BinaryNode ParentNode;

strcpy(DataArray,InsertString);


for ( i= 0; i < count; i++)
    {
        Index = GetIndexFromChar(DataArray[i]);
        if(ChildSearch(&ParentNode,DataArray[i],Index) == 1)
        {
        ParentNode.fChildNum++;
        printf("InsertStringByChar  Null Info  ");
        printf("InsertStringByChar  ParentNode.fChildNum :%d\n\n",ParentNode.fChildNum);

        BinaryNodeCopy(ParentNode.fChildPointer[i],&ParentNode);
        if(i == 0)
        {
        BinaryNodeCopy(&ParentNode,&Tree->fHeadNode);
        Tree->fHeadNode.fChildNum++;
        printf("InsertStringByChar  Tree->fHeadNode.fData  :   %c  and i==1 \n",Tree->fHeadNode.fData);
        }
        getch();
        }
        else
        {
        ParentNode.fChildNum++;
        InsertAt(DataArray[i],i,&ParentNode);
        printf("InsertStringByChar  DataArray[%d]  :%c  \n ",i,DataArray[i]);
        printf("InsertStringByChar  Have Info \n");
         printf("InsertStringByChar  ParentNode.fChildNum :%d \n\n",ParentNode.fChildNum);
         BinaryNodeCopy(ParentNode.fChildPointer[i],&ParentNode);
          printf("InsertStringByChar  ParentNode.fChildNum :%d \n\n",ParentNode.fChildNum);
         if(i == 0)
        {
        BinaryNodeCopy(&ParentNode,&Tree->fHeadNode);
        Tree->fHeadNode.fChildNum++;
        printf("InsertStringByChar  Tree->fHeadNode.fData  :   %c  and i==1 \n",Tree->fHeadNode.fData);
        }
         getch();
        }
    }


}

int SearchStringByChar(char* SearchString,BinaryTree* Tree)
{
int Index;
int i;
int flag =0;
char DataArray[50];
int count = strlen(SearchString);
BinaryNode ParentNode;
BinaryNodeCopy(&Tree->fHeadNode,&ParentNode);
strcpy(DataArray,SearchString);
printf("fHeadNode.fData  :  %c \n",Tree->fHeadNode.fData);
printf("fHeadNode.fChildNum  :  %d  \n",Tree ->fHeadNode.fChildNum);
printf("ParentNode.fData  : %c  \n", ParentNode.fData);
getch();
for ( i = 0; i <count ; i++)
    {
        Index = GetIndexFromChar(DataArray[i]);
        if (ChildSearch(&ParentNode,DataArray[i],Index) == 1)
        {
            ParentNode.fChildNum++;
             printf("ParentNode.fData(Before GetChildNode) :  %c  \n",ParentNode.fData);
            BinaryNodeCopy(ParentNode.fChildPointer[i],&ParentNode);
            printf("ParentNode.fData(After GetChildNode) :  %c  \n",ParentNode.fData);
            getch();
            flag = 1;
        }
        else
        {
            flag = 0;
            printf("DataArray[ %d ]  : %c  is not found \n",i,DataArray[i]);
            getch();
         }
    }

return flag;
           
}

 

 

         int ChildSearch(BinaryNode* ParentNode,char Info,int index)
        {
            BinaryNode TempNode;
            BinaryNodeCopy(ParentNode->fChildPointer[index],&TempNode);

            /*printf("ChildSearch  ParentNode.fChildPointer[index].fData  :%c  ",ParentNode->fData); */
            printf("\nChildSearch  TempNode.fData  :%c  \n",TempNode.fData);
            if ( TempNode.fData != Info)
                {
                printf("ChildSearch  :Return 0  \n");
                return 0;
                }
             else
                {
                printf("ChildSearch  :Return 1  \n");
                return 1;
                }
           
        }

         int GetIndexFromChar(char index)
        {
            if (index > 64 && index < 91)
                return index - 65;
            else if (index > 96 && index < 123)
                return index - 97;
            return -1;
        }

        int main()
        {
          BinaryTree objTree;
          InitialBinaryTree(&objTree);
          InsertStringByChar("TestNation",&objTree);
          printf("%d",SearchStringByChar("Test",&objTree));
          getch();
          return 0;
        }

 【from 林海听松 http://hi.baidu.com/xianchao/blog/item/2e0019d73b8afddda144df86.html

  评论这张
 
阅读(1355)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017