Recently I want to familiarize myself with the basic data structures and algorithms. So I wrote a binary tree program with simple functions. Only Binary Tree creation and traversal are supported. In this process, I found some details that I didn't notice at ordinary times. I wrote them as a summary and shared them with you.
The details to be discussed and encountered are as follows:
(1) Common Fixed Thinking: If a pointer is passed as a parameter, you do not need to assign another value because the value pointed to by the pointer will be changed synchronously. However, you have considered it, what if the value of the pointer variable is changed? What should I do?
(2) The usage is not commonly used. Have you used the pointer variable reference?
Prerequisites:
(1) considering that I have been obfuscation with C and C ++, I plan to use pure C to write this simple algorithm program. Therefore, the C compiler is used.
(2) I plan to write a series of articles to summarize and analyze the differences between C and C ++.
(3) In addition, because it is a program written in practice, there is no way to add memory for release.
Program header definition and general methods:
#include <stdio.h><malloc.h> TRUE 1 FALSE -1 _BinaryTreeNode* _BinaryTreeNode** PreOrderTraverse(BinareTreeNode* pNode, (* (NULL !=->->->
The program I first wrote is as follows:
PreOrderCreateBinaryTree(BinareTreeNode*, & ( ==== (BinareTreeNode*)malloc(->elem =->->* pHeadNode =
Symptoms:
In fact, the pHeadNode value in the main function is always NULL. As a result, no value is output in PreOrderTraverse.
Input: ABD # E # C # output after traversal: None, because the input parameter value pHeadNode is NULL.
Problem Analysis:
Why?
The first question discussed in this article has surfaced: the Common Fixed Thinking: do we not need to assign a value if the pointer is passed as a parameter?
Analysis:
Although it is Pointer passing, the pHeadNode in the main function is a pointer variable. The value of the pointer variable is null and there is no pointing value.
The shape parameter pNode in the PreOrderCreateBinaryTree function is another pointer variable. Its value was input from the main function at the beginning, which is also NULL. Then, pNode is assigned a value after malloc.
The problem is, is the pHeadNode in the main function synchronized? Doesn't it mean that the pointer is passed, will the value change together?
In fact, a low-level error is made here, that is, when the pointer is used as a function parameter and the pointer is not re-assigned, the value pointed to by the pointer must be changed synchronously, however, if the pointer is used as a variable and its value changes, the parameter source will not change.
For example:
( FuncChangeObj(**pInt)++ FuncNoChangeObj(*= (*)malloc((*pInt =
In the preceding example, the binary tree program does not traverse the binary tree in sequence, because the root node of the binary tree is not returned when the binary tree is created in sequence, resulting in pHeadNode being NULL all the time.
So, isn't the above program actually successfully created a binary tree, but does not return the root node of the tree? Otherwise, because of the same reason, this binary tree is not successfully created, and there is no association between each tree node, all of which are isolated nodes.
Specific solution:
How to modify it?
The answer is as follows:
Method 1: The function returns a pointer and assigns a value to the source pointer.
BinareTreeNode* PreOrderCreateBinaryTree(BinareTreeNode*, & ( ==== (BinareTreeNode*)malloc(->elem =pNode->left = PreOrderCreateBinaryTree(pNode->->right = PreOrderCreateBinaryTree(pNode->* pHeadNode ==
Method 2: Use the pointer reference to pass the value, that is, the pointer variable itself is referenced, instead of the pointer variable value.
Note: This method is an explanation of the second problem, but you must note that this method cannot be compiled successfully in the C language compiler, because there is no reference concept in the C language.
PreOrderCreateBiTree2(BiTree&, & ( ==== (BinareTreeNode *)malloc(->elem =->->=