二叉树的存储方式和遍历方式

二叉树:

二叉树的每个节点至多有两个子树。如这个二叉树,其中1,2有两个子树,3只有左子树,5有右子树,4,6,7没有子树。

《二叉树的存储方式和遍历方式》

二叉树有两种存储方式:

第一种,数组表示。用数组存储方式就是用一组连续的存储单元存储二叉树的数据元素。

《二叉树的存储方式和遍历方式》                                    《二叉树的存储方式和遍历方式》

两颗树分别用数组表示为:

《二叉树的存储方式和遍历方式》                                                  《二叉树的存储方式和遍历方式》

用下标就可以直接找到那个节点,也可以找这个节点的左孩子2n+1,右孩子2n+2.找父节点(n-1)/2

但是数组也有缺陷,比如第二个就会浪费存储空间。

若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。完全二叉树就适合用数组表示。

满二叉树就是除了叶结点外每一个结点都有左右子叶且叶结点都处在最底层的二叉树。也适合用数组表示。

完全二叉树不是满二叉树,满二叉树就是完全二叉树。

第二种,链表存储表示。其中又有二叉链表结构和三叉链表结构。

《二叉树的存储方式和遍历方式》

左图就是二叉链表结构,右图是三叉链表结构。

二叉链表里有leftchild指针,rightchild指针和数据。三叉链表里不仅有leftchild指针,rightchild指针和数据还有parent指针,能找到父节点。


二叉的遍历方式有四种:

第一种:前序遍历。先访问根节点,再访问左子树,最后访问右子树。

第二种:中序遍历。先访问左子树,再访问根节点,最后访问右子树。

第三种:后序遍历。先访问左子树,再访问右子树,最后访问根节点。

第四种:层序遍历。一层层节点依次遍历。   

《二叉树的存储方式和遍历方式》

这颗二叉树的前序遍历:1-2-4-5-7-3-6

中序遍历:4-2-5-7-1-6-3

后序遍历:4-7-5-2-6-3-1

层序遍历:1-2-3-4-5-6-7


代码实现:

#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
#include <queue>
template <class T>
struct BinaryTreeNode
{
	T _data;
	BinaryTreeNode<T>* _leftchild;
	BinaryTreeNode<T>* _rightchild;

	BinaryTreeNode(const T& x)//构造函数
		:_data(x)
		, _leftchild(NULL)
		, _rightchild(NULL)
	{}
};

template <class T>
class BinaryTree
{
	typedef BinaryTreeNode<T> Node;
public:
	BinaryTree()//无参构造函数
		:_root(NULL)
	{}
	BinaryTree(T* a, size_t n, const T& invalid = T())//构造函数
	{
		size_t index = 0;
		_root = _GreateTree(a, n, invalid, index);
	}
	BinaryTree(const BinaryTree<T>& t)//拷贝构造
	{
		_root = _copy(t._root);
	}
	BinaryTree<T>& operator=(const BinaryTree<T>& t)
	{
		if (this != &t)
		{
			Node* newRoot = _copy(t._root);
			_Destroy(_root);
			_root = newRoot;
		}
		return *this;
	}
	~BinaryTree()//析构函数
	{
	void _Destory();
	}
	void PrevOrder()//前序遍历递归
	{
		_PrevOrder(_root);
		cout << endl;
	}
	void InOrder()//中序遍历递归
	{
		_InOrder(_root);
		cout << endl;
	}
	void PostOrder()//后序遍历递归
	{
		_PostOrder(_root);
		cout << endl;
	}
	void LeveIOrder()//层序遍历,运用队列,以当前层带下一层
	{
		queue<Node*> q;
		if (_root)
			q.push(_root);
		while (!q.empty())//队列不为空
		{
			Node* front = q.front();//取队头
			cout << front->_data << " ";
			q.pop();//删除队头
			if (front->_leftchild)
				q.push(front->_leftchild);
			if (front->_rightchild)
				q.push(front->_rightchild);
		}
		cout << endl;
	}
	size_t Size()//节点的个数
	{
		return _Size(_root);
	}
	size_t Depth()//树的深度
	{
		return _Depth(_root);
	}
	size_t LeafSize()//叶子节点的个数
	{
		return _LeafSize(_root);
	}
	size_t GetKLevel(size_t k)//第k层节点的个数
	{
		assert(k > 0);
		return _GetKLevel(_root, k);
	}
	Node* Find(const T& x)//找节点x
	{
		return _Find(_root, x);
	}
protected:
	Node* _copy(Node* root)//拷贝
	{
		if (root == NULL)
			return NULL;
		Node* newNode = new Node(root->_data);
		newNode->_leftchild = _copy(root->_leftchild);
		newNode->_rightchild =_copy(root->_rightchild);
		return newNode;
	}
	void _Destroy(Node* root)//销毁
	{
		if (root == NULL)
			return;
		_Destroy(root->_leftchild);
		_Destroy(root->_rightchild);
		delete root;
	}
	Node* _Find(Node* root, const T& x)//找节点x的调用函数
	{
		if (root == NULL)
			return NULL;
		if (root->_data == x)
			return root;
		Node* tmp = _Find(root->_leftchild, x);
		if (tmp)
			return tmp;
		return _Find(root->_rightchild, x);
	}
	size_t _GetKLevel(Node* root,size_t k)//第k层节点的调用函数
	{
		if (root == NULL)
			return 0;
		if (k == 1)
		{
			return 1;
		}
		return _GetKLevel(root->_leftchild, k - 1) + _GetKLevel(root->_rightchild, k - 1);
	}
	size_t _LeafSize(Node* root)//叶子节点的调用函数
	{
		if (root == NULL)
			return 0;
		if (root->_leftchild == NULL && root->_rightchild == NULL)
		{
			return 1;
		}
		return _LeafSize(root->_leftchild) + _LeafSize(root->_rightchild);
	}
	size_t _Depth(Node* root)//树的深度调用函数
	{
		if (root == NULL)
			return 0;
		int left = _Depth(root->_leftchild);
		int right = _Depth(root->_rightchild);
		return left > right ? left + 1: right + 1;
	}
	size_t _Size(Node* root)//节点的个数
	{
		if (root == NULL)
			return 0;
		return (_Size(root->_leftchild) + _Size(root->_rightchild))+1;
	}
	void _PostOrder(Node* root)//后序遍历调用函数
	{
		if (root == NULL)
			return;
		_PostOrder(root->_leftchild);
		_PostOrder(root->_rightchild);
		cout << root->_data << " ";
	}
	void _InOrder(Node* root)//中序遍历的调用函数
	{
		if (root == NULL)
			return;
		_InOrder(root->_leftchild);
		cout << root->_data << " ";
		_InOrder(root->_rightchild);
	}
	void _PrevOrder(Node* root)//前序遍历的调用函数
	{
		if (root == NULL)
			return;
		cout << root->_data << " ";
		_PrevOrder(root->_leftchild);
		_PrevOrder(root->_rightchild);
	}
	Node* _GreateTree(T* a, size_t n, const T& invalid, size_t& index)//构造函数的调用函数
	{
		Node* root = NULL;
		if ((index< n) && (a[index] != invalid))
		{
			root = new Node(a[index]);
			root->_leftchild = _GreateTree(a, n, invalid, ++index);
			root->_rightchild = _GreateTree(a, n, invalid, ++index);
		}
		return root;
	}
protected:
	Node* _root;
};

void BinaryTreeTest()//测试
{
	int a[] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6 };
	BinaryTree<int> t1(a, sizeof(a) / sizeof(a[0]), '#');
	t1.PrevOrder();
	t1.LeveIOrder();
	t1.InOrder();
	t1.PostOrder();
	cout << "节点的个数:" << t1.Size() << endl;
	cout << "深度:" << t1.Depth() << endl;
	cout << "叶子节点的个数:" << t1.LeafSize() << endl;
	cout << "第k层:" << t1.GetKLevel(2) << endl;
	BinaryTree<int> t2(t1);
	t2.PostOrder();
	BinaryTree<int> t3;
	t3 = t1;
}

int main()
{
	BinaryTreeTest();
	return 0;
}
    原文作者:simplehap
    原文地址: https://blog.csdn.net/simplehap/article/details/63259845
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞