C + + data structure-------binary tree to realize __ garbled problem

Source: Internet
Author: User

Recently wrote a simple two-fork tree, only for reference, the code is as follows:

#include <iostream>
using namespace Std;

typedef char T;

Class BST
{
struct Node
{
T data;
node* L;
node* R;
Node (const t& D):d Ata (d), L (), R () {}
Node (const t& d,node* l,node* R):d ATA (d), L (L), R (r) {}
};
typedef node* Tree;
node* RP;
int n;
Public
BST (): RP (), n () {}
~bst ()
{
Clear ();
}
void Clear ()
{
Clear (RP);
n = 0;
}
void Insert (const t& D)
{
Insert (Rp,new Node (d));
++n;
}
tree& Find (const t& D)
{
return find (RP,D);
}
void Travel () const
{
Travel (RP);
cout << Endl;
}
BOOL Empty () const
{
return RP = = NULL;
}
int size () const
{
return n;
}
BOOL Remove (const t& D)//delete node of specified value in tree
{
tree& t = find (d);
if (t = = NULL) return false;
node* p = t;
if (t->l!= NULL) Insert (T-&GT;R,T-&GT;L);
t = t->r;
Delete p;
--n;
return true;
}
Const t& root () const
{
if (RP = null) throw "null";
Return rp->data;
}
void Update (const t& olddata,const t& NewData)//Modify node data
{
if (remove (olddata))
Insert (NewData);
}
Private
static void Insert (tree& t,node* p)//Insert a node in the tree
{
if (t = = NULL)
t = P;
else if (P->data < T->data)
Insert (T-&GT;L,P);
Else
Insert (T-&GT;R,P);
}
Static tree& Find (tree& t,const t& D)//returns the root pointer of a subtree that has D as the root node
{
if (t = = NULL) return t; Didn't find
else if (t->data = = d) return t; Got it
else if (D < t->data)
return find (T-&GT;L,D);
Else
return find (T-&GT;R,D);
}
static void travel (tree T)//traversal
{
if (t!= NULL)
{
Travel (T-&GT;L);
cout << t->data << ';
Travel (T-&GT;R);
}
}
static void Clear (tree& t)//Empty
{
if (t!= NULL)
{
Clear (T->l);
Clear (T->r);
Delete T;
t = NULL;
}
}
static int high (tree T)
{
if (t = = NULL) return 0;
int lh = high (t->l);
int RH = high (T-&GT;R);
Return 1+ (LH&GT;RH?LH:RH);
}
};


int main ()
{
BST b;
B.insert (' K ');
B.insert (' s ');
B.insert (' f ');
B.insert (' t ');
B.insert (' a ');
B.insert (' m ');
B.insert (' x ');
B.insert (' e ');

B.travel ();

B.remove (' K ');
B.travel ();

B.update (' x ', ' * ');
B.travel ();

while (!b.empty ())
B.remove (B.root ());
cout << "Size:" << b.size () << Endl;

B.travel ();

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.