二叉树数组存储结构实现
2013-03-26 22:12
375 查看
不是满二叉树的话非常浪费空间,一般就在堆排序中用了,写这个一般操作纯粹是因为坑爹的软设作业= =
测试:
#ifndef arrayBinaryTree_H_ #define arrayBinaryTree_H_ #define maxsize 1000 #include <iostream> #include <stack> #include <cstdlib> #include <cstring> using namespace std; class arrayBinaryTree { private: int num; char node[maxsize]; public: arrayBinaryTree(); //~arrayBinaryTree(); void creat(); void preOrder(); }; arrayBinaryTree::arrayBinaryTree() { cout<<"please input the num of the node(include the empty node)"<<endl; cin>>num; } void arrayBinaryTree::creat() { memset(node,'#',sizeof(node)); int i; for(i=0; i<num; i++) { cin>>node[i]; } } void arrayBinaryTree::preOrder() { int temp; stack<int>S; S.push(0); while(!S.empty()) { temp=S.top(); S.pop(); cout<<node[temp]<<" "; int lchild=temp*2+1; int rchild=temp*2+2; if(node[rchild]!='#') S.push(rchild); if(node[lchild]!='#') S.push(lchild); } } #endif
测试:
#include <iostream> #include "arrayBinaryTree.h" using namespace std; int main() { //input //31 //A#B##CD####EF#G################ arrayBinaryTree test; test.creat(); test.preOrder(); return 0; }
相关文章推荐
- 二叉树-----数组存储结构及操作算法的实现------堆排序
- 8. C#数据结构与算法 -- 线性存储结构(线性表之顺序表,数组实现)
- 数据结构之---C语言实现二叉树的三叉链表存储表示
- 二叉树的存储结构和实现
- 用数组实现顺序存储二叉树及操作的实现
- 数据结构之二叉树 使用数组实现
- 6.33③ 假定用两个一维数组L[1..n]和R[1..n]作为 有n个结点的二叉树的存储结构, L[i]和R[i]分别指 示结点i的左孩子和右孩子,0表示空。试写一个算法 判别结点u是否为结点v的
- 二叉树的存储结构和实现
- 优先队列,由于优先队列的树形结构是完全二叉树,所以可以使用数组的形式去存储,我的上一个博客是用二叉树的形式去存储优先队列的
- 数据结构线性存储之连续存储数组的实现
- 【数据结构之线性表顺序存储】简单的数组的方式实现
- 数据结构(12)--二叉树的静态链表存储和遍历的实现
- 数据结构之---C语言实现二叉树的二叉链表存储表示
- Java中二叉树存储结构实现
- 线性结构——连续存储【数组】的建立与实现
- 数据结构(13)二叉树的动态链表存储和遍历的实现
- 二叉树的链式存储结构 前序 后序 中序 层序遍历操作实现 判断是否完全二叉树
- 数组的顺序存储表示和实现-数据结构
- 二叉树 二叉树的性质 存储结构 遍历二叉树 C实现二叉树的创建和遍历 线索二叉树
- 二叉树的三叉链表存储结构的简单实现