实现算法导论第三版中的二叉搜索树

使用C++实现。为了方便调试,加了一个输出函数。此函数,水平遍历树节点。

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <queue>
#include <iostream>

struct TNode
{
	TNode * p; //parent node
	TNode * left; //left child node
	TNode * right; //right child node
	int     key; //node value
	TNode(int k);
};

TNode::TNode(int k)
	:p(0), left(0), right(0),
	 key(k)
{}

class BinaryTree
{
private:
	TNode * root;
public:
	BinaryTree();
	void Insert(TNode * z);
	void Print();
	TNode * Search(int k);
	TNode * Minimum(int k);
	TNode * Successor(int k);
	void InorderWalk();
	bool Delete(TNode * z);
protected:
	void LevelOrder();
	TNode * Search(TNode * x, int k);
	TNode * Minimum(TNode * x);
	void InorderWalk(TNode * x);
	void Transplant(TNode * u, TNode * v);
};

BinaryTree::BinaryTree()
	:root(0)
{}

void BinaryTree::Insert(TNode * z)
{
	assert(z);
	TNode * y = 0;
	TNode * x = root;
	
	while(x != 0)
	{
		y = x;
		
		if(z->key < x->key)
			x = x->left;
		else
			x = x->right;
	}

	z->p = y;
	if(0 == y)
		root = z;
	else if(z->key < y->key)
		y->left = z;
	else
		y->right = z;
}


void BinaryTree::Print()
{
	LevelOrder();
	std::cout << std::endl;
}

TNode * BinaryTree::Search(int k)
{
	return Search(root, k);
}

TNode * BinaryTree::Minimum(int k)
{
	TNode * x = Search(k);
	
	if(!x)
		return 0;

	return Minimum(x);
}

TNode * BinaryTree::Successor(int k)
{
	TNode * x = Search(k);
	if(!x)
		return 0;

	if(x->right != 0)	
		return Minimum(x->right);

	TNode * y = x->p;

	while(y != 0 && x == y->right)
	{
		x = y;
		y = y->p;
	}

	return y;

}

void BinaryTree::InorderWalk()
{
	InorderWalk(root);
}

bool BinaryTree::Delete(TNode * z)
{
	if(!root)
		return false;

	if(!z)
		return false;

	if(0 == z->left)
		Transplant(z, z->right);
	else if(0 == z->right)
		Transplant(z, z->left);
	else
	{
		TNode * y = Minimum(z->right); //= successor(z->right)
		assert(y);

		if(y->p != z)  //case d
		{
			std::cout << "case d" << std::endl;
			Transplant(y, y->right);
			y->right = z->right;
			y->right->p = y;
		}

		std::cout << "case c" << std::endl;
		//y->p == z
		Transplant(z, y);
		y->left = z->left;
		y->left->p = y;
	}

	return true;
}
void BinaryTree::LevelOrder()
{
	if(!root)
		return ;

	std::queue<TNode *> que;
	TNode * t = 0;
	
	que.push(root);

	while(!que.empty())
	{
		t = que.front();
		que.pop();
		
		if(0 == t->p)
			std::cout<<"R("<<t->key<<")";
		else if(t == t->p->left)
			std::cout<<"L("<<t->key<<")";
		else
			std::cout<<"R("<<t->key<<")";
		
		if(t->left != 0)
			que.push(t->left);

		if(t->right != 0)
			que.push(t->right);
	}
}

TNode * BinaryTree::Search(TNode * x, int k)
{
	while(x != 0 && k != x->key)
		k < x->key?(x = x->left):(x = x->right); 

	return x;
}

TNode * BinaryTree::Minimum(TNode * x)
{	
	while(x->left != 0)
		x = x->left;
	
	return x;
}

void BinaryTree::InorderWalk(TNode * x)
{
	if(x)
	{
		InorderWalk(x->left);
		std::cout << x->key << "," << std::endl;
		InorderWalk(x->right);
	}
}

void BinaryTree::Transplant(TNode * u, TNode * v)
{
	if(0 == u->p)     //u is root
		root = v;
	else if(u == u->p->left)  //u is left child, let left child of the parent is v
		u->p->left = v;
	else                      //u is right child
		u->p->right = v;
	
	if(v != 0)
		v->p = u->p; //change child's parent
}

int main()
{
	BinaryTree tree;
	
	TNode t15(15);
	TNode t5(5);
	TNode t18(18);
	TNode t3(3);
	TNode t2(2);
	TNode t4(4);
	TNode t7(7);
	TNode t13(13);
	TNode t9(9);
	TNode t17(17);
	TNode t20(20);
	TNode t6(6);

	tree.Insert(&t15);
	tree.Insert(&t5);
	tree.Insert(&t18);
	tree.Insert(&t3);
	tree.Insert(&t2);
	tree.Insert(&t4);
	tree.Insert(&t7);
	tree.Insert(&t6);
	tree.Insert(&t13);
	tree.Insert(&t9);
	tree.Insert(&t17);
	tree.Insert(&t20);
	
	tree.Print();
	std::cout << "----find----" << std::endl;

	TNode * res = tree.Search(13);
	if(!res)
		std::cout << "cann't find 13 in the tree" << std::endl;
	else
		std::cout << "find 13.it's parent key = " << res->p->key << std::endl;

	
	res = tree.Successor(13);
	if(!res)
		std::cout << "successor operator fail" << std::endl;
	else
		std::cout << "successor key = " << res->key << std::endl;

		
	std::cout << "\n----inorder walk----" << std::endl;
	tree.InorderWalk();

	std::cout << "\n-----------delete------------\n";
	//tree.Delete(&t7);
	//tree.Print();
	tree.Print();
	tree.Delete(&t5);
	tree.Print();
	
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值