Basic algorithm - traversal of the tree

xiaoxiao2021-03-06  98

Sixth, the traversal of the tree

1. Required sequence in pre-sequence

Procedure Solve (Pre, Mid: String);

VAR i: integer;

Begin

IF (pre= ') or (MID =' ') THEN EXIT;

I: = POS (Pre [1], MID);

Solve (Copy (Pre, 2, I), COPY (MID, 1, I-1);

Solve (Copy (Pre, I 1, Length (Pre) -i), Copy (MID, I 1, Length (MID) -i));

Post: = post pre [1]; {plus root, post-recursive end traversal traversal}

END;

2. Present sequence prequet sequence

Procedure Solve (MID, Post: String);

VAR i: integer;

Begin

IF (MID = '') or (post = '') THEN EXIT;

I: = POS (Post [Length (POST)], MID);

Pre: = pre post [length (post)]; {plus root, after recursive end, pre-sequence traversal}

Solve (Copy (MID, 1, I-1), COPY (POST, 1, I-1);

Solve (Copy (MID, I 1, Length (MID) -i), Copy (POST, I, Length (POST) -i);

END;

3. One of known pre-sequences

Function OK (S1, S2: String): Boolean;

Var i, l: integer; p: boolean;

Begin

OK: = true;

L: = Length (S1);

For i: = 1 to l do begin

P: = false;

For j: = 1 to l do

IF S1 [I] = S2 [J] THEN P: = TRUE

IF NOT; End;

END;

END;

Procedure Solve (Pre, Post: String);

VAR i: integer;

Begin

IF (pre = '') or (post = '') THEN EXIT;

I: = 0;

Repeat

INC (I);

Until OK (Copy (Pre, 2, I), Copy (POST, 1, I));

Solve (Copy (Pre, 2, I), COPY (POST, 1, I));

Midstr: = Midstr pre [1];

Solve (Copy (Pre, I 2, Length (Pre) -i-1), Copy (POST, I 1, Length (POST) -i-1);

END;

转载请注明原文地址:https://www.9cbs.com/read-105357.html

New Post(0)