【二叉搜索树】c++实现二叉搜索树

二叉搜索树的定义:
二叉树中每一个节点:
若其左子树存在,则其左子树中每个节点的值都小于该节点值,
若其右子树存在,则其右子树中每个节点的值都大于该节点值。
没有键值相等的节点。

如果想了解更多二叉搜索树的定义:数据结构(二):二叉搜索树(Binary Search Tree)

二叉搜索树主要是用来查找和排序的,
查找的时间复杂度o(logn) ~ o(n) ,
排序只要中序遍历一遍就可以得到有序的序列。

代码:

#include <iostream>
using namespace std;
#include <stack>

class BSTreeNode
{
	int data;
	BSTreeNode* left; 
	BSTreeNode* right;

public:
	BSTreeNode(int d) :data(d), left(NULL), right(NULL) {};
	int GetData() const
	{
		return data;
	}

	void SetData(const int& d)
	{
		data = d;
	}
	BSTreeNode* GetLeft()
	{
		return left;
	}
	BSTreeNode* GetRigh()
	{
		return right;
	}

	void SetLeft(BSTreeNode*& newLeft)
	{
		left = newLeft;
	}
	void SetRight(BSTreeNode*& newRight)
	{
		right = newRight;
	}
};

//创建一个额二叉搜索树
void CreatBSTree(BSTreeNode*& treeRoot)
{
	cout << "输入第一串数字以空格分隔创建搜索二叉树:" << endl;
	int tData;
	while (cin >> tData)
	{
		//声明临时节点并把数据放入进去,注意不能delete
		BSTreeNode* newNode = new BSTreeNode(tData);
		//树没有根节点
		if (treeRoot == NULL)
		{
			treeRoot = newNode;
		}
		//树存在根节点
		else
		{
			//声明移动指针
			BSTreeNode* tmp = treeRoot;
			//找到插入的位置才退出
			while (true)
			{
				//输入数小于该节点的值
				if (tData < tmp->GetData())
				{
					//左孩子为空,则新的节点为左孩子
					if (tmp->GetLeft() == NULL)
					{
						tmp->SetLeft(newNode);
						break;
					}
					//左孩子不为空,p指针移动为左孩子
					else
					{
						tmp = tmp->GetLeft();
					}
				}
				//输入的值大于该节点的值
				else
				{
					//右孩子为空,则新的节点为右孩子
					if (tmp->GetRigh() == nullptr)
					{
						tmp->SetRight(newNode);
						break;
					}
					//若孩子不为空,p指针移动为右孩子
					else
					{
						tmp = tmp->GetRigh();
					}
				}
			}
		}
		if (cin.get() == '\n')
		{
			break;
		}
	}
}

//先序遍历
void PreOrder(BSTreeNode* root)
{
	if (root)
	{
		cout << root->GetData() << " ";
		PreOrder(root->GetLeft());
		PreOrder(root->GetRigh());
	}

}
void PreOrderS(BSTreeNode* root)
{
	if (root == nullptr) return;
	stack<BSTreeNode* > st;
	st.push(root);
	while (!st.empty())
	{
		BSTreeNode* node = st.top();
		st.pop();
		cout << node->GetData() << " ";
		if (node->GetRigh()) st.push(node->GetRigh());
		if (node->GetLeft()) st.push(node->GetLeft());
	}
}


//中序遍历
void InOrder(BSTreeNode* root)
{
	if (root)
	{
		InOrder(root->GetLeft());
		cout << root->GetData() << " ";
		InOrder(root->GetRigh());
	}
}
void InOrderS(BSTreeNode* root)
{
	if (root == nullptr) return;
	stack<BSTreeNode* > st;
	BSTreeNode* cur = root;

	while (!st.empty() || cur != nullptr)
	{
		while (cur != NULL)
		{
			st.push(cur);
			cur = cur->GetLeft();
		}
		cur = st.top();
		st.pop();
		cout << cur->GetData() << " ";
		cur = cur->GetRigh();
	}
}

//后续遍历
void PostOrder(BSTreeNode* root)
{
	if (root)
	{
		PostOrder(root->GetLeft());
		PostOrder(root->GetRigh());
		cout << root->GetData() << " ";
	}
}
void PostOrderS(BSTreeNode* root)
{
	if (root == NULL) return;
	stack<BSTreeNode* > st;
	st.push(root);

	while (!st.empty())
	{
		BSTreeNode* node = st.top();
		if (node != nullptr)
		{
			st.pop();
			st.push(node);
			st.push(nullptr);

			if (node->GetRigh()) st.push(node->GetRigh());
			if (node->GetLeft()) st.push(node->GetLeft());
		}
		else
		{
			st.pop();
			node = st.top();
			st.pop();
			cout << node->GetData() << " ";
		}

	}
}

int main()
{
	//声明树的根节点
	BSTreeNode* tree = nullptr;

	//创建二叉搜索树
	CreatBSTree(tree);

	cout << "前序递归遍历二叉树:";
	PreOrder(tree);
	cout << endl << "前序非递归遍历二叉树:";
	PreOrderS(tree);

	cout << endl << "中序递归遍历二叉树" ;
	InOrder(tree);
	cout << endl << "中序非递归遍历二叉树:" ;
	InOrderS(tree);


	cout<< endl << "后序递归遍历二叉树:" ;
	PostOrder(tree);
	cout << endl << "后序非递归遍历二叉树:";
	PostOrderS(tree);

	getchar();
	return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值