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

你可能感兴趣的文章
Netty源码—2.Reactor线程模型一
查看>>
Netty源码—4.客户端接入流程一
查看>>
Netty源码—4.客户端接入流程二
查看>>
Netty源码—5.Pipeline和Handler一
查看>>
Netty源码—6.ByteBuf原理二
查看>>
Netty源码—7.ByteBuf原理三
查看>>
Netty源码—7.ByteBuf原理四
查看>>
Netty源码—8.编解码原理二
查看>>
Netty源码解读
查看>>
Netty的Socket编程详解-搭建服务端与客户端并进行数据传输
查看>>
Netty相关
查看>>
Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
查看>>
Network Sniffer and Connection Analyzer
查看>>
NFS共享文件系统搭建
查看>>
ng 指令的自定义、使用
查看>>
nginx + etcd 动态负载均衡实践(二)—— 组件安装
查看>>
Nginx + uWSGI + Flask + Vhost
查看>>
Nginx Location配置总结
查看>>
Nginx 动静分离与负载均衡的实现
查看>>
Nginx 反向代理解决跨域问题
查看>>