二叉树链式存储相关基本算法的递归实现

二叉树链式存储相关基本算法的递归实现。
语言:C++

  1. 创建
  2. 括号表示法输出
  3. 查找、查找并取得节点的深度、取得查找节点的深度
  4. 取左子树、取右子树
  5. 求树的最大深度
  6. 销毁树
  7. 前序遍历、中序遍历、后序遍历
  8. 求全部节点数
  9. 输出叶子结点
  10. 求特定深度的节点数量
  11. 两棵树是否相似
  12. 输出节点的所有祖先
#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;
}

参考:数据结构教程 第五版

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值