博客
关于我
二叉树的创建遍历应用删除
阅读量: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/

你可能感兴趣的文章
Oracle 11g 操作ASM权限问题
查看>>
Oracle 11g 数据类型
查看>>
Oracle 11g 编译使用BBED
查看>>
oracle 11g 静默安装
查看>>
Oracle 11gR2学习之二(创建数据库及OEM管理篇)
查看>>
Oracle 11gR2构建RAC之(2)--配置共享存储
查看>>
Oracle 11g中的snapshot standby特性
查看>>
Oracle 11g关闭用户连接审计
查看>>
Oracle 11g忘记sys、system、scott密码该这样修改!
查看>>
Oracle 11g数据库安装和卸载教程
查看>>
Oracle 11g数据库成功安装创建详细步骤
查看>>
Oracle 11g超详细安装步骤
查看>>
Oracle 12c中的MGMTDB
查看>>
Oracle 12c安装报错Installation failed to access the temporary location(无法访问临时位置)...
查看>>
Oracle 9i数据库管理教程
查看>>
ORACLE Active dataguard 一个latch: row cache objects BUG
查看>>
oracle avg、count、max、min、sum、having、any、all、nvl的用法
查看>>
Oracle BEQ方式连接配置
查看>>
oracle Blob保存方式,oracle 存储过程操作blob
查看>>
Oracle BMW Racing sailing vessel帆船图
查看>>