二叉搜索树的实现(增,删,顺序输出)

27 篇文章 0 订阅
20 篇文章 1 订阅

这里只对删除做出部分解释
    ①左右子树都有:
            a、左子树没有右孩子
                    直接让左孩子继承自己的右孩子和父亲
            b、左子树有右孩子
                    一路向右,找到最后的一个右孩子,然后将这个孩子的
                    左子树挂在它父亲的右子树上,然后让它继承要删除节点的人际关系(左右子树和父亲)当要删除的节点是根节点                        时,不用继承父亲关系,但要修改根节点指向。
    ②只有左子树
             直接让左子树继承自己的父亲关系,如果要删的是根,那么直接换根即可。
    ③其他
             直接让右子树(或者空)继承自己的父亲关系,其他同上
    */

其余比较简单直接上代码

#pragma once
#include<vector>
#include<stack>

using namespace std;

namespace wr
{
	template<class T>
	class TreeNode
	{
		T m_val;
		TreeNode *m_left;
		TreeNode *m_right;
	public:
		TreeNode(T val) :
			m_val(val),
			m_left(nullptr),
			m_right(nullptr)
		{}

		template<class T>
		friend class binSortTree;
	};

	template<class T>
	class binSortTree
	{
		TreeNode<T> *m_root;
	public:
		binSortTree() :
			m_root(nullptr)
		{}

		bool insert(T val)
		{
			if (m_root == nullptr)//If m_root is equal to null
			{
				m_root = new TreeNode<T>(val);
				return true;
			}
			TreeNode<T> *pre = m_root;
			TreeNode<T> *cur = m_root;
			while (cur)//Find the point to insert
			{
				if (cur->m_val > val)//Vale is smaller
				{
					pre = cur;
					cur = cur->m_left;
				}
				else if (cur->m_val < val)//Vale is biger
				{
					pre = cur;
					cur = cur->m_right;
				}
				else//If the point to be iserted aleardy exit 
				{
					return false;
				}
			}
			cur = new TreeNode<T>(val);//insert
			if (val < pre->m_val)
			{
				pre->m_left = cur;
			}
			else
			{
				pre->m_right = cur;
			}
			return true;
		}

		bool erase(T val)
		{
			//Find this vale
			TreeNode<T> *pre = m_root;//Mark the previous
			TreeNode<T> *cur = m_root;//Mark vale to delte
			while(cur)
			{
				if (cur->m_val > val)
				{
					pre = cur;
					cur = cur->m_left;
				}
				else if (cur->m_val < val)
				{
					pre = cur;
					cur = cur->m_right;
				}
				else
				{
					break;
				}
			}
			if (cur == nullptr)//the value does not exist
			{
				return false;
			}

			if (cur->m_left && cur->m_right)//if left and right nodes exist
			{
				TreeNode<T> *pre2 = cur;//previous value
				TreeNode<T> *cur2 = cur->m_left;//replace value
				if (cur2->m_right)
				{
					while (cur2->m_right)//find the replace value
					{
						pre2 = cur2;
						cur2 = cur2->m_right;
					}
					pre2->m_right = cur2->m_left;
					cur2->m_left = cur->m_left;
				}
				cur2->m_right = cur->m_right;
				if (pre == cur)
				{
					m_root = cur2;
				}
				else
				{
					if (pre->m_val > val)
					{
						pre->m_left = cur2;
					}
					else
					{
						pre->m_right = cur2;
					}
				}
			}
			else if (cur->m_left)//Only the left nodes exit
			{
				if (cur == pre)
				{
					m_root = m_root->m_left;
				}
				else
				{
					if (pre->m_val > val)
					{
						pre->m_left = cur->m_left;
					}
					else
					{
						pre->m_right = cur->m_left;
					}
				}
			}
			else//Only the right nodes exit or left and right nodes do oot esist
			{
				if (pre == cur)
				{
					m_root = m_root->m_right;
				}
				else
				{
					if (pre->m_val > val)
					{
						pre->m_left = cur->m_right;
					}
					else
					{
						pre->m_right = cur->m_right;
					}
				}
			}
			delete cur;
		}

		vector<T> inorder()
		{
			vector<T> v;
			stack<TreeNode<T>*> s;
			TreeNode<T>* cur = m_root;
			while(cur || !s.empty())
			{
				while (cur)
				{
					s.push(cur);
					cur = cur->m_left;
				}
				TreeNode<T> *tmp = s.top();
				s.pop();
				if (tmp)
				{
					v.push_back(tmp->m_val);
					cur = tmp->m_right;
				}
			}
			return v;
		}

	};

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值