Linux and Data Structure 2019-3-16 Morning
1. Create a binary tree
1.1 The main step of creating a binary tree is to create left and right nodes. First, we create a binary tree statically.
- 1. Firstly, a structure is created to load the node of the tree. Secondly, a function is defined to generate the tree.
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int n_value;
struct node* p_left;
struct node* p_right;
}BinaryTree;
BinaryTree* CreateTree()
{
BinaryTree* p_tree = NULL;
p_tree = (BinaryTree*)malloc(sizeof(BinaryTree));
p_tree->n_value = 1;
p_tree->p_left = (BinaryTree*)malloc(sizeof(BinaryTree));
p_tree->p_left->n_value = 2;
p_tree->p_left->p_left = (BinaryTree*)malloc(sizeof(BinaryTree));
p_tree->p_left->p_left->n_value = 4;
p_tree->p_left->p_left->p_left = NULL;
p_tree->p_left->p_left->p_right = NULL;
p_tree->p_left->p_right = (BinaryTree*)malloc(sizeof(BinaryTree));
p_tree->p_left->p_right->n_value = 5;
p_tree->p_left->p_right->p_left = NULL;
p_tree->p_left->p_right->p_right = NULL;
p_tree->p_right = (BinaryTree*)malloc(sizeof(BinaryTree));
p_tree->p_right->n_value = 3;
p_tree->p_right->p_left = (BinaryTree*)malloc(sizeof(BinaryTree));
p_tree->p_right->p_left->n_value = 6;
p_tree->p_right->p_left->p_left = NULL;
p_tree->p_right->p_left->p_right = NULL;
p_tree->p_right->p_right = NULL;
return p_tree;
}
int main()
{
BinaryTree* p_tree = NULL;
p_tree = CreateTree();
return 0;
}
- 2. We debug the next breakpoint to determine the success of the creation.
1.2 Traversing a Binary Tree
- 1. It can be traversed by depth. There are three methods of traversing binary tree in depth: pre-order traversal, middle-order traversal and post-order traversal.
- 2. It can traverse by breadth; breadth traverses binary trees;
- 3. First, the preface traversal is carried out by recursive method.
void PreorderTraversal(BinaryTree *p_tree)
{
if(p_tree == NULL)return;
printf("%d ", p_tree->n_value);
PreorderTraversal(p_tree->p_left);
PreorderTraversal(p_tree->p_right);
}
- 4. The results obtained by using the preamble traversal are as follows: 1 24 5 3 6;
- 5. Use recursive method to complete the intermediate traversal;
void InorderTraversal(BinaryTree *p_tree)
{
if(p_tree == NULL)return;
InorderTraversal(p_tree->p_left);
printf("%d ", p_tree->n_value);
InorderTraversal(p_tree->p_right);
}
- 6. The results obtained by using intermediate order traversal are 425 163;
- 7. Complete the post-order traversal by recursion.
void LastorderTraversal(BinaryTree *p_tree)
{
if(p_tree == NULL)return;
LastorderTraversal(p_tree->p_left);
LastorderTraversal(p_tree->p_right);
printf("%d ", p_tree->n_value);
}
- 8. The results of post-order traversal are as follows: 45,226,31;
1.3 Dynamic creation of a binary tree
- 1. Recursive thinking is also needed. We need to apply for space for tree nodes to load trees, and then deal with left and right.
void DynamicCreateTree(BinaryTree** pp_tree)
{
int num;
scanf("%d ", &num);
if(num == 0)return;
*pp_tree = (BinaryTree*)malloc(sizeof(BinaryTree));
(*pp_tree)->n_value = num;
(*pp_tree)->p_left = NULL;
(*pp_tree)->p_right = NULL;
DynamicCreateTree(&((*pp_tree)->p_left));
DynamicCreateTree(&((*pp_tree)->p_right));
}
Posted by Jim02 on Fri, 22 Mar 2019 09:39:52 -0700