Binary search tree Implementation-doubly linked list

Source: Internet
Author: User

Binary lookup tree, for ease of implementation, adds a pointer to the parent node for each node
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace Std;

Template<class t>
Class Binarysearchtree
{
Private
struct Node
{
T data;
int deep;
Node *left;
Node *right;
Node *prev;
Node (T val,int deep)
{
data = Val;
Deep = 0;
left = NULL;
right = NULL;
prev = NULL;
}

Private
Node ()
{
}
};
Node *root;
int size;

Public
Binarysearchtree ()
{
root = NULL;
size = 0;
}
~binarysearchtree ()
{
Clear (Root);
root = NULL;
size = 0;
}
T min (Node *node) const
{
if (Node->left = = NULL)
Return node->data;
Else
return min (node->left);
}
T Max (Node *node) const
{
if (node->right = = NULL)
Return node->data;
Else
Return Max (node->right);
}

Node *insert (node *& node,t val)
{
if (size = = 0 && node = = NULL)
{
root = new Node (val,0);
Root->prev = NULL;
size++;
return root;
}
if (size! = 0 && node = = NULL)
{
cout<< "error\n";
return NULL;
}
if (val > Node->data)
{
if (node->right! = NULL)
return insert (Node->right,val);
Else
{
Node *tmp = new node (val,node->deep+1);
Tmp->prev = node;
Node->right = tmp;
size++;
return TMP;
}
}
else if (Val < node->data)
{
if (node->left! = NULL)
return insert (Node->left,val);
Else
{
Node *tmp = new node (val,node->deep+1);
Tmp->prev = node;
Node->left = tmp;
Size + +;
return TMP;
}
}
else if (val = = Node->data)
{
}
}

BOOL Contain (Node *node,t val) const
{
if (node = = NULL)
return false;

if (val > Node->data)
return contain (node->right,val);
else if (Val < node->data)
return contain (node->left,val);
Else
return true;
}
void RemoveNode (Node *node)
{
if (Node->left = = NULL && Node->right = = null)
{
if (Node->prev->left = = node)
Node->prev->left = NULL;
Else
Node->prev->right = NULL;

Delete node;
size--;
}
else if (Node->left = = NULL)
{
Node->right->prev = node->prev;
if (Node->prev->left = = node)
Node->prev->left = node->right;
Else
Node->prev->right = node->right;

Decdeep (Node->right);
Delete node;
size--;
}
else if (node->right = = NULL)
{
Node->left->prev = node->prev;
if (Node->prev->left = = node)
Node->prev->left = node->left;
Else
Node->prev->right = node->left;

Decdeep (Node->left);
Delete node;
size--;
}
Else
{
Node *p = node->right;
while (p->left! = NULL)
{
p=p->left;
}
Node->data = p->data;
if (p->right! = NULL)
{
P->prev->left = p->right;
P->right->prev = p->prev;
Decdeep (P->right);
Delete p;
size--;
}
Else
{
P->prev->left = NULL;
Delete p;
size--;
}
}
}
void Decdeep (Node *node)
{
node->deep--;
if (node->left! = NULL)
Decdeep (Node->left);
if (node->right! = NULL)
Decdeep (Node->right);
}
void Remove (T val)
{
Node * P=ROOT;
while (1)
{
if (val > P->data)
p = p->right;
else if (Val < p->data)
p = p->left;
else if (val = = P->data)
{

RemoveNode (P);
Return
}
}
}
void Clear (Node*node)
{
if (node->left! = NULL)
Clear (Node->left);
if (node->right! = NULL)
Clear (Node->right);

Delete node;
node = NULL;
}
void print (Node *node)
{
if (node = = NULL)
Return
cout<<node->data<< "";
if (node->left! = NULL)
Print (node->left);
if (node->right! = NULL)
Print (node->right);
}
void Insert (T val)
{
Insert (Root,val);
}
void print ()
{
print (root);
cout<< "\ n";
}
};

int main ()
{
Binarysearchtree<int> Tree;
Tree.insert (10);
Tree.insert (1);
Tree.insert (11);
Tree.insert (9);
Tree.insert (8);
Tree.print ();
cout<< "\ n";
Tree.remove (9);
Tree.print ();

return 0;
}

Binary search tree Implementation-doubly linked list

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.