• <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 丑石 閱讀(240) 評論(0)  編輯 收藏 引用

            My Links

            Blog Stats

            News

            常用鏈接

            留言簿(1)

            隨筆分類(13)

            隨筆檔案(17)

            文章檔案(1)

            相冊

            收藏夾(1)

            Friends' blog

            useful sites

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲国产精品无码久久九九| 久久久久国产精品人妻| 久久国产乱子伦精品免费强| 国产精品18久久久久久vr| 色综合久久精品中文字幕首页| 国产69精品久久久久9999| 久久久无码精品亚洲日韩蜜臀浪潮| 久久精品国产亚洲AV无码偷窥| 婷婷久久综合九色综合98| 色青青草原桃花久久综合| 国产亚洲精品自在久久| 亚洲一级Av无码毛片久久精品| 久久久这里有精品| 99久久精品费精品国产| 亚洲va久久久噜噜噜久久天堂| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 久久亚洲AV永久无码精品| 亚洲伊人久久大香线蕉综合图片| 成人综合伊人五月婷久久| 色偷偷91久久综合噜噜噜噜| 久久国产一区二区| 无码精品久久久天天影视| 欧美精品福利视频一区二区三区久久久精品| 亚洲AV成人无码久久精品老人 | 亚洲欧洲日产国码无码久久99| 夜夜亚洲天天久久| 91精品国产乱码久久久久久| 亚洲伊人久久精品影院| 亚洲伊人久久成综合人影院 | 亚洲va中文字幕无码久久| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久精品国产亚洲AV香蕉| 久久综合亚洲色HEZYO国产| yellow中文字幕久久网| 久久综合九色综合精品| 久久精品中文无码资源站| 色婷婷综合久久久中文字幕| 亚洲人成伊人成综合网久久久| 久久午夜夜伦鲁鲁片免费无码影视| 日韩精品久久久久久久电影| 思思久久99热只有频精品66|