当前位置:网站首页>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、运行结果
边栏推荐
- 动态数组-vector
- 发布模块到npm应该怎么操作?及错误问题解决方案
- Detailed introduction to drawing complex surfaces using the plot_surface command
- TCP三次握手、四次挥手
- Win11 keeps popping up User Account Control how to fix it
- 3. User upload avatar
- 第二十五章:一文掌握while循环
- Codeforces Round #605 (Div. 3)
- Actual combat Meituan Nuxt +Vue family bucket, server-side rendering, mailbox verification, passport authentication service, map API reference, mongodb, redis and other technical points
- Codeforces Round #624 (Div. 3)
猜你喜欢
随机推荐
pytorch模型转libtorch和onnx格式的通用代码
Redis常见面试题
Knapsack Problem - Dynamic Programming - Theory
pygame绘制弧线
二叉排序树与 set、map
Open the door of electricity "Circuit" (1): voltage, current, reference direction
mysql的索引结构为什么选用B+树?
测试用例练习
Lightweight AlphaPose
模板系列-并查集
win10怎么设置不睡眠熄屏?win10设置永不睡眠的方法
4. Publish Posts, Comment on Posts
mysql学习总结 & 索引
Do Windows 10 computers need antivirus software installed?
轻量化AlphaPose
General syntax and usage instructions of SQL (picture and text)
IPV4和IPV6是什么?
KiCad常用快捷键
[STM32 Learning 1] Basic knowledge and concepts are clear
Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment