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

你可能感兴趣的文章
python3爬虫抓取智联招聘职位信息代码
查看>>
Python3爬虫数据入数据库---把爬取到的数据存到数据库,带数据库去重功能
查看>>
python3的 pymysql把mysqldb库取代了,让python 3支持mysqldb的解决方法
查看>>
Python3笔记——IDE的选择
查看>>
python3简单使用requests 用户代理,cookie池
查看>>
Python3组播通信编程实现教程(发送者+接收者)
查看>>
Python3编写网络爬虫10-数据存储方式三-CSV文件存储
查看>>
python3读取redis返回数据带有'b'的问题
查看>>
Python3连接MySQL
查看>>
Python关于isinstance函数的解析 | 附Demo(全)
查看>>
Python3:setup.py:完成所有任务的PIP安装(Build_ext+Install)
查看>>
python4.0 gil_Python 并发编程系列--1、浅谈GIL历史及对多线程的影响
查看>>
python60行绘图程序_天底下最简单的QT画图板,就一个类,60行代码
查看>>
Python: AES加密与解密
查看>>
Python: Range() 最大尺寸;动态的还是静态的?
查看>>
Python: Windows下用 multiprocessing 的深坑
查看>>
python:Could not find platform independent libraries <prefix>解决方法
查看>>
Python:datetime tzinfo 时区名称文档
查看>>
Python:deepcopy(list) vs new_list = old_list[:]
查看>>
Python:Google API - 从消息中获取 mimeTypes
查看>>