1.幾個易混淆的概念
樹:節點的有限結合。如下:
二叉樹:所有結點的度都小於等於2的樹。如下:
完全二叉樹:葉子節點只能出現在最下層和次下層,並且最下面一層的結點都集中在該層最左邊的若干位置的二叉樹。如下:
完全二叉樹的特性
完全二叉樹是效率很高的數據結構,具有廣泛的應用。
完全二叉樹是一種特殊的二叉樹,每一個元素與其子節點(孩子)有非常好的對應關係。對於編號爲i的元素,有如下關係:
(1)若i>1,則該元素的父節點爲[i/2]
(2)若 2i <= n,則左孩子的編號爲2i
(3)若2i+1 <= n ,則右孩子的編號爲2i+1
滿二叉樹:一個二叉樹,如果每一個層的結點數都達到最大值,則這個二叉樹就是滿二叉樹。也就是說,如果一個二叉樹的層數爲K,且結點總數是(2^k) -1 ,則它就是滿二叉樹。如下:
包含關係:樹 > 二叉樹 > 完全二叉樹 > 滿二叉樹
2.二叉樹的遍歷方式
3. 二叉樹的數組描述(編碼)
編碼中由於將根節點索引設爲0,故此例中節點索引與第一節中滿二叉樹的節點關係特性稍微有所變化。
數組描述較爲簡單,當然功能也單一。主要就是建立一個一維數組,數組下標代表節點索引。
Tree.h
#ifndef TREE_H
#define TREE_H
class Tree
{
public:
Tree(int size,int *pRoot);//創建樹
~Tree();//銷燬樹
int *SearchNode(int nodeIndex);//根據索引尋找節點
bool AddNode(int nodeIndex, int direction, int *pNode);//添加節點
bool DeleteNode(int nodeIndex, int *pNode); //刪除節點
void TreeTraverse();//遍歷節點
private:
int *m_pTree;
int m_iSize;
};
#endif
Tree.cpp
#include"Tree.h"
#include<iostream>
using namespace std;
Tree::Tree(int size, int *pRoot)
{
m_iSize = size;
m_pTree = new int[size];
for (int i = 0; i < size; i++)
{
m_pTree[i] = 0;
}
m_pTree[0] = *pRoot;
}
Tree::~Tree()
{
delete[]m_pTree;
m_pTree = NULL;
}
int *Tree::SearchNode(int nodeindex)
{
if (nodeindex < 0 || nodeindex >= m_iSize)
{
return NULL;
}
if (m_pTree[nodeindex] == 0)
{
return NULL;
}
return &m_pTree[nodeindex];//由索引取值後取地址
}
bool Tree::AddNode(int nodeindex, int direction, int *pNode)
// direction決定往左插,還是往右插。
{
if (nodeindex < 0 || nodeindex >= m_iSize || m_pTree[nodeindex] == 0)
//節點合法性
{
return false;
}
switch (direction)
{
// 0值定義爲左孩子
case 0:
// 不等於0,說明插入過了,這裏我們的處理是,如果插過了,不允許替換。
if (nodeindex * 2 + 1 >= m_iSize || m_pTree[nodeindex * 2 + 1] != 0)
{
return false;
}
m_pTree[nodeindex * 2 + 1] = *pNode;
break;
case 1:
if (nodeindex * 2 + 2 >= m_iSize || m_pTree[nodeindex * 2 + 2] != 0)
{
return false;
}
m_pTree[nodeindex * 2 + 2] = *pNode;
break;
}
return true;
}
bool Tree::DeleteNode(int nodeindex, int * pNode)
{
if (nodeindex < 0 || nodeindex >= m_iSize)
{
return false;
}
if (m_pTree[nodeindex] == 0)
{
return false;
}
*pNode = m_pTree[nodeindex];
m_pTree[nodeindex] = 0;
return true;
}
void Tree::TreeTraverse()
{
for (int i =0;i<m_iSize;i++)
{
cout << m_pTree[i] << " ";
}
}
在主函數中測試
main.cpp
#include <iostream>
#include <stdlib.h>
#include "Tree.h"
using namespace std;
/********************************************************************
數組---樹 Tree【】=3 5 8 2 6 9 7
3(0) 左孩子小標=父節點下標*2+1
5(1) 8(2) 右孩子小標=父節點下標*2+2
2(3) 6(4) 9(5) 7(6)
**********************************************************************/
int main()
{
int root = 3;
Tree *pTree = new Tree(10, &root);
int node1 = 5;
int node2 = 8;
//0號節點插入左孩子。
pTree->AddNode(0, 0, &node1);
//0號節點插入右孩子。
pTree->AddNode(0, 1, &node2);
int node3 = 2;
int node4 = 6;
int node5 = 9;
int node6 = 7;
pTree->AddNode(1, 0, &node3);
pTree->AddNode(1, 1, &node4);
pTree->AddNode(2, 0, &node5);
pTree->AddNode(2, 1, &node6);
pTree->TreeTraverse();
// 傳入節點值,找到index
int *p = pTree->SearchNode(2);
cout << endl;
cout << "***************"<<endl;
cout << "index:" << *p<<endl;
cout << "***************"<<endl;
// 刪除傳入index
int temp;
pTree->DeleteNode(6, &temp);
cout << endl;
cout << "delete node=" << temp << endl;
pTree->TreeTraverse();
cout << endl;
delete pTree;
pTree = NULL;
return 0;
}
運行結果:
4. 二叉樹的鏈表描述(編碼)
結點要素:索引、數據、左孩子指針、右孩子指針
Node.h
#ifndef NODE_H
#define NODE_H
class Node
{
public:
Node();
Node *SearchNode(int nodeIndex);
void DeleteNode();
void PreorderTraversal(); // 前序遍歷
void InorderTraversal(); // 中序遍歷
void PostorderTraversal(); // 後序遍歷
int index; //索引
int data; // 數據
Node *pLChild; // 左孩子指針
Node *pRChild; // 右孩子指針
Node *pParent; // 父節點指針
};
#endif
Node.cpp
#include "Node.h"
#include <iostream>
using namespace std;
Node::Node()
{
index = 0;
data = 0;
pLChild = NULL;
pRChild = NULL;
pParent = NULL;
}
/*
*
*/
Node *Node::SearchNode(int nodeIndex)
{
// 看自己是不是
if (this->index == nodeIndex)
{
return this;
}
// 左右節點是不是
Node *temp = NULL;
if (this->pLChild != NULL)
{
if (this->pLChild->index == nodeIndex)
{
return this->pLChild;
}
// 注意沒找到的情況繼續往下找
else
{
temp = this->pLChild->SearchNode(nodeIndex);
if (temp != NULL)
{
return temp;
}
}
}
if (this->pRChild != NULL)
{
if (this->pRChild->index == nodeIndex)
{
return this->pRChild;
}
// 注意沒找到的情況繼續往下找
else
{
temp = this->pRChild->SearchNode(nodeIndex);
if (temp != NULL)
{
return temp;
}
}
}
return NULL;
}
void Node::DeleteNode()
{
if (this->pLChild != NULL)
{
this->pLChild->DeleteNode();
}
if (this->pRChild != NULL)
{
this->pRChild->DeleteNode();
}
if (this->pParent != NULL)
{
// 判斷自己是父節點的左孩子還是右孩子。
if (this->pParent->pLChild == this)
{
this->pParent->pLChild = NULL;
}
if (this->pParent->pRChild == this)
{
this->pParent->pRChild = NULL;
}
}
// 自殺
delete this;
}
//先自己,然後左邊然後右邊
void Node::PreorderTraversal()
{
cout << this->index << " " << this->data << endl;
if (this->pLChild != NULL)
{
this->pLChild->PreorderTraversal();
}
if (this->pRChild != NULL)
{
this->pRChild->PreorderTraversal();
}
}
void Node::InorderTraversal()
{
if (this->pLChild != NULL)
{
this->pLChild->InorderTraversal();
}
cout << this->index << " " << this->data << endl;
if (this->pRChild != NULL)
{
this->pRChild->InorderTraversal();
}
}
void Node::PostorderTraversal()
//後序遍歷
{
if (this->pLChild != NULL)
{
this->pLChild->PostorderTraversal();
}
if (this->pRChild != NULL)
{
this->pRChild->PostorderTraversal();
}
cout << this->index << " " << this->data << endl;
}
Tree.h
#ifndef TREE_H
#define TREE_H
#include "Node.h"
class Tree
{
public:
Tree(); //創建樹
~Tree();//銷燬樹 del node 刪除最根節點即可
Node *SearchNode(int nodeIndex); //搜索節點
bool AddNode(int nodeIndex, int direction, Node *pNode);//搜索節點基礎上添加
bool DeleteNode(int nodeIndex, Node *pNode);//刪除結點
void PreorderTraversal(); //前序遍歷
void InorderTraversal(); //中序遍歷
void PostorderTraversal(); //後序遍歷
private:
Node *m_pRoot;
};
#endif
Tree.cpp:
#include "Tree.h"
#include <iostream>
using namespace std;
Tree::Tree()
{
m_pRoot = new Node();// 第一個節點
}
Tree::~Tree()
{
DeleteNode(0, NULL);
//m_pRoot->DeleteNode();
}
//刪除結點爲頭結點。整棵樹銷燬
//刪除和添加都需要找到節點
Node *Tree::SearchNode(int nodeIndex)
{
return m_pRoot->SearchNode(nodeIndex);
}
bool Tree::AddNode(int nodeIndex, int direction, Node *pnode) {
Node *temp = SearchNode(nodeIndex);
// 掛載節點不存在
if (temp == NULL)
{
return false;
}
Node *node = new Node();
if (node == NULL)
{
return false;
}
// 要把pnode值保存下來。
node->index = pnode->index;
node->data = pnode->data;
node->pParent = temp; //注意,要在添加時把父節點也記着登記上。
//0掛左,1掛右
if (direction == 0)
{
temp->pLChild = node;
}
if (direction == 1)
{
temp->pRChild = node;
}
return true;
};
// 刪除節點(級聯刪除子節點)& 析構函數
bool Tree::DeleteNode(int nodeIndex, Node *pNode)
{
Node *temp = SearchNode(nodeIndex);
if (temp == NULL)
{
return false;
}
if (pNode != NULL)
{
pNode->data = temp->data;
}
// 把樹中刪除節點的操作下移到node中來解決
temp->DeleteNode();
return true;
}
void Tree::PreorderTraversal()
{
m_pRoot->PreorderTraversal();
}
void Tree::InorderTraversal()
{
m_pRoot->InorderTraversal();
}
void Tree::PostorderTraversal()
{
m_pRoot->PostorderTraversal();
}
5.參考
《慕課網》
Programming-Notebook