二叉搜索树

BinarySearchTree.h

#ifndef BinarySearchTree_H
#define BinarySearchTree_H

#include <iostream>

template<class Type> class BST; //前置声明
enum Boolean {FALSE, TRUE};


template<class Type>
class Element
{
public:
	Type key;
	//添加更多的数据
};


template<class Type>
class BstNode //树节点
{
	friend class BST<Type>;
//private:
public:
	Element<Type> data;
	BstNode* LeftChild;
	BstNode* RightChild;
	void display(int i);
};


template<class Type>
class BST
{
public:
	BST(BstNode<Type> *init = 0):root(){}

	Boolean Insert(const Element<Type>& x);
	//要增加 Delete
	//要增加 InOrder
	//		 PreOrder
	//		 PostOrder
	BstNode<Type>* Search(const Element<Type>& x);
	BstNode<Type>* Search(BstNode<Type>*, const Element<Type>&);
	BstNode<Type>* IterSearch(const Element<Type>&);
	void display()
	{
		cout << "\n";
		if(root)
			root->display(1);
		else
			cout << "这是一棵空树\n";
	}
private:
	BstNode<Type> *root;
};

template<class Type>
void BstNode<Type>::display(int i)
{
	std::cout << "Position: " << i << ", data.key = " << data.key << "\n";
	if(LeftChild) LeftChild->display(2*i);
	if(RightChild) RightChild->display(2*i+1);
}

template<class Type>
Boolean BST<Type>::Insert(const Element<Type> &x)
{
	BstNode<Type> *current = root;
	BstNode<Type> *parent = 0; //parent指向current的父节点
							   //insert之前要先查找
	while (current)
	{
		//两个结点向前移动,后面的先移动
		parent = current;
		if (x.key == current->data.key) return FALSE; //发生重复,失败返回FALSE
		if (x.key < current->data.key)
			current = current->LeftChild;
		else
			current = current->RightChild;
	}

	//此时current为空,要插入的结点就应该变成current结点,所以新的结点newNode应该放在current的父亲上
	BstNode<Type> *newNode;
	newNode = new BstNode<Type>;
	newNode->LeftChild = newNode->RightChild = 0;
	newNode->data = x;
	if (!root)
		root = newNode;
	else if (x.key < parent->data.key)
		parent->LeftChild = newNode;
	else
		parent->RightChild = newNode;
	return TRUE;
}


template<class Type>
BstNode<Type>* BST<Type>::Search(const Element<Type> &x)
{
	return Search(root, x);
}

template<class Type>
BstNode<Type>* BST<Type>::Search(BstNode<Type> * b, const Element<Type> &x)
{
	
	if(!b) return 0;
	if(x.key == b->data.key) 
		return b;
	else if(x.key < b->data.key)  
		return Search(b->LeftChild,x);
	else
		return Search(b->RightChild,x);
	/*这里输出离开了是因为大家可以试一下,把111-114行注释掉,然后顺便注释掉117行,你会发现程序运行良好。
	而将111-114行注释掉,然后不注释117行,程序就会出错,所以。。。反正我不知道为什么*/
	cout << "离开了";
}

template<class Type>
BstNode<Type>* BST<Type>::IterSearch(const Element<Type> &x)
{
	BstNode<Type>* t = root;
	while(t)
	{
		if(x.key == t->data.key) 
			return t;
		if(x.key < t->data.key)
			t = t->LeftChild;
		else
			t = t->RightChild;
	}
	return 0;
}

#endif

main.cpp

// 用来进行测试

#include <iostream>
#include "BinarySearchTree.h"


using namespace std;

int main()
{
	BST<int> m;
	Element<int> a, b, c, d, e, f, g, h, i, j, k, l;
	a.key = 5;
	b.key = 3;
	c.key = 11;
	d.key = 3;
	e.key = 15;
	f.key = 2;
	g.key = 8;
	h.key = 22;
	i.key = 20;
	j.key = 9;

	cout << endl;
	cout << m.Insert(a) << endl; //a=5,就是root
	cout << m.Insert(b) << endl; //b=3
	cout << m.Insert(c) << endl; // 11
	cout << m.Insert(d) << endl; //
	cout << m.Insert(e) << endl; // 15
	cout << m.Insert(f) << endl; // 2
	cout << m.Insert(g) << endl; // 8
	cout << m.Insert(h) << endl; // 22

	m.display();

	BstNode<int> *p = m.Search(f);
	cout << "找到的是: " << p->data.key << endl;

	BstNode<int> *p2 = m.IterSearch(e);
	cout << "找到的是: " << p2->data.key << endl;

	system("pause");
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值