二叉树链式存储相关基本算法的递归实现。
语言:C++
- 创建
- 括号表示法输出
- 查找、查找并取得节点的深度、取得查找节点的深度
- 取左子树、取右子树
- 求树的最大深度
- 销毁树
- 前序遍历、中序遍历、后序遍历
- 求全部节点数
- 输出叶子结点
- 求特定深度的节点数量
- 两棵树是否相似
- 输出节点的所有祖先
#include <iostream>
#include <algorithm>
using namespace std;
typedef char ElemType;
struct BTNode {
ElemType data;
BTNode* lchild;
BTNode* rchild;
BTNode() {data = '\0'; lchild = rchild = NULL;}
BTNode(ElemType e): data(e) {
lchild = rchild = NULL;
}
};
// A, A(B), A(,B), A(B,C)
void fCreateTree_(BTNode*& t, ElemType* s, int& n) {
if (s[n] == '\0') return;
t = new BTNode(s[n++]);
if (s[n] == '(') {
n++;
if (s[n] != ',') {
fCreateTree_(t->lchild, s, n);
}
if (s[n] == ',') {
n++;
fCreateTree_(t->rchild, s, n);
}
n++;
}
}
inline BTNode* CreateTree(BTNode*& t, ElemType* s) {
int n = 0; //s[n]
t = NULL;
fCreateTree_(t, s, n);
return t;
}
void DispTree(BTNode* t) {
if (t == NULL) return;
cout << t->data;
if (t->lchild != NULL || t->rchild!=NULL) {
cout << '(';
if (t->lchild != NULL)
DispTree(t->lchild);
if (t->rchild != NULL) {
cout << ',';
DispTree(t->rchild);
}
cout << ')';
}
}
BTNode* FindNode(BTNode* t, ElemType x) {
if (t == NULL) return NULL;
if (t->data == x) return t;
BTNode* p = FindNode(t->lchild, x);
if (p != NULL) return p;
return FindNode(t->rchild, x);
}
BTNode* FindNode(BTNode* t, ElemType x, int& h) {
h = 0; // Output: Node Level
if (t == NULL) return NULL;
if (t->data == x) {h++; return t;}
BTNode* p = FindNode(t->lchild, x, h);
if (p != NULL) {h++;return p;}
p = FindNode(t->rchild, x, h);
if (p != NULL) {h++;return p;}
return NULL;
}
int FindNodeHeight(BTNode* t, ElemType x) {
if (t == NULL) return 0;
if (t->data == x) return 1;
int ans = 0;
if (t->lchild != NULL)
ans = FindNodeHeight(t->lchild, x);
if (ans != 0) return ans + 1;
if (t->rchild != NULL)
ans = FindNodeHeight(t->rchild, x);
if (ans != 0) return ans + 1;
return 0;
}
inline BTNode* Lchild(BTNode* t) {
return t->lchild;
}
inline BTNode* Rchild(BTNode* t) {
return t->rchild;
}
int TreeHeight(BTNode* t) {
if (t == NULL) return 0;
return max(TreeHeight(t->lchild), TreeHeight(t->rchild)) + 1;
}
void Destroy(BTNode*& t) {
if (t == NULL) return;
Destroy(t->lchild);
Destroy(t->rchild);
delete t;
}
void PreOrder(BTNode* t) {
if (t == NULL) return;
cout << t->data;
PreOrder(t->lchild);
PreOrder(t->rchild);
}
void InOrder(BTNode* t) {
if (t == NULL) return;
InOrder(t->lchild);
cout << t->data;
InOrder(t->rchild);
}
void PostOrder(BTNode* t) {
if (t == NULL) return;
PostOrder(t->lchild);
PostOrder(t->rchild);
cout << t->data;
}
int CountNode(BTNode* t) {
if (t == NULL) return 0;
return CountNode(t->lchild) + CountNode(t->rchild) + 1;
}
void DispLeaf(BTNode* t) {
if (t == NULL) return;
if (t->lchild == NULL && t->rchild == NULL)
cout << t->data;
DispLeaf(t->lchild);
DispLeaf(t->rchild);
}
int CountLevel(BTNode* t, int h, int k=1) {
// Counts Node at Level h.
if (t == NULL) return 0;
if (k == h) return 1;
if (k > h) return 0;
return CountLevel(t->lchild, h, k+1) + CountLevel(t->rchild, h, k+1);
}
bool IsSimilar(BTNode* a, BTNode* b) {
if (a == NULL && b == NULL)
return true;
if (a == NULL || b == NULL)
return false;
return IsSimilar(a->lchild, b->lchild) && IsSimilar(a->rchild, b->rchild);
}
bool DispAncestor(BTNode* t, ElemType x) {
if (t == NULL) return false;
if (t->data == x) return true;
if (t->lchild != NULL)
if (DispAncestor(t->lchild, x)) {
cout << t->data;
return true;
}
if (t->rchild != NULL)
if (DispAncestor(t->rchild, x)) {
cout << t->data;
return true;
}
return false;
}
int main() {
ElemType str[] = "A(B(,C(,D)),E(F,G(H(,I))))";
BTNode* pt = NULL;
CreateTree(pt, str);
DispTree(pt);
cout << endl;
cout << "Height: " << TreeHeight(pt) << endl;
cout << "Enter a char to Find:";
ElemType e;
cin >> e;
int h;
BTNode* pp = FindNode(pt, e, h);
if (pp != NULL) {
cout << "Child: ";
DispTree(pp);
cout << endl;
cout << "Child Level: " << h;
cout << endl;
cout << "Left child: ";
if (Lchild(pp))
DispTree(Lchild(pp)) ;
else
cout << "None";
cout << endl;
cout << "Right child: ";
if (Rchild(pp))
DispTree(Rchild(pp)) ;
else
cout << "None";
cout << endl;
} else {
cout << "Not Found." << endl;
}
PreOrder(pt);
cout << endl;
InOrder(pt);
cout << endl;
PostOrder(pt);
cout << endl;
cout << CountNode(pt) << endl;
DispLeaf(pt);
cout << endl;
cout << CountLevel(pt, 3) << endl;
if (IsSimilar(FindNode(pt, 'C'), FindNode(pt, 'H')))
cout << "Is similar. " << endl;
else
cout << "Not similar. " << endl;
DispAncestor(pt, 'H');
cout << endl;
Destroy(pt);
return 0;
}
参考:数据结构教程 第五版