Binary Search Tree implementation

#include <iostream>
#include <vector>
#include <string>
#include <memory>


using namespace std;

struct Node
{
	int data;
	struct Node *left = nullptr;
	struct Node *right = nullptr;
	Node() = default;
	Node(int d, Node *l, Node *r) : data(d), left(l), right(r) {}
};

class BST
{
public:
	BST(Node *rt = nullptr) : root(rt) {}
	BST(const vector<int> &arry)
	{
		for (int i = 0; i < arry.size(); ++i)
			insert(arry[i]);
	}

	// find function
	bool lookup(int target)
	{
		Node *node = root;
		vector<Node*> stack;
		while (node != nullptr || !stack.empty())
		{
			if (node != nullptr)
			{
				stack.push_back(node);
				node = node->left;
			}
			else
			{
				node = stack.back();
				stack.pop_back();
				if (node->data == target)
					return true;
				node = node->right;
			}
		}
		return false;
	}

	// insert function, return the pointr to inserted node 
	// or existed node have same value
	Node *insert(int data)
	{
		 Node **node = &root;
		while (*node != nullptr)
		{
			if (data == (*node)->data) 
				return *node;
			if (data < (*node)->data)
			{
				node = &(*node)->left;
				continue;
			}
			if (data > (*node)->data)
				node = &(*node)->right;
		}
		*node = new Node(data, nullptr, nullptr);	
		return *node;
	}

	size_t size() { return sizeHelper(root); }										// get the size of binary tree
	size_t maxDepth() { return maxDepthHelper(root); }								// get the max depth
	bool deleteNode(int target) { return deleteHelper(nullptr, root, target); }		// delete target value node, return true for success deletion
	void printTree() { printHelper(root); }											// in-order
private:
	size_t sizeHelper(Node *node)
	{
		if (node == nullptr)
			return 0;
		else
			return sizeHelper(node->left) + sizeHelper(node->right) + 1;
	}

	size_t maxDepthHelper(Node *node)
	{
		if (node == nullptr)
			return 0;
		else
		{
			int ldepth = maxDepthHelper(node->left);
			int rdepth = maxDepthHelper(node->right);
			return (ldepth > rdepth ? ldepth : rdepth) + 1;
		}
	}

	bool deleteHelper(Node *parent, Node *current, int target)
	{
		// root == nullptr
		if (current == nullptr)
			return false;
		if (current->data == target)
		{
			// target node has only one child or no child
			// just set current node's child's parent to the parent of current
			if (current->left == nullptr || current->right == nullptr)				
			{																		
				Node *temp = current->left;
				if (current->right != nullptr)
					Node *temp = current->right;
				if (parent != nullptr)
				{
					if (parent->left == current)
						parent->left = temp;
					else
						parent->right = temp;
				}
				else
				{
					root = temp;
				}
			}
			else
			{	
				Node *parent2 = current;
				Node *successor = current->right;
				while (successor->left != nullptr)
				{
					parent2 = successor;
					successor = successor->left;
				}
				swap(current->data, successor->data);
				return deleteHelper(parent2, successor, target);
			}
		}
		return deleteHelper(current, current->left, target) || 
			   deleteHelper(current, current->right, target);
	}

	void printHelper(Node *node)
	{
		if (node == nullptr)
			return;
		printHelper(node->left);
		cout << node->data << " ";
		printHelper(node->right);
	}
private:
	Node *root;
};


int main()
{
	vector<int> vi{ 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31 };
	BST binaryTree(vi);
	cout << "size of Tree: " << binaryTree.size() << endl;
	cout << "in order traverse:" << endl;
	binaryTree.printTree();
	cout << endl;
	cout << "Deleting value 19, after deletion:" << endl;
	binaryTree.deleteNode(19);
	binaryTree.printTree();
	cout << endl;
	system("PAUSE");
	return 0;
}

最难的部分就是删除,具体算法在第一个引用网站。
Reference:
http://www.algolist.net/Data_structures/Binary_search_tree/Removal
http://cslibrary.stanford.edu/110/BinaryTrees.html


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值