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:
![data structure tree [создать, три обхода, разрушение бинарных деревьев] 3 7782e035a5d37c993bdc7d8037b26bc5](https://winmactool.com/wp-content/uploads/2022/12/7782e035a5d37c993bdc7d8037b26bc5.png)