Chapter 6 complete implementation of Binary Tree in essential C ++

Source: Internet
Author: User

I am going to study c ++ in depth. I am still very fond of reading C ++. I have made some incomplete items, but I am willing to give them a reference. I hope you can add some incomplete items!

 

 

# Include <iostream>

# Include <string>

 

Using namespace STD;

// Class btnode declare

Template <typename elemtype>
Class binarytree;

Template <typename valtype>
Class btnode {
Public:
Friend class binarytree <valtype>;
Btnode (const valtype & Val );
Void insert_value (const valtype & Val );
Void remove_value (const valtype & Val, btnode * & PREV );
Void preorder (btnode * PT, ostream & OS) const;
Void display_val (btnode * PT, ostream & OS) const;
Static void lchild_leaf (btnode * leaf, btnode * subtree );

PRIVATE:
Valtype _ Val;
Int _ CNT;
Btnode * _ lchild;
Btnode * _ rchild;
};

// Class binarytree declare

Template <typename elemtype>
Class binarytree {
Public:
Binarytree ();
Binarytree (const binarytree &);
~ Binarytree ();
Binarytree & operator = (const binarytree &);
Void insert (const elemtype & ELEM );
Bool empty ()
{
Return _ root = 0;
}
Void remove (const elemtype & ELEM );

Void preorder () const
{
_ Root-> preorder (_ root, cout );
}
Void remove_root ();

Void clear ()
{
If (_ root)
{
Clear (_ root );
_ Root = 0;
}
} // Ostream & OS = _ current_ OS

Friend ostream & operator <(ostream &, const binarytree <elemtype> );

PRIVATE:
Btnode <elemtype> * _ root;
Void copy (btnode <elemtype> * &, btnode <elemtype> *);
Void clear (btnode <elemtype> *); // static ostream * _ current_ OS;
};

// Btnode define

Template <typename valtype>
Inline btnode <valtype> ::
Btnode (const valtype & Val): _ Val (VAL)
{
_ CNT = 1;
_ Lchild = _ rchild = 0;
}

Template <typename valtype>
Void btnode <valtype> ::
Preorder (btnode * PT, ostream & OS) const
{
If (PT)
{
Display_val (PT, OS );
If (Pt-> _ lchild) preorder (Pt-> _ lchild, OS );
If (Pt-> _ rchild) preorder (Pt-> _ rchild, OS );
}
}
Template <typename valtype>
Void btnode <valtype> ::
Insert_value (const valtype & Val)
{
If (val = _ Val)
{
_ CNT ++;
Return;
}
If (Val <_ Val)
{
If (! _ Lchild)
_ Lchild = new btnode (VAL );
Else
_ Lchild-> insert_value (VAL );
}
Else
{
If (! _ Rchild)
_ Rchild = new btnode (VAL );
Else
_ Rchild-> insert_value (VAL );
}
}

Template <typename valtype>
Void btnode <valtype> ::
Remove_value (const valtype & Val, btnode * & PREV)
{
If (Val <_ Val)
{
If (! _ Lchild)
Return;

Else
_ Lchild-> remove_value (Val, _ lchild );
}
Else
If (Val> _ Val)
{
If (! _ Rchild)
Return;
Else
_ Rchild-> remove_value (Val, _ rchild );
}
Else
{
If (_ rchild)
{
Prev = _ rchild;
If (_ lchild)
If (! Prev-> _ lchild)
Prev-> _ lchild = _ lchild;
Else
Btnode <valtype >:: lchild_leaf (_ lchild, Prev-> _ lchild );
}
Else Prev = _ lchild;
Delete this;
}

}
Template <typename valtype>
Inline void btnode <valtype> ::
Display_val (btnode * PT, ostream & OS) const
{
OS <Pt-> _ Val;
If (Pt-> _ CNT> 1)
OS <"(" <Pt-> _ CNT <")";
Else OS <'';
}
Template <typename valtype>
Void btnode <valtype> ::
Lchild_leaf (btnode * leaf, btnode * subtree)
{
While (subtree-> _ lchild)
Subtree = subtree-> _ lchild;
Subtree-> _ lchild = leaf;
}

// Binarytree define
Template <typename elemtype>
Inline binarytree <elemtype >:: binarytree (): _ root (0)
{}

Template <typename elemtype>
Inline binarytree <elemtype>: binarytree (const binarytree & RHs)
{
Copy (_ root, RHS. _ root );
}

Template <typename elemtype>
Inline binarytree <elemtype> ::~ Binarytree ()
{
Clear ();
}

Template <typename elemtype>
Inline binarytree <elemtype> &
Binarytree <elemtype> ::
Operator = (const binarytree & RHs)
{
If (this! = & RHs)
 
 
{
Clear ();
Copy (_ root, RHS. _ root );
}

Return * this;
}

Template <typename elemtype>
Inline void binarytree <elemtype> ::
Insert (const elemtype & ELEM)
{
If (! _ Root)
_ Root = new btnode <elemtype> (ELEM );
Else
_ Root-> insert_value (ELEM );
}
 
Template <typename elemtype>
Void binarytree <elemtype> ::
Remove_root ()
{
If (! _ Root)
Return;
Btnode <elemtype> * TMP = _ root;
If (_ root-> _ rchild)
{

_ Root = _ root-> _ rchild;

If (TMP-> _ rchild)
{

Btnode <elemtype> * lc = TMP-> _ lchild;
Btnode <elemtype> * newlc = _ root-> _ lchild;
If (! Newlc)
_ Root-> _ lchild = Lc;
Else
Btnode <elemtype>: lchild_leaf (LC, newlc );
}
}
Else
_ Root = _ root-> _ lchild;
Delete TMP;
}

Template <typename elemtype>
Void binarytree <elemtype> ::
Remove (const elemtype & ELEM)
{
If (_ root)
{
If (_ root-> _ Val = ELEM)
Remove_root ();
Else
_ Root-> remove_value (ELEM, _ root );
}
}


Template <typename elemtype>
Void binarytree <elemtype> ::
Clear (btnode <elemtype> * PT)
{
If (PT)
{
Clear (Pt-> _ lchild );
Clear (Pt-> _ rchild );
Delete pt;
}
}
Template <typename elemtype> // binarytree <elemtype> ::
Ostream & operator <(ostream & OS, const binarytree <elemtype> & BT)
{
OS <"Tree:" <Endl;
Bt. Print (OS );
 
Return OS;
}

Template <typename elemtype>
Void binarytree <elemtype> ::
Copy (btnode <elemtype> * & tar, btnode <elemtype> * SRC)
{
If (SRC)
{//()
Tar = new btnode <elemtype> (SRC-> _ Val );
If (SRC-> _ lchild) Copy (tar-> _ lchild, Src-> _ lchild );
If (SRC-> _ rchild) Copy (tar-> _ rchild, Src-> _ rchild );
}
}

Int main ()
{
Binarytree <string> Bt;

Bt. insert ("Piglet ");
Bt. insert ("Eeyore ");
Bt. insert ("roo ");
Bt. insert ("Tigger ");
Bt. insert ("Chris ");
Bt. insert ("Pooh ");
Bt. insert ("Kanga ");

Cout <"preorder traversal:/N ";

Bt. preorder ();

Bt. Remove ("Piglet ");
Cout <"/n preorder traversal after Piglet removal:/N ";
Bt. preorder ();

Bt. Remove ("Eeyore ");
Cout <"/n preorder traversal after Eeyore removal:/N ";
Bt. preorder ();

Return 0;
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.