博客
关于我
二叉树的创建遍历应用删除
阅读量:379 次
发布时间:2019-03-04

本文共 3646 字,大约阅读时间需要 12 分钟。

???????????????????????????????????????????????????????????????

??????

?????

?????????????????????????????????????????????????

typedef struct BiNode {    char data;    struct BiNode* lchild;    struct BiNode* rchild;} BiNode;BiTree* createBiTree(BiNode* bt) {    char c;    cin >> c;    if (c == '#') {        bt = NULL;    } else {        bt = new BiNode;        bt->data = c;        bt->lchild = createBiTree(bt->lchild);        bt->rchild = createBiTree(bt->rchild);    }    return bt;}

?????

????????????????????C++?????????????????????

class BiTree {private:    struct Node {        char data;        Node* left;        Node* right;    };    Node* root;public:    BiTree() {        root = create(root);    }    void create(Node*& node) {        char c;        cin >> c;        if (c == '#') {            node = NULL;        } else {            node = new Node;            node->data = c;            create(node->left);            create(node->right);        }    }};

??????

????

???????????????????????????????????

void preOrder(BiTree* bt) {    if (bt == NULL) {        return;    } else {        cout << bt->root->data << " ";        preOrder(bt->root->left);        preOrder(bt->root->right);    }}

????

????????????????

void levelOrder(BiTree* root) {    if (root == NULL) {        return;    }    queue
q; q.enqueue(root->root); while (!q.empty()) { Node* current = q.front(); q.pop(); cout << current->data << " "; if (current->left != NULL) { q.enqueue(current->left); } if (current->right != NULL) { q.enqueue(current->right); } }}

??

????????

????????????????????

int getTreeDepth(BiTree* T) {    if (T == NULL) {        return 0;    } else {        int leftDepth = getTreeDepth(T->root->left);        int rightDepth = getTreeDepth(T->root->right);        return (leftDepth > rightDepth) ? (leftDepth + 1) : (rightDepth + 1);    }}

?????

??????????????

void copyTree(BiTree* T, BiTree*& NewT) {    if (T == NULL) {        NewT = NULL;        return;    } else {        NewT = new BiTree;        NewT->root->data = T->root->data;        copyTree(T->root->left, NewT->root->left);        copyTree(T->root->right, NewT->root->right);    }}

?????

?????????????

int nodeCount(BiTree* T) {    if (T == NULL) {        return 0;    } else {        return nodeCount(T->root->left) + nodeCount(T->root->right) + 1;    }}

???????

??????????????????

int getLeafNumber(BiTree* root) {    if (root == NULL) {        return 0;    } else if (root->root->left == NULL && root->root->right == NULL) {        return 1;    } else {        return getLeafNumber(root->root->left) + getLeafNumber(root->root->right);    }}

???1????

?????????????

int getDegreeOneNode(BiTree* T) {    if (T == NULL) {        return 0;    }    if ((T->root->left == NULL && T->root->right != NULL) ||         (T->root->left != NULL && T->root->right == NULL)) {        return 1 + getDegreeOneNode(T->root->left) + getDegreeOneNode(T->root->right);    } else {        return getDegreeOneNode(T->root->left) + getDegreeOneNode(T->root->right);    }}

???2????

??????????

int getDegreeTwoNode(BiTree* T) {    if (T == NULL) {        return 0;    }    if (T->root->left != NULL && T->root->right != NULL) {        return 1 + getDegreeTwoNode(T->root->left) + getDegreeTwoNode(T->root->right);    } else {        return getDegreeTwoNode(T->root->left) + getDegreeTwoNode(T->root->right);    }}

?????

?????????

void releaseTree(BiTree* T) {    if (T != NULL) {        releaseTree(T->root->left);        releaseTree(T->root->right);        delete T;    }}

???????????????????????????????????

转载地址:http://ttyg.baihongyu.com/

你可能感兴趣的文章
poj 1286 Necklace of Beads
查看>>
POJ 1321 棋盘问题
查看>>
poj 1321(回溯)
查看>>
Qt高级——Qt元对象系统源码解析
查看>>
qt调用vs2008编写的dll动态库(隐式调用)
查看>>
Qt读取注册表默认值
查看>>
poj 1679 判断MST是不是唯一的 (次小生成树)
查看>>
POJ 1703 Find them, Catch them
查看>>
POJ 1703 Find them, Catch them 并查集
查看>>
POJ 1738 An old Stone Game(石子合并)
查看>>
POJ 1740 A New Stone Game(博弈)题解
查看>>
Qt网络编程之实例二POST方式
查看>>
POJ 1765 November Rain
查看>>
poj 1860 Currency Exchange
查看>>
POJ 1961 Period
查看>>
POJ 2019 Cornfields (二维RMQ)
查看>>
poj 2057 The Lost House 贪心思想在动态规划上的应用
查看>>
poj 2057 树形DP,数学期望
查看>>
poj 2112 最优挤奶方案
查看>>
Qt编写自定义控件12-进度仪表盘
查看>>