Data structure, binary tree, front, middle and back order traversal

         Types of Binary Trees

 

 optimal binary tree

 

Optimal Binary Tree Drawing

  1. to sort
  2. Take the sum of the smallest two values ​​to get a new value and add it to the sort
  3. Repeat 1, 2

        Pre-order, in-order, and post-order traversals are commonly used traversal methods in tree data structures (such as binary trees), which are used to traverse the nodes of the tree in a specific order. These traversal methods have different purposes in different applications.

Here is an explanation of these traversals:

1. Preorder Traversal:
    - Starting from the root node, traverse the nodes of the tree in the order of "root node - left subtree - right subtree".
    - For each node, first visit the node, then recursively traverse the left subtree, and finally recursively traverse the right subtree.
    - Preorder traversal can be used to copy the entire tree.

Function method:

void PREORDER(bitree *r)
{
    if(r==NULL) return;//空树返回
    printf("%c",r->data); //先访问当前节点
    PREORDER(r->lchild);   //再访问该节点的左子树
    PREORDER(r->rchild);   //最后访问该节点右子树
}

Image method: Look at the order of traversing the ring to determine the order of preorder traversal.

2. Inorder Traversal:
    - Starting from the root node, traverse the nodes of the tree in the order of "left subtree - root node - right subtree".
    - For each node, first recursively traverse the left subtree, then visit the node, and finally recursively traverse the right subtree.
    - The results obtained by inorder traversal in the binary search tree are ordered.

void INORDER(bitree *r)
{
    if(r==NULL) return;//空树返回
    INORDER(r->lchild);   //先访问该节点的左子树
    printf("%c",r->data); //再访问当前节点
    INORDER(r->rchild);   //最后访问该节点右子树
}

Image method: Look at the order of traversing the ring to determine the order of inorder traversal. 

 

3. Postorder Traversal:
    - Starting from the root node, traverse the nodes of the tree in the order of "left subtree - right subtree - root node".
    - For each node, first recursively traverse the left subtree, then recursively traverse the right subtree, and finally visit the node.
    - Post-order traversal is often used for operations such as memory recovery or resource release.

void POSTORDER(bitree *r)
{
    if(r==NULL) return;//空树返回
    POSTORDER(r->lchild);   //先访问该节点的左子树
    POSTORDER(r->rchild);   //最后访问该节点右子树
    printf("%c",r->data); //再访问当前节点
    
}

  Image method: Look at the order of traversing the ring to determine the order of post-order traversal. 

        These traversal methods are a kind of depth-first traversal (Depth-First Traversal). Depth-first traversal starts at the root node, visits the branches of the tree as deeply as possible, and then backtracks to other branches. The opposite is breadth-first traversal (Breadth-First Traversal), which starts from the root node and traverses the nodes of the tree hierarchically.

Guess you like

Origin blog.csdn.net/qq_52119661/article/details/132411347