• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            linux&c++ R&D

            programing is a pleasure!

            A simple application example of binary tree structure

            Binary Tree is  widely  employed in many cases as a very important data structrue.
            I will take a simple example to introduce it.
            Suppose we want to handle the more general problem of counting the occurrences of all the
            words in some input.Since the list of words isn't known in advance,we can't conveniently sort it and use a binary search.Yet we can't do a linear search for each word as it arrives,to see if it's already been seen;the program would take a long time.
            a binary tree will help us to solve the problem.
            terminal:
            First we define the node structure,which is used to store the infomation of each word.
            it consists of word name,count,two pointer,which point to left subtree and right substree.
             secondly,tree structure is a binary sorted tree.
            each word has a unique node in the tree.
            the detailed code implemention below:

            // wordTree.h: interface for the wordTree class.
            //
            //////////////////////////////////////////////////////////////////////
            #ifndef        __WORDTREE_
            #define        __WORDTREE_

            class wordTree;


            class wordNode{
            friend 
            class wordTree;
            private:
               
            char *word;
               
            int count;
               wordNode 
            *left,*right;
            private:
               wordNode(
            const char* w,wordNode *left=0,wordNode *right=0,int count=1);
               
            ~wordNode();
                inline 
            void incrcount(){
                    count
            ++;
                }

            }
            ;
            class wordTree  
            {
             
            public:
                wordTree():root(
            0){
                    
                }

                virtual 
            ~wordTree(){
                    freetree(root);
                }

                
            void addWord(const char *w);
                
            void printTree();
            private:
                wordNode
            * addWord(wordNode *p,const char *w);
                
            void printTree(wordNode *p);
                
            void freetree(wordNode *p);
                wordNode
            * root;
            }
            ;

            #endif 
            // end __WORDTREE_

             

            // wordTree.cpp: implementation of the wordTree class.
            //
            //////////////////////////////////////////////////////////////////////

            #include 
            "wordTree.h"
            #include 
            <string.h>
            #include 
            <iostream>


            wordNode::wordNode(
            const char* w,wordNode *left/* =0 */,wordNode *right/* =0 */,int count/* =0 */)
            {
              
            int len=strlen(w);
              word
            =new char[len+1];
              strcpy(word,w);
              
              
            this->left=left;
              
            this->right=right;
              
            this->count=count;

            }

            wordNode::
            ~wordNode()
            {
                
            if(word!=0)
                    delete [] word;
            }




            void wordTree::addWord(const char *w){

                root
            =addWord(root,w);

            }

            wordNode
            * wordTree::addWord(wordNode *p,const char *w)
            {
             
            int cond;
             
            if(p==0)
                 p
            =new wordNode(w);
              
            else if((cond=strcmp(w,p->word))==0)
                  p
            ->incrcount();
             
              
            else if (cond<0)
                  p
            ->left=addWord(p->left,w);
              
            else
                  p
            ->right=addWord(p->right,w);
              
            return p;
            }

            void wordTree::printTree()
            {
              printTree(root);
             }

            void wordTree::printTree(wordNode *p)
            {
             
            if (p==0)
                 
            return;
             printTree(p
            ->left);
             std::cout
            <<p->word<<"  count:  "<<p->count<<std::endl;
             printTree(p
            ->right);
            }

            void wordTree::freetree(wordNode *p)
            {
              
            if(p==0)
                  
            return;
              freetree(p
            ->left);
              freetree(p
            ->right);
              delete p;

            }

             

            //test.cpp
            //test the example

            #include 
            "wordTree.h"
            #include 
            <iostream>
            #include 
            <string>
            int main()
            {
             wordTree wt;
             std::string str;
             
            while (std::cin>>str)
             
            {
               wt.addWord(str.c_str());
               wt.printTree();
             }


             

            }

                   

            posted on 2007-05-17 13:17 丑石 閱讀(237) 評論(0)  編輯 收藏 引用

            My Links

            Blog Stats

            News

            常用鏈接

            留言簿(1)

            隨筆分類(13)

            隨筆檔案(17)

            文章檔案(1)

            相冊

            收藏夾(1)

            Friends' blog

            useful sites

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            久久国产精品波多野结衣AV| 国产精品久久99| 久久久久亚洲爆乳少妇无| 色综合久久88色综合天天 | 亚洲?V乱码久久精品蜜桃 | 久久久精品人妻无码专区不卡| 国产精品女同一区二区久久| 亚洲国产综合久久天堂| 久久国产精品77777| 无码人妻少妇久久中文字幕 | 国产精品gz久久久| 亚洲国产小视频精品久久久三级 | 亚洲国产成人久久综合碰碰动漫3d| 国产成人综合久久久久久| 中文字幕无码免费久久| 久久99热这里只有精品国产| 午夜久久久久久禁播电影| 人妻丰满?V无码久久不卡| 国产精品无码久久久久久| 亚洲乱码日产精品a级毛片久久| 久久99国产综合精品| 久久精品国产99国产精品亚洲| 亚洲午夜久久影院| 国产精品久久久久久吹潮| 久久人人爽人人爽人人av东京热| 日本道色综合久久影院| 成人妇女免费播放久久久| 亚洲中文久久精品无码ww16| 色狠狠久久综合网| 久久天天婷婷五月俺也去| 久久综合一区二区无码| 伊人久久精品线影院| 欧美一区二区精品久久| 久久精品免费观看| 国产99久久久久久免费看| 国产成人精品久久免费动漫| 99久久国产综合精品麻豆| 久久综合综合久久97色| 久久国产精品99精品国产987| 国产精品一区二区久久| 亚洲一本综合久久|