• <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) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            My Links

            Blog Stats

            News

            常用鏈接

            留言簿(1)

            隨筆分類(13)

            隨筆檔案(17)

            文章檔案(1)

            相冊(cè)

            收藏夾(1)

            Friends' blog

            useful sites

            搜索

            積分與排名

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            精品国产一区二区三区久久蜜臀| 精品久久久久久国产免费了| 午夜精品久久影院蜜桃| 久久人与动人物a级毛片| 久久综合色老色| 久久er热视频在这里精品| 久久电影网一区| 亚洲午夜久久久久久噜噜噜| 久久久久亚洲AV无码麻豆| 亚洲狠狠综合久久| 久久精品国产日本波多野结衣| 99久久国产综合精品麻豆| 久久天天躁狠狠躁夜夜2020| 久久久久久国产精品免费无码| 亚洲嫩草影院久久精品| 精品久久久无码人妻中文字幕| 99久久国产亚洲高清观看2024| 久久久久亚洲AV无码专区首JN | 品成人欧美大片久久国产欧美...| 久久99久久99精品免视看动漫| 国产91久久精品一区二区| 免费精品国产日韩热久久| 99国产精品久久| 99蜜桃臀久久久欧美精品网站| 国产精品热久久毛片| 久久久久久久久久久久中文字幕| 久久亚洲国产精品成人AV秋霞 | 97香蕉久久夜色精品国产| 久久无码一区二区三区少妇| 久久青青草原综合伊人| 久久精品无码专区免费青青| 7777精品久久久大香线蕉 | 久久se精品一区二区影院 | 久久精品国产亚洲AV电影 | 国产成人精品久久免费动漫 | 9久久9久久精品| 日韩乱码人妻无码中文字幕久久| 久久久高清免费视频| 一本大道久久香蕉成人网| 久久亚洲精品国产精品婷婷| 亚洲综合熟女久久久30p|