二叉树
来源:互联网 发布:软件架构风格 编辑:程序博客网 时间:2024/05/27 00:32
#include <iostream>extern "C"{ #include <stdio.h> #include <stdlib.h>}using namespace std;typedef struct BinTreeNode{ char cData; BinTreeNode* lNode; BinTreeNode* rNode;}BinTreeNode;char dataBuf[] = "ABDH#K###E##CFI###G#J##";int index2 = 0;//注意此处全局变量不能使用index,index被库定义掉了void createBinTree(BinTreeNode*& node){ char data = dataBuf[index2++]; if(data == '#') return; node = (BinTreeNode*)malloc(sizeof(BinTreeNode)); if(node == NULL) return; node->cData = data; node->lNode = NULL; node->rNode = NULL; createBinTree(node->lNode); createBinTree(node->rNode);}void createBinTree2(BinTreeNode*& node){ char data; cout << "input node value:"; cin >> data; if(data == '0'){ node = NULL; return; } node = (BinTreeNode*)malloc(sizeof(BinTreeNode)); if(node == NULL){ return; } node->cData = data; createBinTree2(node->lNode);//先序创建二叉树 createBinTree2(node->rNode);}int treeDepth(BinTreeNode* node){ int leftDepth,rightDepth; if(node == NULL){ return 0; } else{ leftDepth = treeDepth(node->lNode); rightDepth = treeDepth(node->rNode); } return leftDepth>rightDepth?leftDepth+1:rightDepth+1;}//横向打印树结构void printBinTree(BinTreeNode* node,int depth){ int iTemp = depth; if(node == NULL){ return; } printBinTree(node->rNode,depth+1); while(--iTemp){ cout << " "; } cout << node->cData << endl; printBinTree(node->lNode,depth+1);}bool findData(BinTreeNode* node,char data){ if(node == NULL) return false; if(node->cData == data){ cout << "find the data " << data << endl; return true; } else{ bool res = findData(node->lNode,data); if(res){ return true; } else{ res = findData(node->rNode,data); return res; } }}int main(int argc,char* argv[]){ BinTreeNode* node = NULL; createBinTree(node); printBinTree(node,1); cout << "########################" << endl; int depth = treeDepth(node); cout << "the tree depth:" << depth << endl; char data; cin >> data; bool res = findData(node,data); cout << (res?"found":"not found")<< endl; return 0;}
0 0
- 二叉树、二叉堆
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 一个前端工程师所需要掌握的技能...
- STM32 资源
- 第十二周项目3—摩托车继承自行车和机动车
- 常用芯片整理---74VHC86
- 人脸识别实践haar-like特征+积分图+adaboost+级联
- 二叉树
- Ubuntu12.04下Qt5.2中源码编译Qwt
- 可扩展的开源移动端框架:Helios
- iOS7的Cell的分割线宽度不满屏,解决方法
- sscanf 函数说明
- 12周项目1.3
- 如何解决每次打开office2010都会出现正在配置
- iOS—应用检查更新的最优方法
- 精品文章记录