第35课 - 创建二叉树

1. 指路法定位结点

从根节点开始。。。

结点1的位置:{NUll}

结点2的位置:{左}

结点3的位置:{右}

结点4的位置:{左,左}

结点5的位置:{左,右}

结点6的位置:{右,左}

结点7的位置:{右,右}

结点8的位置:{左,左,左}

结点9的位置:{左,左,右}

结点10的位置:{左,右,左}

指路法通过根结点与目标结点的相对位置进行定位。

指路法可以避开二叉树递归的性质“线性”定位。

思想:在C语言中可以利用bit位进行指路。

#define BT_LEFT 0

#define BT_RIGHT 1

typedef unsigned long long BTP0s;

2. 二叉树存储结构

用结构体来定义二叉树中的指针域。

二叉树的头结点也可以用结构体实现。

3. 二叉树的操作

(1)定位

while((count > 0)&&(current != NULL))

{

bit = pos &1;

pos = pos >>1;

count--;

parent = current;

if( bit == BT_LEFT)

{

current = current->left;

}

else if( bit == BT_RIGHT)

{

current = current->right;

}

}

技巧:利用二进制中的0和1分别代表left和right。

位运算是实现指路法的基础。

4. 程序

main.c

#include <stdio.h>

#include <stdlib.h>

#include "BTree.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

struct Node

{

BTreeNode header;

char v;

};

void printf_data(BTreeNode* node)

{

if( node != NULL )

{

printf("%c", ((struct Node*)node)->v);

}

}

int main(int argc, char *argv[])

{

BTree* tree = BTree_Create();

struct Node n1 = {{NULL, NULL}, 'A'};

struct Node n2 = {{NULL, NULL}, 'B'};

struct Node n3 = {{NULL, NULL}, 'C'};

struct Node n4 = {{NULL, NULL}, 'D'};

struct Node n5 = {{NULL, NULL}, 'E'};

struct Node n6 = {{NULL, NULL}, 'F'};

BTree_Insert(tree, (BTreeNode*)&n1, 0, 0, 0);

BTree_Insert(tree, (BTreeNode*)&n2, 0x00, 1, 0);

BTree_Insert(tree, (BTreeNode*)&n3, 0x01, 1, 0);

BTree_Insert(tree, (BTreeNode*)&n4, 0x00, 2, 0);

BTree_Insert(tree, (BTreeNode*)&n5, 0x02, 2, 0);

BTree_Insert(tree, (BTreeNode*)&n6, 0x02, 3, 0);

printf("Height: %d\n", BTree_Height(tree));

printf("Degree: %d\n", BTree_Degree(tree));

printf("Count: %d\n", BTree_Count(tree));

printf("Position At (0x02, 2): %c\n", ((struct Node*)BTree_Get(tree, 0x02, 2))->v);

printf("Full Tree: \n");

BTree_Display(tree, printf_data, 4, '-');

BTree_Delete(tree, 0x00, 1);

printf("After Delete B: \n");

printf("Height: %d\n", BTree_Height(tree));

printf("Degree: %d\n", BTree_Degree(tree));

printf("Count: %d\n", BTree_Count(tree));

printf("Full Tree: \n");

BTree_Display(tree, printf_data, 4, '-');

BTree_Clear(tree);

printf("After Clear: \n");

printf("Height: %d\n", BTree_Height(tree));

printf("Degree: %d\n", BTree_Degree(tree));

printf("Count: %d\n", BTree_Count(tree));

BTree_Display(tree, printf_data, 4, '-');

BTree_Destroy(tree);

return 0;

}

 

BTree.h

#ifndef _BTREE_H_

#define _BTREE_H_

#define BT_LEFT 0

#define BT_RIGHT 1

typedef void BTree;

typedef unsigned long long BTPos;

typedef struct _tag_BTreeNode BTreeNode;

struct _tag_BTreeNode

{

BTreeNode* left;

BTreeNode* right;

};

typedef void (BTree_Printf)(BTreeNode*);

BTree* BTree_Create();

void BTree_Destroy(BTree* tree);

void BTree_Clear(BTree* tree);

int BTree_Insert(BTree* tree, BTreeNode* node, BTPos pos, int count, int flag);

BTreeNode* BTree_Delete(BTree* tree, BTPos pos, int count);

BTreeNode* BTree_Get(BTree* tree, BTPos pos, int count);

BTreeNode* BTree_Root(BTree* tree);

int BTree_Height(BTree* tree);

int BTree_Count(BTree* tree);

int BTree_Degree(BTree* tree);

void BTree_Display(BTree* tree, BTree_Printf* pFunc, int gap, char div);

#endif

 

BTree.c

#include <stdio.h>

#include <malloc.h>

#include "BTree.h"

typedef struct _tag_BTree TBTree;

struct _tag_BTree

{

int count;

BTreeNode* root;

};

static void recursive_display(BTreeNode* node, BTree_Printf* pFunc, int format, int gap, char div) // O(n)

{

int i = 0;

if( (node != NULL) && (pFunc != NULL) )

{

for(i=0; i<format; i++)

{

printf("%c", div);

}

pFunc(node);

printf("\n");

if( (node->left != NULL) || (node->right != NULL) )

{

recursive_display(node->left, pFunc, format + gap, gap, div);

recursive_display(node->right, pFunc, format + gap, gap, div);

}

}

else

{

for(i=0; i<format; i++)

{

printf("%c", div);

}

printf("\n");

}

}

static int recursive_count(BTreeNode* root) // O(n)

{

int ret = 0;

if( root != NULL )

{

ret = recursive_count(root->left) + 1 + recursive_count(root->right);

}

return ret;

}

static int recursive_height(BTreeNode* root) // O(n)

{

int ret = 0;

if( root != NULL )

{

int lh = recursive_height(root->left);

int rh = recursive_height(root->right);

ret = ((lh > rh) ? lh : rh) + 1;

}

return ret;

}

static int recursive_degree(BTreeNode* root) // O(n)

{

int ret = 0;

if( root != NULL )

{

if( root->left != NULL )

{

ret++;

}

if( root->right != NULL )

{

ret++;

}

if( ret == 1 )

{

int ld = recursive_degree(root->left);

int rd = recursive_degree(root->right);

if( ret < ld )

{

ret = ld;

}

if( ret < rd )

{

ret = rd;

}

}

}

return ret;

}

BTree* BTree_Create() // O(1)

{

TBTree* ret = (TBTree*)malloc(sizeof(TBTree));

if( ret != NULL )

{

ret->count = 0;

ret->root = NULL;

}

return ret;

}

void BTree_Destroy(BTree* tree) // O(1)

{

free(tree);

}

void BTree_Clear(BTree* tree) // O(1)

{

TBTree* btree = (TBTree*)tree;

if( btree != NULL )

{

btree->count = 0;

btree->root = NULL;

}

}

int BTree_Insert(BTree* tree, BTreeNode* node, BTPos pos, int count, int flag) // O(n)

{

TBTree* btree = (TBTree*)tree;

int ret = (btree != NULL) && (node != NULL) && ((flag == BT_LEFT) || (flag == BT_RIGHT));

int bit = 0;

if( ret )

{

BTreeNode* parent = NULL;

BTreeNode* current = btree->root;

node->left = NULL;

node->right = NULL;

while( (count > 0) && (current != NULL) )

{

bit = pos & 1;

pos = pos >> 1;

parent = current;

if( bit == BT_LEFT )

{

current = current->left;

}

else if( bit == BT_RIGHT )

{

current = current->right;

}

count--;

}

if( flag == BT_LEFT )

{

node->left = current;

}

else if( flag == BT_RIGHT )

{

node->right = current;

}

if( parent != NULL )

{

if( bit == BT_LEFT )

{

parent->left = node;

}

else if( bit == BT_RIGHT )

{

parent->right = node;

}

}

else

{

btree->root = node;

}

btree->count++;

}

return ret;

}

BTreeNode* BTree_Delete(BTree* tree, BTPos pos, int count) // O(n)

{

TBTree* btree = (TBTree*)tree;

BTreeNode* ret = NULL;

int bit = 0;

if( btree != NULL )

{

BTreeNode* parent = NULL;

BTreeNode* current = btree->root;

while( (count > 0) && (current != NULL) )

{

bit = pos & 1;

pos = pos >> 1;

parent = current;

if( bit == BT_LEFT )

{

current = current->left;

}

else if( bit == BT_RIGHT )

{

current = current->right;

}

count--;

}

if( parent != NULL )

{

if( bit == BT_LEFT )

{

parent->left = NULL;

}

else if( bit == BT_RIGHT )

{

parent->right = NULL;

}

}

else

{

btree->root = NULL;

}

ret = current;

btree->count = btree->count - recursive_count(ret);

}

return ret;

}

BTreeNode* BTree_Get(BTree* tree, BTPos pos, int count) // O(n)

{

TBTree* btree = (TBTree*)tree;

BTreeNode* ret = NULL;

int bit = 0;

if( btree != NULL )

{

BTreeNode* current = btree->root;

while( (count > 0) && (current != NULL) )

{

bit = pos & 1;

pos = pos >> 1;

if( bit == BT_LEFT )

{

current = current->left;

}

else if( bit == BT_RIGHT )

{

current = current->right;

}

count--;

}

ret = current;

}

return ret;

}

BTreeNode* BTree_Root(BTree* tree) // O(1)

{

TBTree* btree = (TBTree*)tree;

BTreeNode* ret = NULL;

if( btree != NULL )

{

ret = btree->root;

}

return ret;

}

int BTree_Height(BTree* tree) // O(n)

{

TBTree* btree = (TBTree*)tree;

int ret = 0;

if( btree != NULL )

{

ret = recursive_height(btree->root);

}

return ret;

}

int BTree_Count(BTree* tree) // O(1)

{

TBTree* btree = (TBTree*)tree;

int ret = 0;

if( btree != NULL )

{

ret = btree->count;

}

return ret;

}

int BTree_Degree(BTree* tree) // O(n)

{

TBTree* btree = (TBTree*)tree;

int ret = 0;

if( btree != NULL )

{

ret = recursive_degree(btree->root);

}

return ret;

}

void BTree_Display(BTree* tree, BTree_Printf* pFunc, int gap, char div) // O(n)

{

TBTree* btree = (TBTree*)tree;

if( btree != NULL )

{

recursive_display(btree->root, pFunc, 0, gap, div);

}

}

小结:

二叉树在结构上不依赖组织链表。

通过指路法可以方便的定位二叉树中的结点。

基于指路法的二叉树在插入、删除和获取操作的实现细节上与单链表相似。

单链表就是特殊的额二叉树,实现上当然相似,只是更简单而已。

转载于:https://www.cnblogs.com/free-1122/p/11336047.html

数据--第35课 - 创建二叉树相关推荐

  1. matlab创建二叉树(二维数据)

    一.学习要点 1.注意matlab中全局变量与局部变量的区别:本文中assigned_nn为局部变量,每一次递归中的值都是不一样的,node_nubmer为全局变量,当前值的改变如递增,必回影响以后每 ...

  2. php网课资源百度云盘_安全中国PHP网站开发工程师就业指导班 35课 附课件、源码,全套视频教程学习资料通过百度云网盘下载...

    链接失效请联系微信 ZA_summer,有需要某课网某某课堂精品付费课的朋友也可以联系我.ps:凡在本店**过教程的同学即可加入技术交流群,群内不定期推送最新it教程. 第1课初识php及其php的开 ...

  3. 7-10 先序序列创建二叉树,输出先序序列、中序序列、后序序列并输出叶子结点数 (10 分)

    7-10 先序序列创建二叉树,输出先序序列.中序序列.后序序列并输出叶子结点数 (10 分) 对于给定的二叉树,输出其先序序列.中序序列.后序序列并输出叶子结点数. 输入格式: 二叉树的先序遍历序列. ...

  4. 二叉树学习笔记之利用前序遍历递归创建二叉树

    利用前序遍历创建二叉树 树的遍历 前序遍历(preorder traversal) 利用前序遍历创建二叉树 中序遍历访问所有结点 前序遍历访问所有结点 后序遍历访问所有结点 实例 树的遍历 树的遍历是 ...

  5. 二叉树的创建_【数据结构用python描述】python创建二叉树

    接下来一段时间小编会和大家一起学习数据结构用python描述. C/C++可以通过使用链表来创建二叉树,当然python也可以,但是现在的问题是 python没有指针和引用.C/C++创建链表二叉树需 ...

  6. 先根序列创建二叉树c语言,用C语言创建二叉树并先序遍历.doc

    用C语言创建二叉树并先序遍历 用C语言创建二叉树并 #include "stdio.h" #include "stdlib.h" typedef struct ...

  7. 第35课: 打通Spark系统运行内幕机制循环流程

    第35课: 打通Spark系统运行内幕机制循环流程 Spark通过DAGScheduler面向整个Job划分出了不同的Stage,划分Stage之后,Stage从后往前划分,执行的时候从前往后执行,每 ...

  8. 按照前序遍历创建二叉树及树的四种遍历方式

    一.二叉树的介绍 二叉树的特点是二叉树的每个结点的度都不大于2,可以视为每个结点都有左孩子和右孩子.故二叉树结点的数据结构为 二.二叉树的特点 1.设根结点所在的层数为第1层,则第i层最多有个结点. ...

  9. 数据结构与算法实验 实验6:二叉树ADT的二叉链式实现 (由完全前序序列创建二叉树 / 求二叉树的节点数/树高/叶子节点数 /先序中序后序层序遍历)

    假设二叉数的数据元素为字符,采用二叉链式存储结构.请编码实现二叉树ADT,其中包括创建二叉树.遍历二叉树(深度.广度).求二叉树的深度(高度).计算二叉树的元素个数.计算二叉树的叶子数.二叉树的格式输 ...

  10. 井号法(#)创建二叉树(利用前序遍历来建树)C++实现

    不要自卑,去提升实力 互联网行业谁技术牛谁是爹 如果文章可以带给你能量,那是最好的事!请相信自己 加油o~ 利用#号法来创建二叉树 输入: ABD##E##C#F## 对应该输入的二叉树应为这样 解题 ...

最新文章

  1. (转)HTTP 协议之压缩
  2. JDBC(Java Data Base Connectivity,java数据库连接)
  3. mysql connector配置_mysql connector odbc配置注意事项
  4. linux命令——init 的使用用法
  5. 慎用PHP $_REQUEST数组
  6. 算法训练和模型部署如何避免多次重写数据预处理代码
  7. 求解两个经纬点之间的距离和角度(mm级精度)
  8. 遇到系统问题,先看OS再看CPU
  9. unity3d面试题
  10. 常用等价无穷小以及泰勒公式
  11. 《活着》的优秀读后感范文3000字
  12. postgresql查看数据库及数据表占用空间
  13. Vulkan 教程(1)开篇
  14. 医疗管理系统-项目概述和环境搭建
  15. 《程序是怎么跑起来的》第一章学习笔记
  16. Unity 最新UnityWebRequest下载,同时显示下载进度,和 显示网速,今天贴出来和大家分享
  17. 机器学习中的编码器-解码器结构哲学
  18. 风帆头,旗帜服,“背”在肩上的古国王印
  19. 创业公司如何公平分配股权
  20. 微信小程序中使用ECharts--折线图、柱状图、饼图等

热门文章

  1. Java设计模式----工厂模式-----简单工厂(静态工厂模式)
  2. Struts2-01-配置文件
  3. ASM的基础使用 Android 自动化埋点方案原理剖析
  4. 图片版坦克大战其他相关的 类(三)
  5. 深度集成 Flink: Apache Iceberg 0.11.0 最新功能解读
  6. 疫情之下,计算机仿真程序告诉你,没事别乱出门!企业复工务必做到八个“一”...
  7. python--django基础篇(创建项目,模型类,迁移,测试数据库操作)
  8. linux硬盘掉了,出了大问题,硬盘空间大量丢失
  9. java动画帧储存路径_Java实现帧动画的实例代码
  10. 发布PHP项目_Jenkins发布PHP项目之一自动化部署