Turn the two-fork lookup tree into a sorted two-way list
For example:
Convert to bidirectional linked list
4=6=8=10=12=14=16
struct Bstreenode
{
int m_nvalue;//value of node
Bstreenode *m_pleft; F node
bstreenode *m_pright;//Right Child of Node
};
First, the next two-fork sort tree is described:
It would first be a two-dollar Tree, based on which it is either an empty tree or a two-dollar tree with the following properties: (1) Joz tree is not empty, the value of all nodes in the left subtree is less than the value of its root node; (2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node; (3) left, The right subtree is also a two-yuan lookup tree, respectively.
Solution Idea:
The sequence of sequential traversal is the order of sorted list, so the point of pointing is to be solved.
Well, the first thing I think about is not the path of the pointer in the traversal process (then look at someone else's code ...). )
The first thought is that the left child accesses the parent node of the current node during the sequence traversal process, so the current node and the parent node should be passed in the middle sequence traversal. This causes the root (root) node to be handled differently from the other nodes.
It was later thought that since the sequence traversal is a sorted list, the traversal process puts the address of the current access node into an array of pointers. After the traversal through this array of pointers can easily know each node's predecessor and successor nodes, and then change the node point.
Finally see someone else's code, summed up as follows:
The head pointer points to the list header, and the index pointer points to the trailing node of the list.
The left pointer of all nodes refers to the forward node, and the right pointer points to the latter node.
Therefore: (Intermediate process)
- The left pointer of the current node points to the footer node;
- The right pointer of the footer node points to the current node;
- Update, the tail node points to the current node;
(For the table header, that is, the tail node points to null), initialize the head node.
The code is as follows:
void Converttodoublelist (bstreenode* pcurrent)
{
pcurrent->m_pleft=pindex;
if (Pindex = = NULL)
{
phead=pcurrent
}
else
{
pindex->m_pright=pcurrent;
}
pindex=pcurrent;
}
To determine whether two linked lists Intersect
Give the two one-way linked list of head pointers, such as H1,H2, to determine whether the two linked lists intersect.
To simplify the problem, we assume that both lists have no loops.
Problem extension:
If you need to find the first node column where two linked lists intersect
Linked list definition
typedef struct NODE
{
int data;
struct node * next;
} List;
- If there is no ring, then traverse two linked list to the tail node respectively;
- If two lists intersect, then the tail node must intersect;
- If two lists do not intersect, then the tail node must not intersect;
int Isjoinednocylic (List * h1,list * h2)
{while
(H1!= NULL)
h1 = h1->next;
while (H2!= NULL)
h2 = h2->next;
return H1 = = H2;
What if you need to find the first node column where the two linked lists intersect?
On the internet to see such a solution: set two pointers fast and slow, the initial values are pointing to the head, slow each time before further, fast each step forward two steps, if the linked list exists ring, then fast must first enter the ring, and slow after the ring, two pointers must meet. (Of course, the fast header to the tail is null, then the ring-free list), so you can determine whether two linked lists Intersect, the program is as follows:
int iscycle (List * h)
{
list * p1, * p2;
P1 = P2 = h;
int flag;
while (P2!= null && p2->next!= null)
{
P1 = p1->next;
P2 = p2->next->next;
if (P1 = = p2)
{
flag = 1;
break;
}
}
Flag = 0;
return flag;
}
Below see how to find the entrance of the ring, when fast and slow meet, slow certainly did not walk through the list, and fast has been circulating in the Loop N Circle (1<=n). Assuming that slow walked the s step, then fast went 2s steps (fast steps are also equal to S plus n loops with multiple turns on the ring), and the ring length is R, then:
Set the whole chain table long L, the entry ring and meet point distance is X, the distance from the beginning to the ring entry point is a.
A + x = Nr
A + x = (n–1) r +r = (n-1) R + l-a
a = (n-1) R + (l–a–x)
(l–a–x) is the distance from the point of encounter to the entry point of the ring, so, from the head of the chain to the ring entry point equals (n-1) loop internal Loop + meet point to the ring entry point (from the meeting point back to the distance from the loop back to the entry point), so we from the head of the chain, and meet the point of a separate pointer, each one step, Two pointers must meet, and the meeting point is the ring entry point, which is the first identical node of the two list. The procedure is described as follows:
List * isjoined (list * h1,list * H2)
{
list * PH1,*P1,*P2;
int flag;
PH1 = H1;
while (Ph1->next!= NULL)
ph1 = ph1->next;
Ph1->next = h2;
if (0 = = iscycle (h1))
{
flag = 0;
}
else
{
P1 = h1;
while (P1!= p2)
{
P1 = p1->next;
P2 = p2->next;
}
flag = p1;
}
return flag;
}