The clue binary tree of the dialogue between a and B

Time：2022-1-2

A: What is a clue binary tree?

B: Don’t worry, do you know what the storage structure of the definition of binary tree is?

A: Of course, it is a structure containing one value field and two pointer fields, as shown in the figure below

``````typedef struct node
{
char data;
struct node *left,*right;
}Tree``````

B: Yes, it is. We can see from the code that we can find the left and right children of a node through the left and right pointers, right? What if we want to find the precursor and successor of a node?

A: It’s easy to find left and right children, huh? Preamble and successor? What does this mean?

B: We know that a tree has three kinds of traversal. For example, the preorder traversal of a tree is ABC. It is easy to see that when the precursor node of B is a, the successor node of B is C. when we traverse the whole binary tree, it is easy to find out the precursor and successor of a node. What should you do if I need to directly let you use code to find out the precursor and successor of a node?

A: Add new pointer?

B: No, no, think about how many pointers we created when we built a tree with n nodes?

A: A node has two pointers, of course 2n pointers

B: How many pointers did we use?

There are n-1 branches in the two fork tree of A:n nodes. Then we use n-1 pointer, and 2n- (n-1) pointer is not used.

B: By the way, we can use n + 1 pointers to point to the precursor and successor of a node, and also use space

A: I’m a little confused. What should I do?

B: Because our node has two pointers, how do we know whether its left and right children point to the precursor or the successor? On the original basis, we add ltga and rtag to judge the direction of the left and right pointers

When LTAG = = 0, the left pointer of the node points to the left child

When LTAG = = 1, the left pointer representing the node points to the precursor node

Similarly, rtag

A: I see! So how do we achieve it

B: When we traverse the binary tree, we will know the precursor of the node, and then we can assign values to LTAG and rtag

A: I assign values to LTAG and rtag through middle order traversal

``````#include
#include
using namespace std;

typedef struct node
{
char data;
struct node *left,*right;
int ltag,rlag;
}ThreadTree;// Clue binary tree

Threadtree * create() // create binary tree recursively
{
char a;
cin>>a;
if(a=='#')
t=NULL;
else{
t->data=a;
t->left=t->right=NULL;
t->left=create();
t->right=create();
}
return t;
}

Void Zhongxu (threadtree * t) // medium order clue binary tree
{
ThreadTree *pre;// Representative successor
if(t)
{
Zhongxu(t->left);
If (T - > left = = null) // left is the precursor
{
t->ltag=1;
t->left=pre;
}
If (pre - > right = = null) // the right is the successor
{
pre->rlag=1;
pre->right=t;
}
pre=t;
cout<data;
Zhongxu(t->right);
}
}
int main()
{
t=create();// Create binary tree
Zhongxu(t);// Middle order traversal binary tree
}``````

A: This is the process of clue binary tree. Now you know what a clue binary tree is

B: I see! Clue binary tree is to use the free pointer of binary tree to point it to the predecessor and successor of a node, so that we can quickly find the predecessor and successor of a node

A: Yes, that’s it. Don’t forget that clue binary trees have three structures~

Explanation of websocket heartbeat reconnection

Recently, websocket has been used in the development of applet. The applet provides corresponding native API, which is different from H5 API. Therefore, some mature class libraries of popular H5 are difficult to use, and the native API has some defects, so it implements a set of heartbeat reconnection mechanism by itself. First, let’s briefly […]