二叉树的存储结构
来源:互联网 发布:如何预防网络电信诈骗 编辑:程序博客网 时间:2024/06/06 11:37
1.顺式存储结构 :
数据元素结构的定义如下 :
typedef struct node{
DataType data;
int lchildIndex;
int rchildIndex;
}BitreeNode;
此操作代码略去;
2.链式存储结构 :
代码如下
#include <stdio.h>#include <stdlib.h>typedef char DataType;//数据元素结构typedef struct node{ DataType data; struct node * lchild; struct node * rchild; }BitTreeNode;//初始化根节点void initate(BitTreeNode ** root,DataType e){ *root = (BitTreeNode *)malloc(sizeof(BitTreeNode)); (*root)->data = e; (*root)->lchild = NULL; (*root)->rchild = NULL; }//插入左结点BitTreeNode * insertLeftChild(BitTreeNode * curr,DataType e){ if(curr == NULL) return NULL; BitTreeNode *node = (BitTreeNode *)malloc(sizeof(BitTreeNode)); node->data = e; node->lchild = curr->lchild; node->rchild = NULL; curr->lchild = node; return node;}//插入右结点BitTreeNode * insertRightChild(BitTreeNode * curr,DataType e){ if(curr == NULL) return NULL; BitTreeNode *node = (BitTreeNode *)malloc(sizeof(BitTreeNode)); node->data = e; node->rchild = curr->rchild; node->lchild = NULL; curr->rchild = node; return node;}
/** 销毁树**/void destroyTree(BitTreeNode *node){ if(node == NULL) return; if(node->lchild == NULL && node->rchild == NULL) free(node); //left first to destroy; destroyTree(node->lchild); destroyTree(node->rchild); //destroy self free(node);}//delete left childBitTreeNode * deleteLeftChild(BitTreeNode * curr){ if(curr == NULL) return NULL; if(curr->lchild == NULL) return curr; //deletedestroyTree(curr->lchild);
return curr;
}
BitTreeNode * deleteRightChild(BitTreeNode * curr){
if(curr == NULL) return NULL;
if(curr->rchild == NULL) return curr;
//delete
destroyTree(curr->rchild);
return curr;
}
/**
前序输出
**/
void preOrder(BitTreeNode * node){
if(node == NULL) return;
printf("%c ",node->data);
preOrder(node->lchild);
preOrder(node->rchild);
}
/**
中序输出
**/
void inOrder(BitTreeNode * node){
if(node == NULL) return;
inOrder(node->lchild);
printf("%c ",node->data);
inOrder(node->rchild);
}
/**
后序输出
**/
void postOrder(BitTreeNode * node){
if(node == NULL) return;
postOrder(node->lchild);
postOrder(node->rchild);
printf("%c ",node->data);
}
/**凹式输出二叉树的信息**/void printBitTree(BitTreeNode * curr,int level){ //print current node if(curr == NULL) return; //left child printBitTree(curr->lchild,level+1); int i; for(i=0;i<level ; i++){ printf(" "); } printf("%c\n",curr->data); printBitTree(curr->rchild,level+1);}//在二叉树查找元素BitTreeNode * findElement(BitTreeNode * curr,DataType e){ BitTreeNode * node = NULL; if(curr== NULL) return NULL; //self if(curr->data == e) return curr; //left node = findElement(curr->lchild,e); if(node != NULL) return node; node = findElement(curr->rchild,e); if(node != NULL) return node; return node; //huisu}// int main(){ //initate a tree BitTreeNode *root; initate(&root,'a'); //insert BitTreeNode * b = insertLeftChild(root,'b'); BitTreeNode * c= insertRightChild(root,'c'); BitTreeNode * d = insertLeftChild(b,'d'); BitTreeNode * e = insertRightChild(b,'e'); BitTreeNode * f= insertLeftChild(c,'f'); BitTreeNode * g= insertLeftChild(c,'g'); printf("preorder :"); preOrder(root); printf("\n"); printf("inorder :"); inOrder(root); printf("\n"); printf("podtorder :"); postOrder(root); printf("\n"); printf("ao ru shi :\n"); printBitTree(root,1); //find if(findElement(root,'e')){ printf("e is the element of the tree\n"); }else{ printf("sorry e is not the element of the tree\n"); } //destroy the tree destroyTree(root); printf("destroy succeed\n"); return 0; }
阅读全文
0 0
- 二叉树的存储结构
- 二叉树的存储结构
- 二叉树的存储结构
- 二叉树的存储结构
- 二叉树的存储结构
- 二叉树的存储结构
- 二叉树的存储结构
- 二叉树的存储结构
- 二叉树的存储结构
- 二叉树的存储结构
- 二叉树的存储结构
- 二叉树的存储结构
- 二叉树的存储结构
- 二叉树的存储结构
- 二叉树的存储结构
- 二叉树的存储结构
- 二叉树的存储结构
- 二叉树的存储结构
- 网卡TSO、UFO、GSO、LRO、GRO和RSS介绍
- Codeforces Round #433 Div. 2 B. Maxim Buys an Apartment
- 【知了堂学习笔记】简单了解java集合类
- AngularJs的点击删除,排序,查询
- Apache ActiveMQ 各个版本所依赖的JDK版本
- 二叉树的存储结构
- 图像形态学运算之腐蚀-膨胀篇
- mysql使用存储过程批量生成学生表、课程表和成绩表测试数据
- 关于Unicode编码和ANSI编码(2)
- ubuntu下lua插件使用方式
- 20170916
- 基于平板电脑的面访调查APP
- LeetCode#169 Majority Element题解(C++版)
- 暴搜 栅栏