一、二叉樹的相關概念
概念:二叉樹是n個結點的有限集合,該集合或者為空集(空二叉樹),或者由一個根結點和兩顆互不相交的,分別稱為根結點的的左子樹和右子樹的二叉樹組成。
理解:二叉樹是一個包含遞歸概念的樹,每個結點下面的左子樹和右子樹依舊包含二叉樹。
二叉樹的特點:
- 每個結點最多有兩顆子樹。
- 左子樹和右子樹順序不能顛倒。
二叉樹的形態:
- n=0,空樹。
- n=1,只有一個根結點。
- 根節點只有左子樹。
- 根結點只有右子樹。
- 左子樹和右子樹都有。
特殊二叉樹:
- 滿二叉樹:所有分支節點都存在左子樹和右子樹,并且所有葉子都在同一層上。
- 完全二叉樹: 給一個具有n個結點的二叉樹編號(從左到右),如果這n個結點是滿二叉樹的前n個結點(中間無空隙),則可以稱之為完全二叉樹。
完全二叉樹的特點:
- 葉子節點只能出現在在最下兩層。
- 最下層的葉子一定集中在左部連續位置。
- 倒數二層,若有葉子節點,一定都在右部連續位置。
- 如果結點度為1,則一定為左孩子。
- 同樣結點的二叉樹,完全二叉樹的深度最小。
二叉樹的性質:
1)在二叉樹的第i層上至多有2i-1個結點。
2)深度為k的二叉樹至多有2k-1個結點。
3)對任何一顆二叉樹T,如果其終端結點數為n0,度為2的結點數為n2,則n0=n2+1。
4)具有n個結點的完全二叉樹的深度為[log2n]+1(方括號為向下取整函數)。
5)如果對一顆有n個結點的完全二叉樹(其深度為[log2n]+1)的結點按層序編號(從第1層到第[log2n]+1層,每層從左到右),對任一結點i(1<=i<=n)有:
- 如果i=1,則結點i是二叉樹的根,無雙親;如果i>1,則其雙親是結點[i/2]。(括號含義同上)
- 如果2i>n,則結點i無左孩子(結點i為葉子結點);否則其左孩子是結點2i。
- 如果2i+1>n,則結點i無右孩子;否則其右孩子是結點2i+1。
二、二叉樹遍歷代碼實現
//Binarytree.h
#ifndef BINARYTREE_H_
#define BINARYTREE_H_
struct BtreeNode
{
char alpha;
BtreeNode * lchild;
BtreeNode * rchild;
};
class BinaryTree
{
private:
BtreeNode * root;//樹的根或子樹的根
public:
BinaryTree();
~BinaryTree();
BtreeNode * Create();
BtreeNode * Getroot();
void Release(BtreeNode * root);
void PreOrder(BtreeNode * root);//前序遍歷
void InOrder(BtreeNode * root);//中序遍歷
void PostOrder(BtreeNode * root);//后序遍歷
void LayerOrder(BtreeNode * root);//層序遍歷
int BinaryTreeNodeNum();//返回二叉樹的結點數
int BinaryTreeLeaves();//返回二叉樹的葉子結點數
int Depth();
};
#endif
//Binarytree.cpp
#include<iostream>
#include"BinaryTree.h"
using std::cout;
using std::endl;
using std::cin;
BinaryTree::BinaryTree()
{
this->root = Create();
}
BinaryTree::~BinaryTree()
{
this->Release(root);
}
BtreeNode * BinaryTree::Create()//創建過程為順序遍歷。
{
BtreeNode * root;
char ch;
cout << "輸入一個字符:";
cin >> ch;
if (ch == '#')
root = NULL;//空指針并沒有內存,加一個#是為了在電腦上讓二叉樹返回。畢竟有的二叉樹會缺少lchild或rchild。
else
{
root = new BtreeNode;
root->alpha = ch;
root->lchild = Create();
root->rchild = Create();
}
return root;
}
BtreeNode * BinaryTree::Getroot()
{
return this->root;
}
void BinaryTree::Release(BtreeNode * root)//后序遍歷
{
if(root!=NULL)
{
Release(root->lchild);
Release(root->rchild);
delete root;
}
}
void BinaryTree::PreOrder(BtreeNode * root)
{
if (root == NULL)
return;
cout << root->alpha << " ";
PreOrder(root->lchild);
PreOrder(root->rchild);
}
void BinaryTree::InOrder(BtreeNode * root)
{
if (root == NULL)
return;
InOrder(root->lchild);
cout << root->alpha << " ";
InOrder(root->rchild);
}
void BinaryTree::PostOrder(BtreeNode * root)
{
if (root == NULL)
return;
PostOrder(root->lchild);
PostOrder(root->rchild);
cout << root->alpha << " ";;
}
void BinaryTree::LayerOrder(BtreeNode * root)
{
const int Maxsize = 100;
int front = 0;
int rear = 0;
BtreeNode * Q[Maxsize];
BtreeNode * q;
if (root == NULL)
return;
else
{
Q[rear++] = root;
while (front!=rear)
{
q = Q[front++];
cout << q->alpha << " ";
if(q->lchild!=NULL)
Q[rear++] = q->lchild;
if(q->rchild!=NULL)
Q[rear++] = q->rchild;
}
}
}
//設計一個結點數組Q,用來分層保存二叉樹中的結點;再設計一個臨時結點q,用來打印當前結點。
//Q[front]賦值給q,表示當前要打印的結點數據;Q[rear]表示目前層序遍歷的最后一個結點數據。
//當front==rear時,表示已經打印到了最后一個。、
//相當于把根結點的子樹從左到右遍歷,再把左子樹從左到右遍歷,再把右子樹從左到右遍歷,從而達到層序遍歷。
//main.cpp
#include<iostream>
#include"BinaryTree.h"
using namespace std;
int main()
{
BinaryTree Btree;
BtreeNode * root = Btree.Getroot();
cout << "前序遍歷: " << endl;
Btree.PreOrder(root);
cout << endl;
cout << "中序遍歷: " << endl;
Btree.InOrder(root);
cout << endl;
cout << "后序遍歷: " << endl;
Btree.PostOrder(root);
cout << endl;
cout << "層序遍歷: " << endl;
Btree.LayerOrder(root);
cout << endl;
return 0;
}