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.
// wordTree.h: interface for the wordTree class.
 //
//
 //////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
 #ifndef        __WORDTREE_
#ifndef        __WORDTREE_
 #define        __WORDTREE_
#define        __WORDTREE_

 class wordTree;
class wordTree;



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

 inline void incrcount()
    inline void incrcount() {
{
 count++;
        count++;
 }
    }
 };
};
 class wordTree
class wordTree  


 {
{
 
 
 public:
public:

 wordTree():root(0)
    wordTree():root(0) {
{
 
        
 }
    }

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

 #endif // end __WORDTREE_
#endif // end __WORDTREE_

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

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



 wordNode::wordNode(const char* w,wordNode *left/**//* =0 */,wordNode *right/**//* =0 */,int count/**//* =0 */)
wordNode::wordNode(const char* w,wordNode *left/**//* =0 */,wordNode *right/**//* =0 */,int count/**//* =0 */)


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

 }
}
 wordNode::~wordNode()
wordNode::~wordNode()


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




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

 root=addWord(root,w);
    root=addWord(root,w);

 }
}
 wordNode* wordTree::addWord(wordNode *p,const char *w)
wordNode* wordTree::addWord(wordNode *p,const char *w)


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


 {
{
 printTree(root);
  printTree(root);
 }
 }
 void wordTree::printTree(wordNode *p)
void wordTree::printTree(wordNode *p)


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


 {
{
 if(p==0)
  if(p==0)
 return;
      return;
 freetree(p->left);
  freetree(p->left);
 freetree(p->right);
  freetree(p->right);
 delete p;
  delete p;

 }
} 
 //test.cpp
//test.cpp
 //test the example
//test the example

 #include "wordTree.h"
#include "wordTree.h"
 #include <iostream>
#include <iostream>
 #include <string>
#include <string>
 int main()
int main()


 {
{
 wordTree wt;
 wordTree wt;
 std::string str;
 std::string str;
 while (std::cin>>str)
 while (std::cin>>str)

 
  {
{
 wt.addWord(str.c_str());
   wt.addWord(str.c_str());
 wt.printTree();
   wt.printTree();
 }
 }

 
 

 }
}
