二叉树的实现和遍历

虽然已经从事编程快4年了,但是由于不是科班出身,存在很多的弱点,尤其是最近的开发过程中,一旦遇到复杂点的对应关系就有气无力的,有点时间就补一下基础知识吧。

查找二叉树的实现和几种遍历方法  参考:http://blog.csdn.net/liuhongxiangm/article/details/17379331,数据结构与算法分析c++描述

源码:http://download.csdn.net/detail/liuhongxiangm/6733259


二叉树实现

#pragma once

#include <iostream>
#include <stack>

using namespace std;

template <typename Comparable>
class BinarySearchTree
{
public:
	BinarySearchTree(void)
		:root(NULL)
	{
		
	}
	BinarySearchTree(const BinarySearchTree &rth)
		:root(NULL)
	{
		if(this != &rth )
		{
			*this = operator=(rth);
		}
	}
	~BinarySearchTree(void)
	{
		makeEmpty();
	}

	//查找最小 递归
	const Comparable & findMin() const
	{
		BinaryNode *pnode = findMin(root);
		if(pnode == NULL)
		{
			return NULL;
		}
		return pnode->element;
	}
	//查找最小 非递归
	const Comparable & findMin2() const
	{
		BinaryNode *pnode = findMin2(root);
		if(pnode == NULL)
		{
			return NULL;
		}
		return pnode->element;
	}
	//查找最大 递归
	const Comparable & findMax() const
	{
		BinaryNode *pnode = findMax(root);
		if(pnode == NULL)
		{
			return NULL;
		}
		return pnode->element;
	}
	//查找最大 非递归
	const Comparable & findMax2() const
	{
		BinaryNode *pnode = findMax2(root);
		if(pnode == NULL)
		{
			return NULL;
		}
		return pnode->element;
	}
	//查找是否包含 递归
	bool  contains(const Comparable &x) const
	{
		return contains(x, root);
	}
	//查找是否包含 非递归
	bool  contains2(const Comparable &x) const
	{
		return contains2(x, root);
	}
	//判断为空
	bool  isEmpty() const
	{
		if (root == NULL)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	//遍历 先序 递归 
	void  printTreePreOrder() const
	{
		if(isEmpty())
		{
			cout << "empty" << endl;
		}
		else
		{
			printTreePreOrder(root);
		}
	}
	//遍历 先序 非递归
	void  printTreePreOrder2() const 
	{
		printTreePreOrder2(root);
	}
	//遍历 中序 递归
	void  printTreeInOrder() const
	{
		if(isEmpty())
		{
			cout << "empty" << endl;
		}
		else
		{
			printTreeInOrder(root);
		}
	}
	void printTreeInorder2() const 
	{
		if(isEmpty())
		{
			cout << "empty" << endl;
		}
		else
		{
			printTreeInorder2(root);
		}
		
	}
	//遍历 后序 递归
	void  printTreePostOrder() const
	{
		if(isEmpty())
		{
			cout << "empty" << endl;
		}
		else
		{
			printTreePostOrder(root);
		}
	}
	void printTreePostOrder2() const
	{
		printTreePostOrder2(root);
	}
	//清空
	void makeEmpty()
	{
		makeEmpty(root);
	}
	//插入递归
	void insert(const Comparable &x)
	{
		insert(x, root);
	}
	//插入 非递归
	void insert2(const Comparable &x)
	{
		insert2(x, root);
	}
	//删除 递归
	void remove(const Comparable &x)
	{
		remove(x, root);
	}
	//树高 递归
	int height()const
	{
		return height(root) ;
	}

	const BinarySearchTree & operator=(const BinarySearchTree &rhs)
	{
		if(this != &rhs)
		{
			makeEmpty();
			root = clone(rhs.root);
		}
		return *this;
	}

private:
	struct BinaryNode
	{
		Comparable element;
		BinaryNode *left;
		BinaryNode *right;

		BinaryNode(const Comparable &theElement, BinaryNode *lt, BinaryNode *rt)
			:element(theElement), left(lt), right(rt)
		{ }
	};


	void insert(const Comparable &x, BinaryNode *&t) const //递归插入
	{
		if(t == NULL)
		{
			t = new BinaryNode(x, NULL, NULL);
		}
		else if(x < t->element)
		{
			insert(x, t->left);
		}
		else if(t->element < x)
		{
			insert(x, t->right);
		}
		else
		{
			;
		}
	}
	void insert2(const Comparable &x, BinaryNode *&t) const //非递归 插入
	{
		if( t== NULL)
		{
			t = new BinaryNode(x, NULL,NULL);
		}
		else
		{
			BinaryNode *pt = t;

			while(pt != NULL)
			{
				if(x < pt->element)
				{
					if (pt->left == NULL)
					{
						pt->left = new BinaryNode(x, NULL,NULL);
						return;
					}
					else
					{
						pt = pt->left;
					}
				}
				else if(pt->element < x)
				{
					if(pt->right == NULL)
					{
						pt->right = new BinaryNode(x, NULL,NULL);
						return ;
					}
					else
					{
						pt = pt->right;
					}
				}
				else
				{
					return ; //==
				}
			}

		}
	}

	void remove(const Comparable &x, BinaryNode *&t) const //递归remove
	{
		if(t == NULL)
		{
			return ;
		}
		if( x < t->element)
		{
			remove(x, t->left);
		}
		else if(t->element < x)
		{
			remove(x, t->right);
		}
		else if(t->left != NULL && t->right != NULL)
		{
			t->element = findMin(t->right)->element;
			remove(t->element, t->right);
		}
		else
		{
			BinaryNode *old = t;
			t = (t->left != NULL) ? t->left:t->right;
			delete old;
		}
	}

	BinaryNode *findMin(BinaryNode *t) const //递归查找最小值
	{
		if(t == NULL)
		{
			return NULL;
		}
		if(t->left == NULL)
		{
			return t;
		}
		return findMin(t->left);
	}
	BinaryNode *findMin2(BinaryNode *t) const //非递归查找最小值
	{
		if(t != NULL)
		{
			while(t->left != NULL)
			{
				t = t->left;
			}
		}
		return t;
	}

	BinaryNode *findMax(BinaryNode *t) const //递归查找最大值
	{
		if(t == NULL)
		{
			return NULL;
		}
		if(t->right == NULL)
		{
			return t;
		}
		return findMax(t->right);
	}
	BinaryNode *findMax2(BinaryNode *t) const //非递归查找最大值
	{
		if(t != NULL)
		{
			while(t->right != NULL)
			{
				t = t->right;
			}
		}
		return t;
	}

	bool contains(const Comparable &x, BinaryNode *t ) const //递归判断是否存在 
	{
		if(t == NULL)
		{
			return false;
		}
		else if(x < t->element)
		{
			return contains(x, t->left);
		}
		else if(t->element < x  )
		{
			return contains(x, t->right);
		}
		else
		{
			return true;
		}
	}
	bool contains2(const Comparable &x, BinaryNode *t ) const //非递归 判断是否存在
	{
		while (t != NULL)
		{
			if (x < t->element)
			{
				t = t->left;
			}
			else if(t->element < x  )
			{
				t = t->right;
			}
			else
			{
				return true;
			}
		}
		return false;
	}

	void makeEmpty(BinaryNode *&t)
	{

		if(t != NULL)
		{
			makeEmpty(t->left);
			makeEmpty(t->right);
			delete t;
		}
		t = NULL;
	}

	BinaryNode *clone(BinaryNode *t) const  //clone 晦涩
	{
		if(t == NULL)
		{
			return NULL;
		}
		return new BinaryNode(t->element, clone(t->left), clone(t->right));
	}

	int height(BinaryNode *t) const //树高
	{
		if( t== NULL)
		{
			return -1;
		}
		else
		{
			return 1+max(height(t->left), height(t->right));
		}
	}

	//递归遍历 先序
	void printTreePreOrder(BinaryNode *t) const //递归遍历 先序
	{
		if(t != NULL)
		{
			cout << t->element << "  ";
			printTreePreOrder(t->left);
			printTreePreOrder(t->right);
		}
	}
	//非递归遍历 先序
	void printTreePreOrder2(BinaryNode *t) const 
	{
		stack<BinaryNode*> stackNode;
		BinaryNode *pnode = t;
		while(pnode != NULL || !stackNode.empty())
		{
			while (pnode != NULL)
			{
				cout << pnode->element << "  ";
				stackNode.push(pnode);
				pnode = pnode->left;
			}
			if(!stackNode.empty())
			{
				pnode = stackNode.top();
				stackNode.pop();
				pnode = pnode->right;
			}
		}
	}
	//递归 遍历 中序
	void printTreeInOrder(BinaryNode *t) const //递归遍历 中序
	{
		if (t != NULL)
		{
			printTreeInOrder(t->left);
			cout << t->element << "  ";
			printTreeInOrder(t->right);
		}
	}
	// 非递归遍历 中序
	void printTreeInorder2(BinaryNode *t) const
	{
		stack<BinaryNode*> stackNode;
		BinaryNode *pnode = t;
		while(pnode != NULL || !stackNode.empty())
		{
			while(pnode != NULL)
			{
				stackNode.push(pnode);
				pnode = pnode->left;
			}
			if(!stackNode.empty())
			{
				pnode = stackNode.top();
				cout << pnode->element << "  ";
				stackNode.pop();
				pnode= pnode->right;
			}

		}
	}
	//递归 遍历 后序
	void printTreePostOrder(BinaryNode *t) const //递归遍历 后序
	{
		if(t != NULL)
		{
			printTreePostOrder(t->left);
			printTreePostOrder(t->right);
			cout << t->element << "  ";
		}
	}
	//非递归 遍历 后序
	void printTreePostOrder2(BinaryNode *t) const 
	{
		stack<BinaryNode*> s;
		BinaryNode *cur = NULL;
		BinaryNode *pre = NULL;

		if (root != NULL)
		{
			s.push(root);
		}
		
		while(!s.empty())
		{
			cur = s.top();
			if( (cur->left == NULL && cur->right == NULL) || (pre != NULL &&(pre == cur->left || pre == cur->right)))
			{
				cout << cur->element << "  ";
				s.pop();
				pre = cur;
			}
			else
			{
				if(cur->right != NULL)
				{
					s.push(cur->right);
				}
				if(cur->left != NULL)
				{
					s.push(cur->left );
				}

			}
		}
	}

	private:
		BinaryNode *root;

};

测试:

#include <iostream>
#include <stdio.h>

#include "BinarySearchTree.h"
using namespace std;

int main()
{
	BinarySearchTree<int> binTree;

	binTree.insert(1);
	binTree.insert(3);
	binTree.insert(4);
	binTree.insert2(78);
	binTree.insert2(22);
	binTree.insert2(66);
	binTree.insert2(57);
	binTree.insert(57);
	binTree.insert(89);


	cout << endl <<  "****************************" << endl;
	binTree.printTreeInOrder();
	cout << endl << "****************************" << endl;
	binTree.printTreeInorder2();
	cout << endl << "****************************" << endl;
	binTree.printTreePreOrder();
	cout << endl << "****************************" << endl;
	binTree.printTreePreOrder2();
	cout << endl << "****************************" << endl;
	binTree.printTreePostOrder();
	cout << endl << "****************************" << endl;
	binTree.printTreePostOrder2();
	cout << endl << "****************************" << endl;
	cout << "contins 78 : " << binTree.contains(78) << "  " << binTree.contains2(78) ;
	cout << endl << "****************************" << endl;

	getchar();
	return 0;
}


结果

你可能感兴趣的:(二叉树的实现和遍历)