从数组创建完全二叉树
2013-10-17 11:49
330 查看
由给定的数组创建一颗二叉树。
#include <iostream> using namespace std; struct Node{ int m_nData; Node *m_pLeft; Node *m_pRight; }; Node* buildCore(int *piValue, int iStart, int iLength) { if (iStart>=iLength){ return NULL; } Node *pRoot = new Node; pRoot->m_nData = piValue[iStart]; pRoot->m_pLeft = buildCore(piValue, iStart*2+1, iLength); pRoot->m_pRight = buildCore(piValue, iStart*2+2, iLength); return pRoot; } Node* buildCompleteBinaryTree(int *piValue, int iLength) { if (piValue==NULL || iLength<=0){ return NULL; } return buildCore(piValue, 0, iLength); } void preOrder_Recursive(Node *pRoot) { if (pRoot){ cout<<pRoot->m_nData<<" "; preOrder_Recursive(pRoot->m_pLeft); preOrder_Recursive(pRoot->m_pRight); } } int main() { int data[9] = {1,2,3,4,5,6,7,8,9}; Node *pRoot = buildCompleteBinaryTree(data, 9); cout<<"Pre-Order traverse:"<<endl; preOrder_Recursive(pRoot); }
相关文章推荐
- WinForm中在删除前的温馨提示“您确定要删除吗”
- getDefinitionByName 反射自定义类报错解决办法
- 开篇
- arm权威指南及我们组项目笔记
- DevExpress汉化(WinForm)
- 總結幾種常見的 Word 轉換 PDF 方法
- win 2008 安装完域控,exchange,iis 中开启32位应用程序的应用程序池会自动停止
- linux线程
- Android里面的匿名类与匿名内部类使用
- UVa 11189 A Simple Pendulum
- socket中的短连接与长连接,心跳包示例详解
- mongodb创建replica set时,需要先配置主机名
- MFC中关于字体的设置
- mongodb 启动设置
- 湘军团练志:罗泽南、李续宾、王錱
- js获取上一页、当前页及域名url方法,JS反回上一页的方法
- 网络钓鱼也爱啃苹果 伺机窃取Apple ID
- Java中正则表达式的使用
- 转:步步LINUX C--进程间通信(二)信号
- 实验 4 在分支循环结构中调用自定义函数 利用循环计算多个圆柱体体积