Introduction of clue binary tree
The process of traversing the binary tree is to visit the nodes in the binary tree once and only once along a search path. After the accessed nodes are arranged into a linear sequence, the binary tree can be accessed through this linear sequence. At this time, the binary tree can be accessed as a linear structure (the linear sequences obtained by preorder traversal, middle order traversal, etc. are different). Therefore, two pointer fields can be added to the node of the binary list to store the pointers to the "precursor" and "follower" respectively. These pointers are called clues, and the binary tree with clues is called clue binary tree. The corresponding storage structure is called clue linked list.
The node structure in the clue linked list is defined as follows:
typedef struct BiThrNode { char data; struct BiThrNode *lchild, *rchild; //Left and right pointer struct BiThrNode *pred, *succ; //Precursor, follow-up clue }BiThrNode, *BiThrTree;
Establish a clue binary tree and traverse the clue linked list output
Taking the middle order traversal as an example, the steps to establish the clue binary tree are as follows: 1. Establish an ordinary binary tree (the paths of first order, middle order and later order can be used) 2. After establishing the binary tree, traverse it (the paths of first order, middle order and subsequent order can be used) and establish the (corresponding) clue linked list. When creating a linked list, you need to traverse the function and create the table function. The traversal function recurses according to the left root and the right root. The operation of the root is to connect them through the previous and subsequent pointers to form clues. The building table function is to set up the head node, then traverse the function to traverse the two fork tree, and at the same time, cue, finally connect the tail node with the head node. 3. Traverse the binary tree through the established middle order clue list.
#include<stdio.h> typedef struct BiThrNode { char data; struct BiThrNode *lchild, *rchild; //Left and right pointer struct BiThrNode *pred, *succ; //Precursor, follow-up clue }BiThrNode, *BiThrTree; //Establish binary tree void CreateBiTree(BiThrTree &T) { char c; scanf("%c", &c); if (c == ' ') { //If you enter a space for the first time, it means that this is an empty tree. If you enter a space in the later recursive call, it means that T - > L (R) child is NULL T = NULL; } else //If the input is a specific value, a new node is opened to input the data into the data field, and the left and right child pointers are operated recursively { T = new BiThrNode; T->data = c; CreateBiTree(T->lchild); //It is established according to the path traversed in advance, so the order of these three lines is fixed CreateBiTree(T->rchild); //At the same time, the user must input in the order of pre traversal } } void visit(char c) { printf("%c\n", c); } void InOrder(BiThrTree H) { //H is the pointer to the head node in the middle order clue list. In this algorithm, the binary tree with the root of the node H - >lchild is traversed BiThrNode *p; p = H->succ; while (p != H) { visit(p->data); p = p->succ; } } void InThreading(BiThrTree p, BiThrTree &pre) { //The binary tree indicated by the root pointer p is traversed in middle order, and cued in the traversal process //p is the current pointer and pre is the follow pointer. It traverses the whole binary tree one beat slower than p if (p) { InThreading(p->lchild, pre); //Left subtree cueing pre->succ = p; p->pred = pre; //These two steps are to establish clues, that is, to link the two nodes together pre = p; InThreading(p->rchild, pre); //Right subtree cueing } } void InOrderThreading(BiThrTree &H, BiThrTree T) { //Establish the middle order full line cable linked list of the binary tree referred to by the root pointer T, and H points to the head node of the clue linked list BiThrNode *pre; H = new BiThrNode; //Create the head node of the clue linked list H->lchild = T; H->rchild = NULL; if (!T) { H->pred = H; H->succ = H; //The clue of the empty tree head node points to the head node itself } else { pre = H; InThreading(T, pre); //The clue binary tree is traversed in middle order, and the cueing is carried out in the traversal process pre->succ = H; H->pred = pre; } } void main() { BiThrTree T = NULL; BiThrTree H = NULL; //H points to the head node of the clue linked list CreateBiTree(T); //Establish binary tree InOrderThreading(H, T); InOrder(H); }
This note is based on Yan Weimin's data structure and application algorithm course
All code works correctly on Visual Studio 2017
If there is any error, please point it out