当前位置:网站首页>Basic properties and ergodicity of binary tree
Basic properties and ergodicity of binary tree
2022-06-24 20:29:00 【Wukong stopped buying vegetables】
Binary tree :
Each node in a tree has at most two nodes , Can have no nodes , You can also have only one node , It can also be an empty node
Let's talk about the common forms of binary trees :
The difference between full binary tree and complete binary tree :
A full binary tree is a tree except for leaf nodes , Each node has two children
A complete binary tree means that each node does not necessarily have two children , But if it appears on the same layer , Of the nodes of a complete binary tree i Position and full binary tree node i The position numbers are the same , It's a complete binary tree .
therefore , A full binary tree must be a complete binary tree , But a complete binary tree is not necessarily a full binary tree
Then let's talk about the properties of binary trees :
1. In the binary tree i There are at most 2^i-1 Nodes (i>0)
2. Depth is k There are at most 2^k - 1 Nodes (k>0)
3. For any binary tree , If the degree is 2 The number of nodes is n2 individual , Then the number of leaves n0 Must be n2+1(n0=n2+1)
4. have n The depth of a complete binary tree of nodes must be
5. For a completely binary tree , From top to bottom , From left to right , The number is i The node of , The left child must be 2i, The right child is numbered 2*i + 1, Their parents' number must be i/2(i=1 Time is root )
Traversal of binary tree :
It can be divided into three situations , The discussion of these three cases , It is based on whether to traverse the root first , Or traverse the root in the middle , Or to traverse the root at the end , It is divided into preorder traversal (DLR), In the sequence traversal (LDR), After the sequence traversal (LRD)
D: root L: Left node R: Right node
Let's start with a binary tree :
First, let's talk about preorder traversal (DLR)
Root first on the left and then on the right : Like a function r( from A Incoming node ), And then put A Print , After all, it is the first order , Then go through the left side , That is, go down and traverse
The function stack here is
Don't talk much , Go straight to the code :
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct _binary_node binary_node;
struct _binary_node
{
char ch;
binary_node *lchild;
binary_node *rchild;
};
void recursion_print(binary_node *node)
{
if (node == NULL) {
return;
}
printf("%c ", node->ch);
recursion_print(node->lchild);
recursion_print(node->rchild);
}
int main()
{
// Lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean lean
binary_node node_a = { 'A', NULL, NULL };
binary_node node_b = { 'B', NULL, NULL };
binary_node node_c = { 'C', NULL, NULL };
binary_node node_d = { 'D', NULL, NULL };
binary_node node_e = { 'E', NULL, NULL };
binary_node node_f = { 'F', NULL, NULL };
node_a.lchild = &node_b;
node_a.rchild = &node_c;
node_b.rchild = &node_e;
node_b.lchild = &node_d;
node_c.rchild = &node_f;
// Shit, shit
recursion_print(&node_a);
return 1;
}
Running results :
Yes, of course , Binary trees are certainly not recursive , therefore , For example, calculate the number of leaves of a binary tree , The height of the binary tree , Another example is copying a binary tree , Here's the complete code :
binary_tree.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct _binary_node binary_node;
struct _binary_node
{
char ch;
binary_node *lchild;
binary_node *rchild;
};
void recursion_print(binary_node *node)
{
if (node == NULL) {
return;
}
printf("%c ", node->ch);
recursion_print(node->lchild);
recursion_print(node->rchild);
}
void calc_leaf_num(binary_node *node,int *p)
{
// The end condition of the program
if(node == NULL) {
return ;
}
//left and right is null
if(node->lchild == NULL && node->rchild == NULL) {
(*p)++;
}
calc_leaf_num(node->lchild,p);// Pass in the address of the calculation quantity variable
calc_leaf_num(node->rchild,p);
}
// Calculate tree height
int get_tree_high(binary_node *node)
{
if(node == NULL) {
return 0;
}
// Recursively calculate the height of the left and right trees
int left_high = get_tree_high(node->lchild);
int right_high = get_tree_high(node->rchild);
return left_high > right_high ? left_high + 1 : right_high + 1;
}
// Copying a binary tree is to create a space on the heap
// To store every node of the binary tree
// Finally, return to the header node
binary_node* copy_tree(binary_node *node)
{
if(node == NULL){
return NULL;
}
// I still need to traverse the left tree , In traversing the right tree
binary_node* lnode = copy_tree(node->lchild);
binary_node* rnode = copy_tree(node->rchild);
// Each node has to do one thing
binary_node *new_node = (binary_node*)malloc(sizeof(binary_node));
if(new_node != NULL) {
new_node->ch = node->ch;
new_node->lchild = lnode;
new_node->rchild = rnode;
}
return new_node;
}
// Release the copied tree
void free_tree(binary_node *node)
{
if(node == NULL) {
return;
}
free_tree(node->lchild);
free_tree(node->rchild);
// Before that, take a look at the released nodes
printf("%c Released \n",node->ch);
free(node);// Release this node
}
int main()
{
binary_node node_a = { 'A', NULL, NULL };
binary_node node_b = { 'B', NULL, NULL };
binary_node node_c = { 'C', NULL, NULL };
binary_node node_d = { 'D', NULL, NULL };
binary_node node_e = { 'E', NULL, NULL };
binary_node node_f = { 'F', NULL, NULL };
node_a.lchild = &node_b;
node_a.rchild = &node_c;
node_b.rchild = &node_e;
node_b.lchild = &node_d;
node_c.rchild = &node_f;
recursion_print(&node_a);
int leaf_num = 0;
calc_leaf_num(&node_a,&leaf_num);
printf(" The number of leaf nodes is :%d\n",leaf_num);
int tree_high = get_tree_high(&node_a);
printf(" The number of leaves is :%d\n",tree_high);
// Copy the binary tree
binary_node *node = copy_tree(&node_a);
// Then recursively traverse the binary tree
printf("\n-------------\n");
recursion_print(node);
printf("\n-----\n");
// Release each node of the copy
// Binary tree non recursive traversal
free_tree(node);
return 1;
}
边栏推荐
- Popupwindow touch event transparent transmission scheme
- Jd.com: how does redis implement inventory deduction? How to prevent oversold?
- Database index can improve query efficiency. Ask what will improve, what is the difference between inapplicable index and index use, and what will happen.
- 用自身细胞作为原料,首例3D打印耳朵移植成功!未来可打印更复杂器官
- 等等党们的胜利!挖矿退潮后,显卡价格全面暴跌
- Introduction: continuously update the self-study version of the learning manual for junior test development engineers
- Two solutions to the problem of 0xv0000225 unable to start the computer
- [cann document express issue 05] let you know what operators are
- Accurate calculation of task progress bar of lol mobile game
- Five day summary of software testing
猜你喜欢
[go language questions] go from 0 to entry 4: advanced usage of slice, elementary review and introduction to map
2022年最新四川建筑八大员(电气施工员)模拟题库及答案
苹果不差钱,但做内容“没底气”
1、 Downloading and installing appium
Microsoft Office Excel 2013 2016 graphic tutorial on how to enable macro function
顺序栈遍历二叉树
Apple, Microsoft and Google will no longer fight each other. They will work together to do a big thing this year
顺序栈1.0版本
【Go語言刷題篇】Go從0到入門4:切片的高級用法、初級複習與Map入門學習
Audio and video 2020 2021 2022 basic operation and parameter setting graphic tutorial
随机推荐
[go Language brossage] go from 0 to Getting started 4: Advanced use of slice, Primary Review and Map Getting started Learning
gateway
苹果、微软、谷歌不再掐架,今年要合力干一件大事
Hosting service and SASE, enjoy the integration of network and security | phase I review
云计算发展的 4 个阶段,终于有人讲明白了
Comparative analysis of arrayblockingqueue and linkedblockingqueue
What about the Golden Angel of thunder one? Golden Angel mission details
Behind Tiantian Jianbao storm: tens of millions in arrears, APP shutdown, and the founder's premeditated plan to run away?
Popupwindow touch event transparent transmission scheme
网络安全审查办公室对知网启动网络安全审查,称其“掌握大量重要数据及敏感信息”
Redis installation of CentOS system under Linux, adding, querying, deleting, and querying all keys
"Super point" in "Meng Hua Lu", is the goose wronged?
Redis error: -bash: redis cli: command not found
Bytebase joins Alibaba cloud polardb open source database community
16 excellent business process management tools
RF_ DC system clock setting gen1/gen2
JVM tuning
Bat learning notes
[cloud resident co creation] ModelBox draws your own painting across the air
基于SSM的物料管理系统(源码+文档+数据库)