碎碎念(可以直接跳过):这大概是数据结构复习到现在我写过的最复杂的类定义,看《数据结构应用与算法》到二叉树这里时,一直疑惑函数指针是干什么的,为什么类中要用到函数指针,为什么同一遍历方法要写两遍,一遍私有一遍公有,私有的函数方法为什么要定义成静态函数。二叉树这里一半时间都在研究这个了,因为书上的参考代码肯定是优美、通用且精简,我不理解很正常,当初上课时也没有好好想这个问题。我对比了书上的代码和课程PPT上的代码,找了函数指针和一些资料,大概明白了为什么这样写。
首先看函数指针:
函数指针
函数指针:意义为指向函数的指针,通过使用函数指针,可以让代码结构更灵活。比如在调用某个类的方法时有函数指针参数,那么你就可以自己写个格式与函数指针相同的函数传进去,那么类里只要涉及到该函数的地方就会用你刚刚写的方法实现。
举例:void (*thevisit) (binaryTreeNode *t);
表示返回值类型为void,参数类型为binaryTreeNode *,名为thevisit的函数指针。
函数指针简单实现:
这里实现了三个简单函数,一个函数指针,并设置foo2()函数的参数设置为函数指针。通过对函数指针的不同赋值,foo2()函数可以有不同实现,函数调用变得更灵活。
#include <stdio.h>
void foo0();
void foo1();
void foo2(void (*p)());
int main() {
void (*point)();
point = foo0;
foo2(point);
point = foo1;
foo2(point);
return 0;
}
void foo0() {
printf("冻冻,冻冻,我是冻拐,我是冻拐!\n");
}
void foo1() {
printf("冻冻,冻冻,我是冻腰,我是冻腰!\n");
}
void foo2(void (*p)())
{
p();
printf("我是冻冻,我是冻冻,收到,收到!\n\n");
}
运行结果:
二叉树
二叉树的性质
- 满二叉树:满二叉树的节点数为2^(h)+1,第i层的节点数为2 ^(h-1) +1
- 完全二叉树:最后一行不满的满二叉树(完全二叉树存在的结点与慢二叉树一一对应)
- 结点数 = 边数 + 1
- 结点数 = 分支数 + 1
- n = n0 + n1 + n2 = n1 + 2*n2 + 1 (n0表示没有分支的结点,n1表示有一个分支的结点,n2表示分支为2的结点,n为结点总数)
- 非空二叉树上的叶子结点数等于度为2的结点数+1,n0 = n2 + 1
二叉树实现代码
准备工作完成之后开始上代码,碎碎念中的疑问都有相应注释
定义部分:
/**
二叉树的基本结构,基本操作,递归遍历和非递归遍历
**/
// 树的节点类
template <class T>
class BinaryTreeNode
{
public:
BinaryTreeNode(){leftChild = null; rightChild = null;}
BinaryTreeNode(const T& theData){ data = theData;}
BinaryTreeNode(const T& theData, BinaryTreeNode* L, BinaryTreeNode* R)
{
data = theData;
leftChild = L;
rightChild = R;
}
private:
T data;
BinaryTreeNode<T>* leftChild;
BinaryTreeNode<T>* rightChild;
};
// 二叉树类
template <class T>
class BinaryTree
{
public:
BinaryTree(){root = null; treeSize = 0;}
~BinaryTree(){erase();}
bool empty() const {return treeSize == 0;}
int size() const {return treeSize;}
void makeTree(const T& element, BinaryTree<T>& leftChild,BinaryTree<T>& rightChild);
void breakTree(T& element, BinaryTree<T>& leftChild, BinaryTree<T>& rightChild);
/*
公有遍历函数,参数为函数指针,接收函数指针后将值赋给私有变量visit
并调用私有遍历函数
*/
void preOrder(void (*theVisit)(BinaryTreeNode<E>*))
{
visit = theVisit; preOrder(root);
}
void inOrder(void (*theVisit)(BinaryTreeNode<E>*))
{
visit = theVisit; inOrder(root);
}
void postOrder(void (*theVisit)(BinaryTreeNode<E>*))
{
visit = theVisit; postOrder(root);
}
void levelOrder(void (*theVisit)(BinaryTreeNode<E>*))
{
visit = theVisit; levelOrder(root);
}
void erase()
{
postOrder(dispose);
root = null;
treeSize = 0;
}
private:
BinaryTreeNode<T>* root; // 根节点指针
int treeSize;
/*
静态函数指针,在公有遍历方法中被赋值,声明为静态时因为
该指针属于二叉树类,而不是二叉树对象。被赋值后该函数指针
应该并且可以持续存在。
*/
static void (*visit)(binaryTreeNode<E>*t);
/*
私有遍历方法,接受二叉树节点指针作为参数,真正的遍历方法
在这里实现。声明为私有是因为该方法只被类内的函数调用,不需要
对类外暴露。声明为静态有两个原因,一是遍历函数只应访问静态变量
visit,不需访问其它变量,而声明为静态后便不可以访问非静态变量;
第二,私有遍历方法也应属于类,而不是特定对象。
*/
static void preOrder(binaryTreeNode<E>* t);
static void inOrder(binaryTreeNode<E>* t);
static void postOrder(binaryTreeNode<E>* t);
static void levelOrder(BinaryTreeNode<E>* t);
static dispose(BinaryTreeNode<E>* t){delete t};
};
为什么遍历方法要分公有和私有?
其实我看了多种实现版本,只有参考书和PPT上为两种不相同的函数指针实现,其它也有很多直接写遍历方法,写死visit函数的版本。我认为最大的区别就是程序的灵活性和封装性。
灵活性:利用函数指针,让调用者自己实现visit函数。
封装性:分公有和私有遍历方法,私有遍历方法的具体实现对类外不可见,不可被重载修改。
函数实现:
- makeTree()和breakTree()
- 递归实现
- 非递归实现
合成树与分解树
// 将左右孩子和element合并成一个新树
template <class T>
void BinaryTree<T> :: makeTree(const T& element, BinaryTree<T>& leftChild, BinaryTree& rightChild)
{
root = new BinaryTreeNode<T>(element, leftChild, rightChild);
//阻止访问左右孩子跟节点
left.root = right.root = NULL;
}
//将一颗树分成左孩子和右孩子并保存根节点元素
template <class T>
void BinaryTree<T> :: breakTree(T& element, BinaryTree<T>& leftChild, BinaryTree& rightChild)
{
if(!root)
throw BadInput();
element = root->data;
leftChild = root->leftChild;
rightChild = root->rightChild;
delete root;
root = NULL;
}
递归实现
// 遍历方法递归实现
template<class E>
void BinaryTree<E> :: preOrder(BinaryTreeNode<E> *t)
{
if(t!=null)
{
BinaryTree<E> :: visit(t);
preOrder(t->leftChild);
preOrder(t->rightChild);
}
}
template<class E>
void BinaryTree<E> :: inOrder(BinaryTreeNode<E> *t)
{
if(t!=null)
{
inOrder(t->leftChild);
BinaryTree<E> :: visit(t);
inOrder(t->rightChild);
}
}
template<class E>
void BinaryTree<E> :: postOrder(BinaryTreeNode<E> *t)
{
if(t!=null)
{
postOrder(t->leftChild);
postOrder(t->rightChild);
BinaryTree<E> :: visit(t);
}
}
非递归方法:
前序、中序、后序遍历使用栈结构
层级遍历使用队列结构
//非递归方法
template<class E>
void BinaryTree<E> :: preOrder(BinaryTreeNode<E> *t)
{
Stack<BinaryTreeNode<E>*> s;
BinaryTreeNode<E>* p = t; // 遍历指针
while(p != null || !s.empty())
{
if(p != null)
{
BinaryTree<E> :: visit(p);
s.push(p);
p = p->leftChild;
}else
{
p = s.pop();
p = p->rightChild;
}
}
}
template<class E>
void BinaryTree<E> :: inOrder(BinaryTreeNode<E> *t)
{
Stack<BinaryTreeNode<E>*> s;
BinaryTreeNode<E>* p = t; // 遍历指针
while(p != null || !s.empty())
{
if(p != null)
{
s.push(p);
p = p->leftChild;
}else
{
p = s.pop();
BinaryTree<E> :: visit(p);
p = p->rightChild;
}
}
}
template<class E>
void BinaryTree<E> :: postOrder(BinaryTreeNode<E> *t)
{
Stack<BinaryTreeNode<E>*> s;
BinaryTreeNode<E>* p = t; // 遍历指针
BinaryTreeNode<E>* pre; //辅助指针
while(p != null || !s.empty())
{
if(p != null)
{
s.push(p);
p = p->leftChild;
}else
{
p = s.top();
if(p->rightChild != null && p->rightChild != pre)//右子树存在且未被访问过
{
p = p->rightChild;
s.push(p);
p = p->leftChild;
}else
{
s.pop();
BinaryTree<E> :: visit(p);
pre = p;
p = null;
}
}
}
}
template<class E>
void BinaryTree<E> :: levelOrder(BinaryTreeNode<E> *t)
{
Queue<BinaryTreeNode<E>*> q;
BinaryTreeNode<E>* p = t;
while(p != NULL)
{
BinaryTree<E> :: visit(p);
if(p->leftChild != null)
{
q.push(p->leftChild);
}
if(p->rightChild != null)
{
q.push(p->rightChild);
}
if(!q.empty())
{
t = q.top();
q.pop();
}
}
}
二叉树的数组描述
二叉树还可以用数组进行逻辑描述,根据映射公式来确定节点之间的关系。
优点:可以随机存取,不需要指针,空间利用率高。适用于完全二叉树和满二叉树。
缺点:一个n个元素的二叉树最多需要2^n个空间来存储,当二叉树缺少的元素很多时,会造成空间的极大浪费。