Algorithem Review: Binary Search Tree(Insert, Delete, Search, Balance, PreOrder, InOrder, PostOrder)

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值