二叉树的基本操作

本篇着重介绍二叉树的一些基本操作,实现二叉树的一些基本接口。

二叉树的基本数据成员

template<class T>
struct BinaryTreeNode
{
    BinaryTreeNode<T> * _left;
    BinaryTreeNode<T> * _right;
    T _data;

    BinaryTreeNode(const T& data)
        :_left(NULL)
	, _right(NULL)
	, _data(data)
    {}

};

二叉树的创建

BinaryTree()
    :_root(NULL)
    {}

BinaryTree(T* arr, size_t n, const T& invalid)
{
    size_t index = 0;
    _root=_CreateTree(arr, n, invalid, index);
}

//前序创建
Node* _CreateTree(T* arr, size_t n, const T& invalid, size_t& index)
{
    Node* root = NULL;
    if (index < n)
    {
	if (arr[index] != invalid)
	{
	    root = new Node(arr[index]);
	    root->_left = _CreateTree(arr, n, invalid, ++index);
	    root->_right = _CreateTree(arr, n, invalid, ++index);
	}
    }
		
    return root;
}

二叉树的拷贝

BinaryTree(const BinaryTree<T>& tree)
{    _root = _CopyTree(tree._root);
    if (NULL == root)
}

Node* _CopyTree(Node* root)
{
    Node* newroot = new Node(root->_data);
    {return NULL;    }
    newroot->_left = _CopyTree(root->_left);
    newroot->_right = _CopyTree(root->_right);    return newroot;
}
二叉树的销毁
~BinaryTree()
{
    Destory(_root);
    _root = NULL;
}

void Destory(Node* root)
{
    if (NULL == root)
    return;
    Destory(root->_left);
    Destory(root->_right);

    delete root;
}

二叉树的扶植运算符重载

//第一种写法
/*BinaryTree& operator=(const BinaryTree<T>& tree)
{
    if (this != &tree)
    {
        Destory(_root);
	_CopyTree(tree._root);
    }
    return *this;
}*/

//第二种写法
BinaryTree& operator=(BinaryTree<T> tree)
{
    swap(_root, tree._root);
    return *this;
}

遍历——递归遍历

前序遍历

void Prevorder()
{
    _Prevorder(_root);
    cout << endl;
}
void _Prevorder(Node* root)
{
    if (root == NULL)
    {
	return;
    }

    cout << root->_data << " ";
    _Prevorder(root->_left);
    _Prevorder(root->_right);
}

中序遍历

void Inorder()
{
    _Inorder(_root);
    cout << endl;
}
void _Inorder(Node* root)
{
    if (root == NULL)
    {
        return;
    }
    cout << root->_data << " ";
    _Inorder(root->_left);
    _Inorder(root->_right);
}

后序遍历

void PostOrder()
{
    _PostOrder(_root);
    cout << endl;
}
void _PostOrder(Node* root)
{
    if (root == NULL)
    {
        return;
}
    _PostOrder(root->_left);
    _PostOrder(root->_right);
    cout << root->_data << " ";
}

遍历——非递归遍历

前序非递归遍历

void PrevOrderNon()
{
    stack<Node*> s;
    Node* cur = _root;
    while (cur || !s.empty())
    {
	while (cur)
	{
	    s.push(cur);
	    cout << cur->_data << " ";
	    cur = cur->_left;
	}
	Node* top = s.top();
	s.pop();

	cur = top->_right;
    }
    cout << endl;
}

中序非递归遍历

void InorderNon()
{

    Node* cur = _root;
    stack<Node*> s;

    while (cur || !s.empty())
    {
        while (cur)
	{
	    s.push(cur);
	    cur = cur->_left;
	}
	Node* top = s.top();
	cout << top->_data << " ";
	cur = top->_right;
	s.pop();
    }
    cout << endl;
}

后序非递归遍历

void PostOrderNon()
{
    Node* cur = _root;
    Node* prev = NULL;
    stack<Node*> s;

    while (cur || !s.empty())
    {
	while (cur)
	{
	    s.push(cur);
	    cur = cur->_left;
	}
	Node* top = s.top();
	if (top->_right == NULL || top->_right == prev)
	{
            cout << top->_data << " ";
	    prev = top;
	    s.pop();
	}
	else
	{
	    cur = top->_right;
	}
    }
    cout << endl;
}

层序遍历

void LevelOrder()
{
    if (_root == NULL)
        return;
    queue<Node*> q;
    q.push(_root);
    while (!q.empty())
    {
	Node* cur = q.front();
	cout << cur->_data << " ";
	q.pop();
	if (cur->_left)
	    q.push(cur->_left);
	if (cur->_right)
	    q.push(cur->_right);
    }
    cout << endl;
}

求节点个数

int Size()
{
    size_t size = 0;
    _Size(_root,size);
    return size;
}

void _Size(Node* root,size_t& size)
{
    if (root == NULL)
	return;
    _Size(root->_left,size);
    ++size;
    _Size(root->_right,size);
}

求叶子节点个数

size_t LeafSize()
{
    return _LeafSize(_root);
}

size_t _LeafSize(Node* root)
{
    if (NULL == root)
	return 0;
    if (root->_left == NULL&&root->_right == NULL)
	return 1;
    return _LeafSize(root->_left) + _LeafSize(root->_right);
}

求树的深度

size_t Height()
{
    return _Height(_root);
}

size_t _Height(Node* root)
{
    if (NULL == root)
	return 0;

    size_t LeftH = _Height(root->_left);
    size_t RightH = _Height(root->_right);
    return LeftH > RightH ? LeftH + 1 : RightH + 1;
}

求第k层节点个数

size_t Get_KLevel(size_t k)
{
    return _Get_KLevel(_root,k);
}

size_t _Get_KLevel(Node* root, size_t k)
{
    if (NULL == root)
	return 0;
    if (k == 1)
    return 1;
    return _Get_KLevel(root->_left, k-1) + _Get_KLevel(root->_right, k-1);
}

判断该数是不是完全二叉树

//判断思路:最下一层必须是连续的叶子节点,
//给每个节点设一个flag,空为false当出现上一节点为空,当前节点不为空时则该树不是完全二叉树
bool IsCompleteTree()
{
    queue<Node*> q;
    if (_root)
        q.push(_root);
    else
	return false;

    bool flag = true;
    //节点为空就将flag置false
    while (!q.empty())
    {
        Node* front = q.front();
	q.pop();
	if (front->_left)//左数不为空
	{
	    if (flag == false)//判断上一个节点是否为空
		return false;


            q.push(front->_left);
	}
	else
	    flag = false;


	if (front->_right)//右树不为空
	{
	    if (flag == false)//判断上一个节点是否为空
		return false;


	    q.push(front->_right);
	}
	else
	    flag = false;


    }
    return true;
}
	

查找

Node* Find(const T& data) {     return _Find(_root,data); } Node* _Find(Node* root, const T& data) {     if (root == NULL)     return NULL;     if (data == root->_data)         return root;     Node* ret1 = _Find(root->_left,data);     if (ret1) return ret1;     Node* ret2 = _Find(root->_right, data);     if (ret2) return ret2;     return NULL; }
源码:
#include<iostream>
#include<stack>
#include<queue>
#include<assert.h>
using namespace std;

template<class T>
struct BinaryTreeNode
{
	BinaryTreeNode<T> * _left;
	BinaryTreeNode<T> * _right;
	T _data;

	BinaryTreeNode(const T& data)
		:_left(NULL)
		, _right(NULL)
		, _data(data)
	{}

};

template<class T>
class BinaryTree
{
	typedef BinaryTreeNode<T> Node;
public:
	BinaryTree()
		:_root(NULL)
	{}

	BinaryTree(T* arr, size_t n, const T& invalid)
	{
		size_t index = 0;
		_root=_CreateTree(arr, n, invalid, index);
	}

	//前序创建
	Node* _CreateTree(T* arr, size_t n, const T& invalid, size_t& index)
	{
		Node* root = NULL;
		if (index < n)
		{
			if (arr[index] != invalid)
			{
				root = new Node(arr[index]);
				root->_left = _CreateTree(arr, n, invalid, ++index);
				root->_right = _CreateTree(arr, n, invalid, ++index);
			}
		}
		
		return root;
	}

	BinaryTree(const BinaryTree<T>& tree)
	{
		_root = _CopyTree(tree._root);
	}

	Node* _CopyTree(Node* root)
	{
		if (NULL == root)
		{
			return NULL;
		}

		Node* newroot = new Node(root->_data);
		newroot->_left = _CopyTree(root->_left);
		newroot->_right = _CopyTree(root->_right);

		return newroot;
	}

	~BinaryTree()
	{
		Destory(_root);
		_root = NULL;
	}

	void Destory(Node* root)
	{
		if (NULL == root)
			return;
		Destory(root->_left);
		Destory(root->_right);

		delete root;
	}

	//第一种写法
	/*BinaryTree& operator=(const BinaryTree<T>& tree)
	{
		if (this != &tree)
		{
			Destory(_root);
			_CopyTree(tree._root);
		}
		return *this;
	}*/

	//第二种写法
	BinaryTree& operator=(BinaryTree<T> tree)
	{
		swap(_root, tree._root);
		return *this;
	}


public:
	//递归

	//前序遍历
	void Prevorder()
	{
		_Prevorder(_root);
		cout << endl;
	}
	void _Prevorder(Node* root)
	{
		if (root == NULL)
		{
			return;
		}

		cout << root->_data << " ";
		_Prevorder(root->_left);
		_Prevorder(root->_right);
	}

	//中序遍历
	void Inorder()
	{
		_Inorder(_root);
		cout << endl;
	}
	void _Inorder(Node* root)
	{
		if (root == NULL)
		{
			return;
		}
		_Inorder(root->_left);
		cout << root->_data << " ";
		_Inorder(root->_right);
	}

	//后序遍历
	void PostOrder()
	{
		_PostOrder(_root);
		cout << endl;
	}
	void _PostOrder(Node* root)
	{
		if (root == NULL)
		{
			return;
		}
		_PostOrder(root->_left);
		_PostOrder(root->_right);
		cout << root->_data << " ";
	}

	//层序遍历
	void LevelOrder()
	{
		if (_root == NULL)
			return;
		queue<Node*> q;
		q.push(_root);
		while (!q.empty())
		{
			Node* cur = q.front();
			cout << cur->_data << " ";
			q.pop();
			if (cur->_left)
				q.push(cur->_left);
			if (cur->_right)
				q.push(cur->_right);
		}
		cout << endl;
	}

	//非递归
	//前序遍历
	void PrevOrderNon()
	{
		stack<Node*> s;
		Node* cur = _root;
		while (cur || !s.empty())
		{
			while (cur)
			{
				s.push(cur);
				cout << cur->_data << " ";
				cur = cur->_left;
			}
			Node* top = s.top();
			s.pop();

			cur = top->_right;
		}
		cout << endl;
	}

	//中序遍历
	void InorderNon()
	{

		Node* cur = _root;
		stack<Node*> s;

		while (cur || !s.empty())
		{
			while (cur)
			{
				s.push(cur);
				cur = cur->_left;
			}
			Node* top = s.top();
			cout << top->_data << " ";
			cur = top->_right;
			s.pop();
		}
		cout << endl;
	}

	//后序遍历
	void PostOrderNon()
	{
		Node* cur = _root;
		Node* prev = NULL;
		stack<Node*> s;

		while (cur || !s.empty())
		{
			while (cur)
			{
				s.push(cur);
				cur = cur->_left;
			}
			Node* top = s.top();
			if (top->_right == NULL || top->_right == prev)
			{
				cout << top->_data << " ";
				prev = top;
				s.pop();
			}
			else
			{
				cur = top->_right;
			}
		}
		cout << endl;
	}
public:

	//size节点个数
	int Size()
	{
		size_t size = 0;
		_Size(_root,size);
		return size;
	}

	void _Size(Node* root,size_t& size)
	{
		if (root == NULL)
			return;
		_Size(root->_left,size);
		++size;
		_Size(root->_right,size);
	}

	//叶子节点个数
	size_t LeafSize()
	{
		return _LeafSize(_root);
	}

	size_t _LeafSize(Node* root)
	{
		if (NULL == root)
			return 0;
		if (root->_left == NULL&&root->_right == NULL)
			return 1;
		return _LeafSize(root->_left) + _LeafSize(root->_right);
	}

	//树的高度
	size_t Height()
	{
		return _Height(_root);
	}

	size_t _Height(Node* root)
	{
		if (NULL == root)
			return 0;

		size_t LeftH = _Height(root->_left);
		size_t RightH = _Height(root->_right);
		return LeftH > RightH ? LeftH + 1 : RightH + 1;
	}

	//求第K层节点个数
	size_t Get_KLevel(size_t k)
	{
		return _Get_KLevel(_root,k);
	}

	size_t _Get_KLevel(Node* root, size_t k)
	{
		if (NULL == root)
			return 0;
		if (k == 1)
			return 1;
		return _Get_KLevel(root->_left, k-1) + _Get_KLevel(root->_right, k-1);
	}

	//判断思路:最下一层必须是连续的叶子节点,
	//给每个节点设一个flag,空为false当出现上一节点为空,当前节点不为空时则该树不是完全二叉树
	bool IsCompleteTree()
	{
		queue<Node*> q;
		if (_root)
			q.push(_root);
		else
			return false;

		bool flag = true;
		//节点为空就将flag置false
		while (!q.empty())
		{
			Node* front = q.front();
			q.pop();
			if (front->_left)//左数不为空
			{
				if (flag == false)//判断上一个节点是否为空
					return false;

				q.push(front->_left);
			}
			else
				flag = false;

			if (front->_right)//右树不为空
			{
				if (flag == false)//判断上一个节点是否为空
					return false;

				q.push(front->_right);
			}
			else
				flag = false;

		}
		return true;
	}

	//查找
	Node* Find(const T& data)
	{
		return _Find(_root,data);
	}

	Node* _Find(Node* root, const T& data)
	{
		if (root == NULL)
			return NULL;
		if (data == root->_data)
			return root;

		Node* ret1 = _Find(root->_left,data);
		if (ret1)
			return ret1;

		Node* ret2 = _Find(root->_right, data);
		if (ret2)
			return ret2;

		return NULL;
	}

protected:
	Node* _root;
};

测试用例:

int main()
{
	int array[] = { 1, 2, 3, '#', '#', 4, 40, '#', '#', '#', 5, 6, '#', '#', '#' };
	BinaryTree<int> t(array, sizeof(array) / sizeof(int), '#');
	t.Prevorder();
	t.PrevOrderNon();
	t.Inorder();
	t.InorderNon();
	t.PostOrder();
	t.PostOrderNon();
	t.LevelOrder();
	cout << "节点个数size为:" << t.Size() << endl;
	cout << "叶子节点Leaf Size:" << t.LeafSize() << endl;
	cout << "高度Height:" << t.Height() << endl;
	cout << "IsCompleteTree?" << t.IsCompleteTree() << endl;
	cout << "第K层节点个数 Level Size:" << t.Get_KLevel(4) << endl;

	BinaryTree<int> t1(t);
	t1.PrevOrderNon();

	system("pause");
	return 0;
}

测试结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值