二叉树转双向列表-仿写。
2016-05-26 15:39
369 查看
仿写了一个二叉树转双向列表。核心思想就是记录中序排序和缓存上一个指针。
#include "stdafx.h"
#include<stdlib.h>
struct BinanryTree
{
int value;
BinanryTree* p_left;
BinanryTree* p_right;
};
BinanryTree* insertTree(BinanryTree* p_tree, int value) {
if (p_tree == NULL) {
p_tree =(BinanryTree*) malloc(sizeof(BinanryTree));
memset(p_tree, 0, sizeof(BinanryTree)); /*初始化*/
p_tree->value = value;
return p_tree;
}
if (p_tree->value > value) {
p_tree->p_right=insertTree(p_tree->p_right, value);
}
else
{
p_tree-> p_left = insertTree(p_tree->p_left, value);
}
return p_tree;
}
/*对树中数据的操作*/
void operate(int value) {
printf("%d , ", value);
}
void show_tree(BinanryTree* p_tree) {
if (p_tree == NULL) {
return;
}
/*调换下面三个顺序,实现前中后序遍历*/
show_tree(p_tree->p_right);
operate(p_tree->value);
show_tree(p_tree->p_left);
}
void tree_to_list(BinanryTree* p_tree, BinanryTree* last_node) {
if (p_tree == NULL) {
return;
}
tree_to_list(p_tree ->p_left , last_node);
if (last_node != NULL) {
p_tree->p_left = last_node;
last_node->p_right = p_tree;
}
last_node = p_tree;
if (p_tree->p_right != NULL) {
tree_to_list(p_tree->p_right, last_node);
}
}
struct List
{
int value;
List* p_left;
List* p_right;
};
BinanryTree* get_head(BinanryTree* p_tree) {
if (p_tree == NULL) {
return NULL;
}
if (p_tree->p_right == NULL) {
return p_tree;
}
return get_head(p_tree->p_right);
}
void show_list(BinanryTree* p_list) {
if (p_list != NULL) {
printf("%d, ", p_list->value);
show_list(p_list->p_left);
}
}
int main()
{
int x[8] = {54, 23, 12, 42, 90, 3, 132, 89};
BinanryTree* p_tree = NULL;
for (int i = 0; i < 8; i++) {
p_tree = insertTree(p_tree, x[i]);
}
show_tree(p_tree);
BinanryTree* p_head = NULL;
BinanryTree* last_node = NULL;
p_head = get_head(p_tree);
tree_to_list(p_tree, last_node);
printf("\n\r");
show_list(p_head);
return 0;
}
#include "stdafx.h"
#include<stdlib.h>
struct BinanryTree
{
int value;
BinanryTree* p_left;
BinanryTree* p_right;
};
BinanryTree* insertTree(BinanryTree* p_tree, int value) {
if (p_tree == NULL) {
p_tree =(BinanryTree*) malloc(sizeof(BinanryTree));
memset(p_tree, 0, sizeof(BinanryTree)); /*初始化*/
p_tree->value = value;
return p_tree;
}
if (p_tree->value > value) {
p_tree->p_right=insertTree(p_tree->p_right, value);
}
else
{
p_tree-> p_left = insertTree(p_tree->p_left, value);
}
return p_tree;
}
/*对树中数据的操作*/
void operate(int value) {
printf("%d , ", value);
}
void show_tree(BinanryTree* p_tree) {
if (p_tree == NULL) {
return;
}
/*调换下面三个顺序,实现前中后序遍历*/
show_tree(p_tree->p_right);
operate(p_tree->value);
show_tree(p_tree->p_left);
}
void tree_to_list(BinanryTree* p_tree, BinanryTree* last_node) {
if (p_tree == NULL) {
return;
}
tree_to_list(p_tree ->p_left , last_node);
if (last_node != NULL) {
p_tree->p_left = last_node;
last_node->p_right = p_tree;
}
last_node = p_tree;
if (p_tree->p_right != NULL) {
tree_to_list(p_tree->p_right, last_node);
}
}
struct List
{
int value;
List* p_left;
List* p_right;
};
BinanryTree* get_head(BinanryTree* p_tree) {
if (p_tree == NULL) {
return NULL;
}
if (p_tree->p_right == NULL) {
return p_tree;
}
return get_head(p_tree->p_right);
}
void show_list(BinanryTree* p_list) {
if (p_list != NULL) {
printf("%d, ", p_list->value);
show_list(p_list->p_left);
}
}
int main()
{
int x[8] = {54, 23, 12, 42, 90, 3, 132, 89};
BinanryTree* p_tree = NULL;
for (int i = 0; i < 8; i++) {
p_tree = insertTree(p_tree, x[i]);
}
show_tree(p_tree);
BinanryTree* p_head = NULL;
BinanryTree* last_node = NULL;
p_head = get_head(p_tree);
tree_to_list(p_tree, last_node);
printf("\n\r");
show_list(p_head);
return 0;
}
相关文章推荐
- sqlserver 中临时表、临时变量和with as关键词创建“临时表”的区别
- touch
- 扩展访问列表 IP TCP
- redis 3.0的集群部署
- OwnCloud安装及维护文档(基于linux CentOS6.5)
- python如何支持中文注释
- 把一个钝角三角形分成若干个锐角三角形
- mysql数据库压力测试初学(1)
- shape黑线框背景
- 关于每隔1s执行一段程序的方法
- ORACLE新建表,删除表,设置主键,查询的简单例子
- wmframework v2.0 手册(1-2)系统框架介绍 spring等
- 通过代码自定义cell(cell的高度不一致)
- Shell: 如何计算一个文本文件中有多少个相同的单词??
- Linux下端口被占用解决(netstat,lsof命令)
- Linux sqlite3 基本命令
- 使用delegate的步骤
- iOS学习之—— snapshotViewAfterScreenUpdates
- Android Intent最全面的解析
- 二叉树--判断平衡二叉树