二叉树树的研究——从创建到打印到从顶层逐步打印.....持续研究中

来源:互联网 发布:网络摄像机编码器 编辑:程序博客网 时间:2024/06/05 15:50

time:2014.11.14

#include<iostream>#include<vector>using namespace std;typedef struct BiTNode{char data;struct BiTNode *lChild;struct BiTNode *rChild;}BiTNode,*BiTree;//**********************************************************************************//1.创建二叉树BiTree creat(){char ch;BiTree T;ch = getchar();if (ch == NULL)T = NULL;else{T = new BiTNode;T->data = ch;T->lChild = creat();T->rChild = creat();}return T;}//*********************************************************************************//2.打印二叉树:三种方法//前序打印void pre_print(BiTree T){if (T){cout << T->data;pre_print(T->lChild);pre_print(T->rChild);}}//中序打印void in_print(BiTree T){if (T){in_print(T->lChild);cout << T->data;in_print(T->rChild);}}//后序打印void post_print(BiTree T){if (T){post_print(T->lChild);post_print(T->rChild);cout << T->data;}}//************************************************************************************************//3.顶部开始逐层打印二叉树结点数据void print_at_level(BiTNode* root){vector<BiTree> vec;vec.push_back(root);while (vec.empty() != NULL){BiTNode* tmp = vec.front();if ((tmp->lChild) != NULL)vec.push_back(tmp->lChild);if ((tmp->rChild) != NULL)vec.push_back(tmp->rChild);cout << tmp->data;vec.pop_back();}}//**************************************************************************************************//4.如何判断一棵二叉树是否是平衡二叉树


0 0
原创粉丝点击