Anda di halaman 1dari 3

Thuat toan cay nhi phan

#include "iostream.h"
#include "conio.h"
enum ErrorCode {success,fail,not_present,duplicate_error,underflow,
overflow, range_error};

struct BinaryNode
{
        int data;
        BinaryNode *left;
        BinaryNode *right;
        BinaryNode();
        BinaryNode(const int &x);
};

BinaryNode::BinaryNode()
{
        left=right=NULL;
}

BinaryNode::BinaryNode(const int &x)


{
        data=x;
        left=right=NULL;
}

class SearchTree
{
public:
        SearchTree();
        void inorder(void (*visit)(int &));
        ErrorCode insert(const int &new_data);
        ErrorCode remove(const int &old_data);
protected:
        void recursive_inorder(BinaryNode *sub_root,void (*visit)(int
&),int i);
        ErrorCode search_and_insert(BinaryNode *&sub_root,const int
&new_data);
        ErrorCode remove_root(BinaryNode *&sub_root);
        ErrorCode search_and_destroy(BinaryNode *&sub_root,const int
&target);
        BinaryNode *root;
};      
       
SearchTree::SearchTree()
{
        root=NULL;
}

void SearchTree::inorder(void (*visit)(int &))


{
        recursive_inorder(root,visit,1);
}

void SearchTree::recursive_inorder(BinaryNode *sub_root,void (*visit)


(int &),int i)
{
   if (sub_root != NULL)
   {
          recursive_inorder(sub_root->left, visit, i+1);
          for(int j=0; j<i; j++)
          cout<<"-";
          (*visit)(sub_root->data);
          recursive_inorder(sub_root->right, visit, i+1);
   }
}

ErrorCode SearchTree::insert(const int &new_data)


{
        return search_and_insert(root,new_data);
}

ErrorCode SearchTree::search_and_insert(BinaryNode *&sub_root,const


int &new_data)
{
   if (sub_root == NULL)
   {
          sub_root = new BinaryNode (new_data);
          return success;
   }
   else if (new_data < sub_root->data)
          return search_and_insert(sub_root->left, new_data);
   else if (new_data > sub_root->data)
          return search_and_insert(sub_root->right, new_data);
   else return duplicate_error;
}

ErrorCode SearchTree::remove_root(BinaryNode *&sub_root)


{
        if (sub_root==NULL) return not_present;
        BinaryNode *to_delete=sub_root;
        if (sub_root->right==NULL)
                sub_root=sub_root->left;
        else if (sub_root->left==NULL)
                sub_root=sub_root->right;
        else
        {
                to_delete=sub_root->left;
                BinaryNode *parent=sub_root;
                while (to_delete->right!=NULL)
                {
                        parent=to_delete;
                        to_delete=to_delete->right;
                }
                sub_root->data=to_delete->data;
                if (parent==sub_root)
                        sub_root->left=to_delete->left;
                else
                        parent->right=to_delete->left;
        }
        delete to_delete;
        return success;
}

ErrorCode SearchTree::remove(const int &old_data)


{
        return search_and_destroy(root,old_data);
}

ErrorCode SearchTree::search_and_destroy(BinaryNode *&sub_root,const


int &target)
{
        if (sub_root==NULL || sub_root->data==target)
                return remove_root(sub_root);
        else if (target<sub_root->data)
                return search_and_destroy(sub_root->left,target);
        else
                return search_and_destroy(sub_root->right,target);
}

void print_int(int &x)


{
        cout << x << "\n";
}
void main()
{
        int n, data;
        SearchTree Search_tree;
        cout<<"Moi ban nhap vao so node: ";
        cin>>n;
       
        for(int i=0; i<n; i++)
        {
                cin>>data;
                Search_tree.insert(data);
        }
        Search_tree.inorder(print_int);
}

Anda mungkin juga menyukai