当前位置:网站首页>Detailed introduction to the hierarchical method of binary tree creation
Detailed introduction to the hierarchical method of binary tree creation
2022-08-02 15:33:00 【Yang Lao head soft work】
一、引言
Two methods of creating a binary tree by the preorder method have been given above,Non-recursive and recursive methods, respectively,This article will giveCThe language version of the Hierarchical Method algorithm for creating binary trees.The so-called hierarchical method is actually based on where the nodes in the binary tree are“层”进行遍历,The rule for traversing each layer is from left to right.Therefore, the hierarchical traversal is actually traversed according to the numbering order of the nodes in the complete binary tree,But binary trees are not necessarily complete binary trees,Hence it involves the addition of virtual into a complete binary tree,Then do the hierarchy traversal.
The example binary tree used is shown in the figure below.
二、二叉树的创建-层次法
1、结点编号规则
Hierarchical method to create binary tree,It is still necessary to extend the binary tree.But here is different from the extension of the preorder method,The nodes need to be expanded according to the pattern of a complete binary tree.The purpose of filling these nodes is to record the node number of the complete binary tree,So in fact there is no need to actually fill in the missing nodes,Just remember the number of all nodes on the binary tree on the complete binary tree.
此时,图(1)The numbering of the mid-nodes is as follows(从1开始):
2、算法的基本思路:
首先创建一个数组S,Used to store all the nodes on the binary tree.
After that, start to read the node data in sequence,The specific method is to read the number of a node at the same timei和数据data,Create a node with this,and put the node into the arrayS的下标为i的位置.接着根据i/2to confirm the presence of his parentsS中的下标,且根据i是否被2Divide to determine the nodei是j的左子树还是右子树.
3、算法的具体实现:
#include"stdio.h"
#include"malloc.h"
#define CHAR
#define MAX_NODE 100
#ifdef CHAR
typedef char datatype;
#else
typedef int datatype;
#endif
typedef struct node
{
datatype data;
struct node *Lchild;
struct node *Rchild;
}BiTree;
BiTree *CreateBiTreeLevel( void )
{
BiTree *T, *p, *s[MAX_NODE];
datatype data;
int i, j;
T = NULL;
while ( 1 )
{
printf( " Enter the node number( 0 则终止输入 ): " );
scanf( "%d", &i );
if( i == 0 )
break;
else
{
#ifdef CHAR
getchar();//Filter newlines after the input node number
printf( " 请输入结点数据(字符):" );
scanf( "%c",&data );
getchar();//Newline character after filtering node data
#else
printf( " 请输入结点数据(整数):" );
scanf( "%d",&data );
#endif
//创建结点,并把data存入该结点,At the same time, the left and right subtrees of the node are empty
p = ( BiTree * )malloc( sizeof(BiTree) ) ;
p->data = data ;
p->Lchild = NULL;
p->Rchild = NULL;
s[i] = p ;
if( i == 1 ) //编号为1The node is the root of the tree
T = p ;
else//Find the parents of the newly added node
{
j = i / 2; // j是iThe parent node number of
if( i % 2 == 0 )
{
s[j]->Lchild = p;//左子树
//The output below is to verify that the position of the node is as expected.
#ifdef CHAR
printf( " %c 是 %c 的左子树\n", p->data, s[j]->data );
#else
printf( " %d 是 %d 的左子树\n", p->data, s[j]->data );
#endif
}
else
{
s[j]->Rchild = p;//右子树
#ifdef CHAR
printf( " %c 是 %c 的右子树\n", p->data, s[j]->data );
#else
printf( " %d 是 %d 的右子树\n", p->data, s[j]->data );
#endif
}
}
}
}
return( T );
}
int main()
{
BiTree *T = CreateBiTreeLevel( );
return 0;
}
4、运行结果
边栏推荐
猜你喜欢

In-depth understanding of Golang's Map

1.开发社区首页,注册

推开机电的大门《电路》(一):电压,电流,参考方向

Win11 system cannot find dll file how to fix

Win11 computer off for a period of time without operating network how to solve

How to reinstall Win7 system with U disk?How to reinstall win7 using u disk?

Redis的线程模型

mysql学习总结 & 索引

第二十五章:一文掌握while循环

Network Security Packet Capture
随机推荐
What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
Introduction to in-order traversal (non-recursive, recursive) after binary tree traversal
Daily - Notes
背包问题-动态规划-理论篇
一篇文章彻底理解Redis的持久化:RDB、AOF
MATLAB图形加标注的基本方法入门简介
A clean start Windows 7?How to load only the basic service start Windows 7 system
pygame绘制弧线
Please make sure you have the correct access rights and the repository exists. Problem solved
GMP scheduling model of golang
jest test, component test
Software Testing Basics (Back)
Codeforces Round #605 (Div. 3)
How to set the win10 taskbar does not merge icons
MATLAB制作简易小动画入门详解
What should I do if I install a solid-state drive in Win10 and still have obvious lags?
C语言函数参数传递模式入门详解
使用 腾讯云搭建一个个人博客
General code for pytorch model to libtorch and onnx format
6.统一记录日志