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

你可能感兴趣的文章
Python 字符串的几种拼装方式
查看>>
python 存入数据库bigint_python基础_MySQL的bigint类型
查看>>
python 学习 面向对象编程
查看>>
Python 学习日记第三篇 -- 字典
查看>>
Python 学习笔记(一)Data type
查看>>
python 安装echarts
查看>>
python 安装opencv及问题解决
查看>>
Python 安装程序:“写入文件 C:\Python27\pythonw.exe 时出错“
查看>>
Python 安装避坑指南:从入门到进阶
查看>>
Python 安装配置详解
查看>>
python 实现儿童算术游戏
查看>>
python 实现字符串转整型
查看>>
Python 实现定时任务的八种方案!
查看>>
Python 实现自动化测试 dubbo 协议接口
查看>>
python 对json数据读取及保存与读取,对dump,dumps,load,loads的理解
查看>>
Python 对mysql数据库的操作
查看>>
Python 对象数据库列表
查看>>
Python 导入requests报错No module named requests
查看>>
python 将函数加到列表中进行调用
查看>>
python 将图片与字符串相互转换
查看>>