• <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

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            色婷婷综合久久久久中文 | 人人妻久久人人澡人人爽人人精品| 国产精品免费看久久久| 66精品综合久久久久久久| 久久人人爽人人精品视频| 久久久久青草线蕉综合超碰| 人妻无码久久一区二区三区免费| 久久夜色精品国产亚洲| 亚洲&#228;v永久无码精品天堂久久 | 亚洲中文字幕无码久久综合网| 久久精品人人做人人爽电影蜜月| 国产午夜福利精品久久| 99精品国产99久久久久久97| 九九99精品久久久久久| 国内精品九九久久精品| 精品久久久久久无码人妻热 | 久久人人爽人人爽人人AV东京热| 国产精品九九久久免费视频| 亚洲国产欧美国产综合久久| 人人狠狠综合久久亚洲高清| 色偷偷888欧美精品久久久| 国产精品亚洲综合久久| 久久久久久久综合综合狠狠| 亚洲午夜久久影院| 99热成人精品热久久669| 亚洲欧美一区二区三区久久| 2021久久精品国产99国产精品| 亚洲精品乱码久久久久久按摩 | 国产99久久久久久免费看| 香蕉久久av一区二区三区| 亚洲日本久久久午夜精品| 亚洲国产小视频精品久久久三级| 国产精品永久久久久久久久久| 日韩一区二区久久久久久| 久久精品国产99国产精品澳门| 久久久久久无码Av成人影院| 国产婷婷成人久久Av免费高清| 麻豆AV一区二区三区久久| 人妻少妇久久中文字幕| 久久精品国产亚洲AV无码麻豆| 久久精品国产亚洲av影院|