二叉查找树

#ifndef 二叉查找树_H_
#define 二叉查找树_H_

#include <iostream>  // 在头文件中不要用using namespace std; 容易产生错误,

enum Boolean {FALSE,TRUE};

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

template<class Type> class BST;  // 前置声明,

template<class Type>
class BstNode   // 树节点,
{
	friend class BST<Type>;
//private:
public:
	Element<Type> data;
	BstNode* LeftChild;
	BstNode* RightChild;
	void display(int i);   // display就是显示指针的数据,显示左边的数和右边的数,
};

template<class Type>
class BST
{
public:
	BST(BstNode<Type> *init = 0)
	{
		root = init;
	}
	// 要增加Delete
	// 要增加InOrder/PreOrder/PostOrder
	Boolean Insert(const Element<Type>& x);  //Insert 是插入,返回值是一个bool值,
	BstNode<Type>* Search(const Element<Type>& x); // Search就是查找,
	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> *p = root;
	BstNode<Type> *q = 0;  // q指向p的父节点,
	// insert之前要先查找,
	while(p)
	{
		q = p;
		if(x.key == p->data.key) return FALSE;  // 发生重复,失败返回FALSE,
		if(x.key < p->data.key) 
			p = p->LeftChild;
		else
			p = p->RightChild;
	}

	// 找到的位置就是q,
	p = new BstNode<Type>;
	p->LeftChild = p->RightChild = 0;
	p->data = x;
	if(!root) root = p;
	else if(x.key < q->data.key) q->LeftChild = p;
	else q->RightChild = p;
	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;
	if(x.key < b->data.key) return Search(b->LeftChild,x);
	return Search(b->RightChild,x);
}

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


#endif

#include <iostream>
#include "二叉查找树.h"

using namespace std;

int main()
{
	BST<int> m;
	Element<int> a,b,c,d,e,f,g,h,i,j;
	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 << "\n" << m.Insert(a);  // a 就是5,就是root
	cout << endl;
	
	m.display();  //输出的是: 位置是1,数据是5,

	cout << "\n" << m.Insert(b) << endl << endl;
	cout << m.Insert(c) << endl;
	cout << m.Insert(d) << endl;
	cout << m.Insert(e) << endl;
	cout << m.Insert(f) << endl;
	cout << m.Insert(g) << endl;
	cout << m.Insert(h) << endl;
	cout << m.Insert(i) << endl;
	m.display();

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

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

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值