Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TreeNode{
- public:
- char data;
- TreeNode* Left, Right;
- int count;
- TreeNode(){
- Left=Right=NULL;
- }
- }
- void PrintcountChar(TreeNode* &root){ //gia su duyet NLR, duyet sao tuy ong.
- if(root==NULL) return;
- cout<<"count:"<<root->data<<" :"<<root->count;
- PrintcountChar(root->Left);
- PrintcountChar(root->Right);
- }
- void AddTreeNode(TreeNode* &root, char data){
- if(root==NULL){
- root=new TreeNode; root->data=data;
- root->count=1;
- return;
- }
- else if(data==root->data){
- root->count+=1;
- return;
- }
- else if(data > root->data) AddTreeNode(root->Right, data);
- else if(data < root->data) AddTreeNode(root->Left, data);
- }
- void main(){
- char* str;
- cout<<"input :"; cin>>str;
- TreeNode* root=new TreeNode;
- //while hay for gi do, cho no doc het tu ki tu 1 cua str
- AddTreeNode(root, str[i]);
- //duyet qua het cay, roi xuat ra so lan xuat hien cua ki tu
- //vd duyet LNR
- PrintcountChar(root);
- //con cau 1.b, thi ong viet ham search(TreeNode*&root, char target) search
- //tim dc con tro pResult roi xuat ra pResult->count
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement