模拟实现搜索二叉树

模拟实现搜索二叉树
# include <iostream>

using namespace std;
template <class T>
struct BSTNode
{
	//左右子树构成空
	BSTNode(const T& val = T())
	:_data(val)
	, _pLeft(nullptr)
	, _pRight(nullptr)
	{}
	T _data;
	BSTNode<T>* _pLeft;
	BSTNode<T>* _pRight;
};

template <class T>
class BSTree
{
public:
	typedef BSTNode<T> Node;
	typedef Node* pNode;

​```
BSTree(const pNode root = nullptr)
	:_root(root)
{}

pNode find(const T& val)
{
	if (_root == nullptr)
		return nullptr;
	pNode cur = _root;
	//递归方法查找
	while (cur)
	{
		if (cur->_data == val)
			return cur;
		else if (cur->data > val)
		{
			cur = cur->_pLeft;
		}
		else
		{
			cur = cur->_pRight;
		}
	}
	return nullptr;
}

bool insert(const T& val)
{
	if (_root == nullptr)
	{
		//如果树为空,则创建一个新的节点
		_root = new Node(val);
		return true;
	}
	pNode cur = _root;
	pNode parent = nullptr;
	while (cur)
	{
		//记录一下父母节点
		parent = cur;
		if (cur->_data > val)
		{
			cur = cur->_pLeft;
		}
		else if (cur->_data < val)
		{
			cur = cur->_pRight;
		}
		else
			return false;
	}
	
	pNode newNode = new Node(val);
	//如果大于,左插
	if (parent->_data > val)
	{
		parent->_pLeft = newNode;
	}
	//否则右
	else
		parent->_pRight = newNode;

	return true;
}

bool erase(const T& val)
{
	if (_root == nullptr)
		return false;
	pNode cur = _root;
	pNode parent = nullptr;
	while (cur)
	{
		if (cur->_data == val)
			break;
		else if (cur->_data > val)
		{
			parent = cur;
			cur = cur->_pLeft;
		}
		else
		{
			parent = cur;
			cur = cur->_pRight;
		}
	}

	//删除
	//1. 删除叶子节点
	if (cur->_pLeft == nullptr && cur->_pRight == nullptr)
	{
		//是否删除_root
		if (cur != _root)
		{
			//让父亲节点对应位置置为空
			if (parent->_pLeft == cur)
				parent->_pLeft = nullptr;
			else
				parent->_pRight = nullptr;
		}
		else
		{
			//删除_root, 树为空
			_root = nullptr;
		}
		delete cur;
		cur = nullptr;
	}
	//如果cur的左边为空,则只更新cur的右边
	else if (cur->_pLeft == nullptr)
	{
		if (cur != _root)
		{
			//确定当前节点在父亲节点的左边还是右边,对应位置设为cur->_pRight
			if (parent->_pLeft == cur)
				parent->_pLeft = cur->_pRight;
			else
				parent->_pRight = cur->_pRight;
		}
		else
		{
			//更新_root
			_root = _root->_pRight;
		}
		delete cur;
		cur = nullptr;
	}
	//如果右边为空则只更新cur的左边
	else if (cur->_pRight == nullptr)
	{
		if (cur != _root)
		{
			if (parent->_pLeft == cur)
				parent->_pLeft = cur->_pLeft;
			else
				parent->_pRight = cur->_pLeft;
		}
		else
		{
			_root = _root->_pLeft;
		}
		delete cur;
		cur = nullptr;
	}
	//找左边最大的,或者找右边最小的
	else
	{
		//cur 左右孩子都存在
		//1. 寻找替换节点
		pNode pNext = cur->_pLeft;  // 3   5
		parent = cur;
		while (pNext->_pRight)
		{
			parent = pNext;  // 3  4
			pNext = pNext->_pRight;
		}

		//2. 置换
		cur->_data = pNext->_data;
		//指向置换节点的指针置空
		if (parent->_pRight == pNext)
			parent->_pRight = nullptr;
		parent->_pLeft = nullptr;

		//3. 删除置换节点
		delete pNext;
		pNext = nullptr;
	}
	return true;
}

void Inorder()
{
	_Inorder(_root);
	cout << endl;
}
//中序遍历
void _Inorder(pNode root)
{
	if (root)
	{
		_Inorder(root->_pLeft);
		cout << root->_data << " ";
		_Inorder(root->_pRight);
	}
}

~BSTree()
{
	Distory(_root);
}

void Distory(pNode root)
{
	if (root)
	{
		Distory(root->_pLeft);
		Distory(root->_pRight);
		delete root;
		root = nullptr;
	}
}
​```

private:
	pNode _root;
};

void testTree()
{
	BSTree<int> bt;
	bt.insert(5);
	//bt.Inorder();
	bt.insert(3);
	//bt.Inorder();
	bt.insert(4);
	//bt.Inorder();
	bt.insert(1);
	//bt.Inorder();
	bt.insert(0);
	//bt.Inorder();
	bt.insert(2);
	//bt.Inorder();
	bt.insert(7);
	//bt.Inorder();
	bt.insert(6);
	//bt.Inorder();
	bt.insert(8);
	bt.insert(9);
	bt.Inorder();
	bt.erase(4);
	bt.Inorder();
	bt.erase(8);
	bt.Inorder();
	bt.erase(0);
	bt.Inorder();
	bt.erase(1);
	bt.Inorder();
	bt.erase(2);
	bt.Inorder();
	bt.erase(5);
	bt.Inorder();
	bt.erase(3);
	bt.Inorder();
	bt.erase(7);
	bt.Inorder();
	bt.erase(6);
	bt.Inorder();
	bt.erase(9);
	bt.Inorder();
}
int main()
{
	testTree();
	system("pause");
	return 0;
}

####最优情况下,二叉搜索树为完全二叉树,其平均比较次数为: log2N

####最差情况下,二叉搜索树退化为单支树,其平均比较次数为: N/2

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* * 基于向量实现的完全二叉树 */ package dsa; public class ComplBinTree_Vector extends BinTree_LinkedList implements ComplBinTree { private Vector T;//向量 //构造方法:默认的空树 public ComplBinTree_Vector() { T = new Vector_ExtArray(); root = null; } //构造方法:按照给定的节点序列,批量式建立完全二叉树 public ComplBinTree_Vector(Sequence s) { this(); if (null !=s) while (!s.isEmpty()) addLast(s.removeFirst()); } /*---------- BinaryTree接口中各方法的实现 ----------*/ //返回树根(重写) public BinTreePosition getRoot() { return T.isEmpty() ? null : posOfNode(0); } //判断是否树空(重写) public boolean isEmpty() { return T.isEmpty(); } //返回树的规模(重写) public int getSize() { return T.getSize(); } //返回树(根)的高度(重写) public int getHeight() {return isEmpty() ? -1 : getRoot().getHeight(); } /*---------- ComplBinTree接口中各方法的实现 ----------*/ //生成并返回一个存放e的外部节点,该节点成为新的末节点 public BinTreePosition addLast(Object e) { BinTreePosition node = new ComplBinTreeNode_Rank(T, e); root = (BinTreePosition) T.getAtRank(0); return node; } //删除末节点,并返回其中存放的内容 public Object delLast() { if (isEmpty()) return null;//若树(堆)已空,无法删除 if (1 == getSize()) root = null;//若删除最后一个节点,则树空 return T.removeAtRank(T.getSize()-1); } //返回按照层次遍历编号为i的节点的位置,0 <= i < size() public BinTreePosition posOfNode(int i) { return (BinTreePosition)T.getAtRank(i); } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值