标签:二叉树
二叉树:二叉树是一棵特殊的树,二叉树每个节点最多有两个孩子结点,分别称为左孩子和右孩子。
满二叉树:高度为N的满二叉树有2^N - 1个节点的二叉树。
完全二叉树: 若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树
二叉树的4种遍历方式
前序遍历(先根遍历):(1):先访问根节点; (2):前序访问左子树;(3):前序访问右子树;
中序遍历: (1):中序访问左子树;(2):访问根节点; (3):中序访问右子树;
后序遍历(后根遍历):(1):后序访问左子树;(2):后序访问右子树;(3):访问根节点;
层序遍历: (1):一层层节点依次遍历。
#pragma once
#include<iostream>
#include<queue>
using namespace std;
template<class T>
struct BinaryTreeNode
{
T _data;
BinaryTreeNode* _left;
BinaryTreeNode* _right;
BinaryTreeNode(const T& x)
:_data(x)
, _left(NULL)
, _right(NULL)
{}
};
template<class T>
class BinaryTree
{
protected:
BinaryTreeNode<T>* _root;
protected:
//先序遍历产生二叉树
BinaryTreeNode<T>* _CreateBinaryTree(T* arr, size_t &index, size_t size)
{
BinaryTreeNode<T>* root = NULL;
if (index < size&&arr[index] != ‘#‘)
{
root = new BinaryTreeNode<T>(arr[index]);
root->_left = _CreateBinaryTree(arr, ++index, size);
root->_right = _CreateBinaryTree(arr, ++index, size);
}
return root;
}
void _PreOrder(BinaryTreeNode<T>* root)
{
if (root != NULL)
{
cout << root->_data << " ";
_PreOrder(root->_left);
_PreOrder(root->_right);
}
return;
}
void _InOrder(BinaryTreeNode<T>* root)
{
if (root != NULL)
{
_InOrder(root->_left);
cout << root->_data<<" ";
_InOrder(root->_right);
}
return;
}void _PostOrder(BinaryTreeNode<T>* root)
{
if (root != NULL)
{
_PostOrder(root->_left);
_PostOrder(root->_right);
cout << root->_data << " ";
}
return;
}
void _Clear(BinaryTreeNode<T>* root)
{
if (root)
{
_Clear(root->_left);
_Clear(root->_right);
delete root;
}
}
int _Size(BinaryTreeNode<T>* root)
{
int size = 0;
if (root)
{
size += (_Size(root->_left)+1);
size += _Size(root->_right);
}
return size;
}
int _Depth(BinaryTreeNode<T>* root)
{
int depth = 0;
if (root)
{
int leftDepth = _Depth(root->_left);
int rightDepth = _Depth(root->_right);
depth = leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
}
return depth;
}
public:
BinaryTree()
:_root(NULL)
{}
BinaryTree(T* a, size_t size)
{
size_t index = 0;
_root = _CreateBinaryTree(a, index, size);
}
~BinaryTree()
{
_Clear(_root);
}
void PreOrder()
{
_PreOrder(_root);
cout << endl;
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
void PostOrder()
{
_PostOrder(_root);
cout << endl;
}
void LevelOrder()
{
queue<BinaryTreeNode<T>*> q;
q.push(_root);
while (!q.empty())
{
if (q.front())
{
cout << q.front()->_data << " ";
if (q.front()->_left)
q.push(q.front()->_left);
if(q.front()->_right)
q.push(q.front()->_right);
q.pop();
}
}
cout << endl;
}
int Size()
{
return _Size(_root);
}
int Depth()
{
return _Depth(_root);
}
};
#include"BinaryTree.h"
void Test1()
{
BinaryTree<int> b1;
int array[10] = { 1, 2, 3, ‘#‘, ‘#‘, 4, ‘#‘, ‘#‘, 5, 6 };
BinaryTree<int> b2(array, 10);
b1.PreOrder();
b2.PreOrder();
b2.InOrder();
b2.PostOrder();
b2.LevelOrder();
cout << b1.Size() << " " << b2.Size() << endl;
cout << b1.Depth() << " " << b2.Depth() << endl;
}
int main()
{
Test1();
system("pause");
return 0;
}本文出自 “小止” 博客,请务必保留此出处http://10541556.blog.51cto.com/10531556/1748098
标签:二叉树
原文地址:http://10541556.blog.51cto.com/10531556/1748098