# data structure tree [создать, три обхода, разрушение бинарных деревьев]

Definition of:

``````// узлы деревьев
typedef struct Node
{
int data;
Node* left;
Node* right;
}Node,*Tree;``````

Among them, data is the field of data,

Left and Right are their left children and their right children.

Alias: node

Alias ​​pointer: tree

Suppose we want to build a tree: Then use the code to achieve this:

``````    Node* n1 = (Node*)malloc(sizeof(Node));
Node* n2 = (Node*)malloc(sizeof(Node));
Node* n3 = (Node*)malloc(sizeof(Node));
n1->data = 5;
n1->left = n2;
n1->right = n3;
n2->data = 6;
n2->left = NULL;
n2->right = NULL;
n3->data = 7;
n3->left = NULL;
n3->right = NULL;``````

very huge.

But in fact, we can use iteration to generate a binary tree. It can be encapsulated in a function:

``````// двоичное создание деревьев
Tree createBinaryTree()
{
Node* n;
n = (Node*)malloc(sizeof(Node));
int Value;
cin >> Value;
if (Value == -1)
{
free(n);
n = NULL;
return NULL;
}
else
{
n->data = Value;
n->left = createBinaryTree();
n->right = createBinaryTree();
return n;
}
}``````

First of all, the return value of this function is a node pointer, so we also need a node pointer to get it.

First, create an empty node point and open a space for it in the heap area.

Then enter a value, whether it is a value, to determine whether node -1 has been created.

If it is -1, then first release this node and make it empty,

Then return null to finish setting the node.

If not -1 then:

First give value to this moment,

Then set the left and right nodes in turn.

In short, the first node is returned last, that is, the root, you can.

To generate a binary tree in this way, only one line of code is required in the main function:

``Tree t = createBinaryTree();``

### Travel before:

Sequence: left and right roots

``````// прелюдия к проходу
void preOrder(Tree n)
{
if (n != NULL)
{
cout << n->data << " ";
preOrder(n->left);
preOrder(n->right);
}
}``````

### Journey in the middle:

Sequence: left and right

``````// жить в предисловии
void inOrder(Tree n)
{
if (n != NULL)
{
preOrder(n->left);
cout << n->data << " ";
preOrder(n->right);
}
}``````

### Travel after booking:

Sequence: right and left root

``````// post -dorder traversal
void postOrder(Tree n)
{
if (n != NULL)
{
preOrder(n->left);
preOrder(n->right);
cout << n->data << " ";
}
}``````

Now, when you want to destroy a binary tree, that means every node needs to be released.

Then, apparently, the root node was released.

Because if the left and right trees are not released, the root node is released,

This will result in a disconnected chain, and as a result, some sub-extraction states cannot be released.

So the recursive order here: the left and right roots

``````void releaseTree(Tree t)
{
if (t != NULL)
{
releaseTree(t->left);
releaseTree(t->right);
free
t = NULL;
}
}``````

Taking this tree as an example: ``````	Tree t = createBinaryTree();
Cout << "Прелюдия к Traversal:";
preOrder
cout << endl;
Cout << "Traversing:" ;;
inOrder
cout << endl;
Cout << "Пост -последовательный обход:" ;;
postOrder
cout << endl;
releaseTree
Screenshot playback: ``````
``` ```
``` Categories Back-end Post navigation iterative 3: multiplying the entry -20121-03-02 secondsUgui exercise (2) Animate UI ```
``` ```
``` Leave a Comment CommentName Email Website Save my name, email, and website in this browser for the next time I comment. ```
``` ```
``` SearchRecent Posts“Agile Software Development: Principles, Mode, and Practice (C# . Revised Edition)” Agile Development Part 1 Part 1 MySQL Note-1 Initial MySQL WeChat applet development, database table creation, and custom cloud environments Android Studio AAPT2 error: Check the logs for details Sort Recent CommentsNo comments to show. ```
``` ```
``` © 2023 winmactool • Built with GeneratePress !function(){"use strict";if("querySelector"in document&&"addEventListener"in window){var e=document.body;e.addEventListener("mousedown",function(){e.classList.add("using-mouse")}),e.addEventListener("keydown",function(){e.classList.remove("using-mouse")})}}(); var generatepressMenu = {"toggleOpenedSubMenus":"1","openSubMenuLabel":"Open Sub-Menu","closeSubMenuLabel":"Close Sub-Menu"}; var ezTOC = {"smooth_scroll":"1","visibility_hide_by_default":"","width":"auto","scroll_offset":"30"}; ```