#include<iostream>
#include<vector>
#include<string>
#include<queue>
using namespace::std;
typedef struct Node
{
Node(int input):data(input),visit(0),lChild(NULL),rChild(NULL),parent(NULL){}
int data;
bool visit;
Node* lChild;
Node* rChild;
Node* parent;
};
class BinarySearchTree
{
public:
BinarySearchTree(Node* input)
{
root = input;
}
~BinarySearchTree()
{
destroy(root);
}
void preOrder(Node* root)
{
if(root == NULL)
return;
root->visit = 1;
cout<<root->data<<endl;
preOrder(root->lChild);
preOrder(root->rChild);
}
void inOrder(Node* root)
{
if(root == NULL)
return;
inOrder(root->lChild);
root->visit = 1;
cout<<root->data<<endl;
inOrder(root->rChild);
}
void postOrder(Node* root)
{
if(root == NULL)
return;
postOrder(root->lChild);
postOrder(root->rChild);
root->visit = 1;
cout<<root->data<<endl;
}
Node* search(Node* input, int value)
{
if(input == NULL)
{
cout<<"not found!"<<endl;
return NULL;
}
if(value < input->data)
{
return search(input->lChild, value);
}
if(value > input->data)
{
return search(input->rChild, value);
}
if(value == input->data)
{
return input;
}
}
void insert(Node* input, int value)
{
if(input == NULL)
{
Node* newNode = new Node(value);
input = newNode;
return;
}
if(value < input->data)
{
if(input->lChild == NULL)
{
Node* newNode = new Node(value);
input->lChild = newNode;
newNode->parent = input;
return;
}
else
insert(input->lChild, value);
}
if(value > input->data)
{
if(input->rChild == NULL)
{
Node* newNode = new Node(value);
input->rChild = newNode;
newNode->parent = input;
return;
}
insert(input->rChild, value);
}
}
void deleteNode(Node* input, int value)
{
Node* target = search(input, value);
if(target == NULL)
return;
if(target->lChild == NULL && target->rChild == NULL)
{
if(target->parent->lChild == target)
{
target->parent->lChild = NULL;
}
if(target->parent->rChild == target)
{
target->parent->rChild = NULL;
}
delete target;
return;
}
else if(target->lChild != NULL && target->rChild != NULL)
{
Node* tmp = target->rChild;
while(tmp->lChild != NULL)
{
tmp = tmp->lChild;
}
int tmp_value = tmp->data;
deleteNode(tmp, tmp->data);
target->data = tmp_value;
return;
}
else
{
Node* tmp;
if(target->lChild != NULL)
tmp = target->lChild;
else
tmp = target->rChild;
if(target->parent->lChild == target)
{
target->parent->lChild = tmp;
tmp->parent = target->parent;
}
if(target->parent->rChild == target)
{
target->parent->rChild = tmp;
tmp->parent = target->parent;
}
delete target;
return;
}
}
bool balance(Node* input)
{
if(getMaxDep(input) - getMinDep(input) > 1)
{
return false;
}
else
return true;
}
Node* getRoot()
{
return root;
}
void print(Node* input)
{
if(input == NULL)
{
return;
}
cout<<input->data<<endl;
print(input->lChild);
print(input->rChild);
}
private:
int getMaxDep(Node* input)
{
if(input == NULL)
{
return 0;
}
else
{
return (1 + max(getMaxDep(input->lChild), getMaxDep(input->rChild)));
}
}
int getMinDep(Node* input)
{
if(input == NULL)
{
return 0;
}
else
return (1 + min(getMinDep(input->lChild), getMinDep(input->rChild)));
}
void destroy(Node *input)
{
if(input == NULL)
{
return;
}
destroy(input->lChild);
destroy(input->rChild);
delete root;
}
Node * root;
};
BinarySearchTree* createTree()
{
Node* nodeA = new Node(100);
Node* nodeB = new Node(50);
Node* nodeC = new Node(150);
Node* nodeD = new Node(25);
Node* nodeE = new Node(75);
Node* nodeF = new Node(125);
Node* nodeG = new Node(175);
nodeA->lChild = nodeB;
nodeB->parent = nodeA;
nodeA->rChild = nodeC;
nodeC->parent = nodeA;
nodeB->lChild = nodeD;
nodeD->parent = nodeB;
nodeB->rChild = nodeE;
nodeE->parent = nodeB;
nodeC->lChild = nodeF;
nodeF->parent = nodeC;
nodeC->rChild = nodeG;
nodeG->parent = nodeC;
BinarySearchTree* tree = new BinarySearchTree(nodeA);
return tree;
}
int main()
{
BinarySearchTree* tree = createTree();
//tree->print(tree->getRoot());
//tree->inOrder(tree->getRoot());
//tree->preOrder(tree->getRoot());
//tree->postOrder(tree->getRoot());
//tree->print(tree->getRoot());
//tree->insert(tree->getRoot(), 5);
//tree->print(tree->getRoot());
//Node* result = tree->search(tree->getRoot(), 3);
//tree->deleteNode(tree->getRoot(), 75);
tree->print(tree->getRoot());
cout<<tree->balance(tree->getRoot())<<endl;
}
Algorithem Review: Binary Search Tree(Insert, Delete, Search, Balance, PreOrder, InOrder, PostOrder)
最新推荐文章于 2020-12-18 23:08:10 发布