• <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)  編輯 收藏 引用


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


            My Links

            Blog Stats

            News

            常用鏈接

            留言簿(1)

            隨筆分類(13)

            隨筆檔案(17)

            文章檔案(1)

            相冊

            收藏夾(1)

            Friends' blog

            useful sites

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            久久福利青草精品资源站| 久久se精品一区二区| 亚洲国产精品久久久天堂| 久久久久国产精品熟女影院| 一本一道久久精品综合| 狠狠精品干练久久久无码中文字幕 | www.久久热| 久久本道久久综合伊人| 狠狠精品久久久无码中文字幕| 久久精品国产69国产精品亚洲| 久久久久亚洲av成人无码电影| 久久夜色精品国产网站| 欧美性大战久久久久久| 国产精品青草久久久久婷婷| 久久香综合精品久久伊人| 久久香蕉国产线看观看99| 日韩久久久久久中文人妻 | 久久久久久久久久久久久久| 国内精品伊人久久久久| 久久亚洲精品国产精品| 精品久久久久久无码不卡| segui久久国产精品| 国产精品久久国产精品99盘| 国产成人精品久久| 久久久久久综合网天天| 污污内射久久一区二区欧美日韩| 九九热久久免费视频| 亚洲国产精品婷婷久久| av无码久久久久不卡免费网站| 中文字幕热久久久久久久| 波多野结衣久久精品| 无码人妻久久一区二区三区蜜桃| 久久久久亚洲精品无码网址| 精品国产综合区久久久久久| 夜夜亚洲天天久久| 久久久WWW成人免费毛片| 久久久久亚洲精品男人的天堂| 人妻中文久久久久| 一本久久免费视频| 久久久久久久久久久久久久 | 久久天天躁狠狠躁夜夜2020老熟妇 |