class Node
{
public:
int data;
Node *parent;
Node *left;
Node *right;
public:
Node() : data(-1), parent(NULL),left(NULL),right(NULL){};
Node(int num) :data(num),parent(NULL),left(NULL),right(){};
};
class Tree
{
public:
Tree(int num[],int len);
void insertNode1(int data);
void insertNode(int data);
Node *searchNode(int data);
void deldteNode(int data);
private:
void insertNode(Node* current,int data);
Node *searchNode(Node* current,int data);
void deleteNode(Node* current);
private:
Node* root;
};
void Tree::insertNode(int data)
{
Node *p,*par;
Node *newNode = new Node(data);
p=par=root;
while(p!=NULL)
{
par =p;
if(data>p->data)
p=p->right;
else if(data
data)
p=p->left;
else if(data==p->data)
{
delete newNode;
return;
}
}
newNode->parent =par;
if(par->data>newNode->data)
par->left = newNode;
else
par->right =newNode;
}
阅读(1244) | 评论(0) | 转发(0) |