2021-07-03 05:07:57 +01:00
<!DOCTYPE HTML>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html;charset=US-ASCII" >
2021-08-17 07:47:37 +01:00
< meta name = "generator" content = "ZealOS V1.00" >
2021-07-03 05:07:57 +01:00
< style type = "text/css" >
2021-07-29 03:20:15 +01:00
body {background-color:#fef1f0;}
.cF0{color:#000000;background-color:#fef1f0;}
.cF1{color:#0148a4;background-color:#fef1f0;}
.cF2{color:#3b7901;background-color:#fef1f0;}
.cF3{color:#057c7e;background-color:#fef1f0;}
.cF4{color:#bb2020;background-color:#fef1f0;}
.cF5{color:#9e42ae;background-color:#fef1f0;}
.cF6{color:#b57901;background-color:#fef1f0;}
.cF7{color:#b2b6af;background-color:#fef1f0;}
.cF8{color:#555753;background-color:#fef1f0;}
.cF9{color:#678fbb;background-color:#fef1f0;}
.cFA{color:#82bc49;background-color:#fef1f0;}
.cFB{color:#0097a2;background-color:#fef1f0;}
.cFC{color:#e26a6a;background-color:#fef1f0;}
.cFD{color:#c671bc;background-color:#fef1f0;}
.cFE{color:#c7ab00;background-color:#fef1f0;}
.cFF{color:#fef1f0;background-color:#fef1f0;}
2021-07-03 05:07:57 +01:00
< / style >
< / head >
< body >
2021-07-05 01:12:38 +01:00
< pre style = "font-family:monospace;font-size:12pt" >
2021-07-04 23:11:34 +01:00
< a name = "l1" > < / a > < span class = cF0 > #< / span > < span class = cF1 > define< / span > < span class = cF0 > N < / span > < span class = cFE > 32< / span > < span class = cF0 >
2021-07-03 05:07:57 +01:00
< a name = "l2" > < / a >
< a name = "l3" > < / a > < / span > < span class = cF1 > class< / span > < span class = cF0 > Node
< a name = "l4" > < / a > {
2021-07-04 23:11:34 +01:00
< a name = "l5" > < / a > Node *left, *right;
< a name = "l6" > < / a > < / span > < span class = cF9 > I64< / span > < span class = cF0 > n;
2021-07-03 05:07:57 +01:00
< a name = "l7" > < / a > };
< a name = "l8" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l9" > < / a > < / span > < span class = cF9 > I64< / span > < span class = cF0 > n1, n2, common_ancestor;
2021-07-03 05:07:57 +01:00
< a name = "l10" > < / a > Node *root;
< a name = "l11" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l12" > < / a > #< / span > < span class = cF1 > define< / span > < span class = cF0 > X_SPACING < / span > < span class = cFE > 16< / span > < span class = cF0 >
< a name = "l13" > < / a > #< / span > < span class = cF1 > define< / span > < span class = cF0 > Y_SPACING < / span > < span class = cFE > 45< / span > < span class = cF0 >
2021-07-03 05:07:57 +01:00
< a name = "l14" > < / a > #< / span > < span class = cF1 > define< / span > < span class = cF0 > ARROW_SPACING < / span > < span class = cFE > 3< / span > < span class = cF0 >
< a name = "l15" > < / a >
< a name = "l16" > < / a > < / span > < span class = cF1 > U0< / span > < span class = cF0 > ShowTree(< / span > < span class = cF9 > CDC< / span > < span class = cF0 > *dc, Node *tmpn, < / span > < span class = cF9 > I64< / span > < span class = cF0 > *_node_x, < / span > < span class = cF9 > I64< / span > < span class = cF0 > *_tree_x, < / span > < span class = cF9 > I64< / span > < span class = cF0 > y)
< a name = "l17" > < / a > {
2021-07-04 23:11:34 +01:00
< a name = "l18" > < / a > < / span > < span class = cF9 > I64< / span > < span class = cF0 > node_x;
2021-07-03 05:07:57 +01:00
< a name = "l19" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l20" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (tmpn)
< a name = "l21" > < / a > < / span > < span class = cF7 > {< / span > < span class = cF0 >
< a name = "l22" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (tmpn-> left)
< a name = "l23" > < / a > {
< a name = "l24" > < / a > ShowTree(dc, tmpn-> left, & node_x, _tree_x, y + Y_SPACING);
< a name = "l25" > < / a > dc-> color = < / span > < span class = cF3 > BLUE< / span > < span class = cF0 > ;
< a name = "l26" > < / a > < / span > < span class = cF5 > GrArrow3< / span > < span class = cF0 > (dc, *_tree_x, y, < / span > < span class = cFE > 0< / span > < span class = cF0 > , node_x + ARROW_SPACING, y + Y_SPACING - ARROW_SPACING, < / span > < span class = cFE > 0< / span > < span class = cF0 > );
< a name = "l27" > < / a > }
< a name = "l28" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (tmpn-> n == n1 || tmpn-> n == n2)
< a name = "l29" > < / a > {
< a name = "l30" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (tmpn-> n == common_ancestor)
< a name = "l31" > < / a > dc-> color = < / span > < span class = cF3 > YELLOW< / span > < span class = cF0 > ;
< a name = "l32" > < / a > < / span > < span class = cF1 > else< / span > < span class = cF0 >
< a name = "l33" > < / a > dc-> color = < / span > < span class = cF3 > RED< / span > < span class = cF0 > ;
< a name = "l34" > < / a > }
< a name = "l35" > < / a > < / span > < span class = cF1 > else< / span > < span class = cF0 > < / span > < span class = cF1 > if< / span > < span class = cF0 > (tmpn-> n == common_ancestor)
< a name = "l36" > < / a > dc-> color = < / span > < span class = cF3 > GREEN< / span > < span class = cF0 > ;
< a name = "l37" > < / a > < / span > < span class = cF1 > else< / span > < span class = cF0 >
< a name = "l38" > < / a > dc-> color = < / span > < span class = cF3 > BLUE< / span > < span class = cF0 > ;
2021-07-03 05:07:57 +01:00
< a name = "l39" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l40" > < / a > *_node_x = *_tree_x;
< a name = "l41" > < / a > < / span > < span class = cF5 > GrPrint< / span > < span class = cF0 > (dc, *_node_x, y, < / span > < span class = cF6 > " %d" < / span > < span class = cF0 > , tmpn-> n);
< a name = "l42" > < / a > *_tree_x += X_SPACING;
2021-07-03 05:07:57 +01:00
< a name = "l43" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l44" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (tmpn-> right)
< a name = "l45" > < / a > {
< a name = "l46" > < / a > ShowTree(dc, tmpn-> right, & node_x, _tree_x, y + Y_SPACING);
< a name = "l47" > < / a > dc-> color = < / span > < span class = cF3 > BLUE< / span > < span class = cF0 > ;
< a name = "l48" > < / a > < / span > < span class = cF5 > GrArrow3< / span > < span class = cF0 > (dc, *_node_x, y, < / span > < span class = cFE > 0< / span > < span class = cF0 > , node_x - ARROW_SPACING, y + Y_SPACING - ARROW_SPACING, < / span > < span class = cFE > 0< / span > < span class = cF0 > );
< a name = "l49" > < / a > }
< a name = "l50" > < / a > < / span > < span class = cF7 > }< / span > < span class = cF0 >
2021-07-03 05:07:57 +01:00
< a name = "l51" > < / a > }
< a name = "l52" > < / a >
< a name = "l53" > < / a > < / span > < span class = cF1 > U0< / span > < span class = cF0 > < / span > < span class = cF5 > DrawIt< / span > < span class = cF0 > (< / span > < span class = cF9 > CTask< / span > < span class = cF0 > *, < / span > < span class = cF9 > CDC< / span > < span class = cF0 > *dc)
< a name = "l54" > < / a > {
2021-07-04 23:11:34 +01:00
< a name = "l55" > < / a > < / span > < span class = cF9 > I64< / span > < span class = cF0 > node_x = < / span > < span class = cFE > 0< / span > < span class = cF0 > , tree_x = < / span > < span class = cFE > 0< / span > < span class = cF0 > ;
2021-07-03 05:07:57 +01:00
< a name = "l56" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l57" > < / a > ShowTree(dc, root, & node_x, & tree_x, < / span > < span class = cFE > 20< / span > < span class = cF0 > );
2021-07-03 05:07:57 +01:00
< a name = "l58" > < / a > }
< a name = "l59" > < / a >
< a name = "l60" > < / a > < / span > < span class = cF1 > U0< / span > < span class = cF0 > TreeAdd(Node **_root, Node *tmpn)
< a name = "l61" > < / a > {
2021-07-04 23:11:34 +01:00
< a name = "l62" > < / a > Node *root = *_root;
2021-07-03 05:07:57 +01:00
< a name = "l63" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l64" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (!root)
< a name = "l65" > < / a > *_root = tmpn;
< a name = "l66" > < / a > < / span > < span class = cF1 > else< / span > < span class = cF0 > < / span > < span class = cF1 > if< / span > < span class = cF0 > (tmpn-> n == root-> n)
< a name = "l67" > < / a > < / span > < span class = cF5 > Free< / span > < span class = cF0 > (tmpn);
< a name = "l68" > < / a > < / span > < span class = cF1 > else< / span > < span class = cF0 > < / span > < span class = cF1 > if< / span > < span class = cF0 > (tmpn-> n < root-> n)
< a name = "l69" > < / a > TreeAdd(& root-> left, tmpn);
< a name = "l70" > < / a > < / span > < span class = cF1 > else< / span > < span class = cF0 >
< a name = "l71" > < / a > TreeAdd(& root-> right, tmpn);
2021-07-03 05:07:57 +01:00
< a name = "l72" > < / a > }
< a name = "l73" > < / a >
< a name = "l74" > < / a > < / span > < span class = cF1 > U0< / span > < span class = cF0 > TreeNew()
< a name = "l75" > < / a > {
2021-07-04 23:11:34 +01:00
< a name = "l76" > < / a > < / span > < span class = cF9 > I64< / span > < span class = cF0 > i;
< a name = "l77" > < / a > Node *tmpn;
2021-07-03 05:07:57 +01:00
< a name = "l78" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l79" > < / a > < / span > < span class = cF1 > for< / span > < span class = cF0 > (i = < / span > < span class = cFE > 0< / span > < span class = cF0 > ; i < N; i++)
< a name = "l80" > < / a > < / span > < span class = cF7 > {< / span > < span class = cF0 >
< a name = "l81" > < / a > tmpn = < / span > < span class = cF5 > CAlloc< / span > < span class = cF0 > (< / span > < span class = cF1 > sizeof< / span > < span class = cF7 > (< / span > < span class = cF0 > Node< / span > < span class = cF7 > )< / span > < span class = cF0 > );
< a name = "l82" > < / a > tmpn-> n = < / span > < span class = cF5 > RandU16< / span > < span class = cF0 > % N;
2021-07-03 05:07:57 +01:00
< a name = "l83" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l84" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (i == N - < / span > < span class = cFE > 1< / span > < span class = cF0 > )
< a name = "l85" > < / a > n1 = tmpn-> n;
< a name = "l86" > < / a > < / span > < span class = cF1 > else< / span > < span class = cF0 > < / span > < span class = cF1 > if< / span > < span class = cF0 > (i == N - < / span > < span class = cFE > 2< / span > < span class = cF0 > )
< a name = "l87" > < / a > n2 = tmpn-> n;
2021-07-03 05:07:57 +01:00
< a name = "l88" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l89" > < / a > TreeAdd(& root, tmpn);
< a name = "l90" > < / a > < / span > < span class = cF5 > Sleep< / span > < span class = cF0 > (< / span > < span class = cFE > 50< / span > < span class = cF0 > );
< a name = "l91" > < / a > < / span > < span class = cF7 > }< / span > < span class = cF0 >
2021-07-03 05:07:57 +01:00
< a name = "l92" > < / a > }
< a name = "l93" > < / a >
< a name = "l94" > < / a > < / span > < span class = cF1 > U0< / span > < span class = cF0 > TreeCommonAncestorFind(Node *root)
< a name = "l95" > < / a > {
2021-07-04 23:11:34 +01:00
< a name = "l96" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (root & & root-> n != n1 & & root-> n != n2)
< a name = "l97" > < / a > < / span > < span class = cF7 > {< / span > < span class = cF0 >
< a name = "l98" > < / a > common_ancestor = root-> n;
< a name = "l99" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (n1 < root-> n & & n2 < root-> n)
< a name = "l100" > < / a > TreeCommonAncestorFind(root-> left);
< a name = "l101" > < / a > < / span > < span class = cF1 > else< / span > < span class = cF0 > < / span > < span class = cF1 > if< / span > < span class = cF0 > (n1 > root-> n & & n2 > root-> n)
< a name = "l102" > < / a > TreeCommonAncestorFind(root-> right);
< a name = "l103" > < / a > < / span > < span class = cF7 > }< / span > < span class = cF0 >
2021-07-03 05:07:57 +01:00
< a name = "l104" > < / a > }
< a name = "l105" > < / a >
< a name = "l106" > < / a > < / span > < span class = cF1 > U0< / span > < span class = cF0 > TreeCommonAncestor()
< a name = "l107" > < / a > {< / span > < span class = cF2 > //Make tree and find common ancestor to n1 & n2.< / span > < span class = cF0 >
2021-07-04 23:11:34 +01:00
< a name = "l108" > < / a > root = < / span > < span class = cF3 > NULL< / span > < span class = cF0 > ;
< a name = "l109" > < / a > n1 = n2 = common_ancestor = < / span > < span class = cFE > 0< / span > < span class = cF0 > ;
2021-07-03 05:07:57 +01:00
< a name = "l110" > < / a >
2021-07-26 20:29:49 +01:00
< a name = "l111" > < / a > < / span > < span class = cF5 > SettingsPush< / span > < span class = cF0 > ; < / span > < span class = cF2 > //See < / span > < a href = "https://zeal-operating-system.github.io/ZealOS/System/TaskSettings.CC.html#l3" > < span class = cF4 > SettingsPush< / span > < / a > < span class = cF0 >
2021-07-04 23:11:34 +01:00
< a name = "l112" > < / a > < / span > < span class = cF5 > Fs< / span > < span class = cF0 > -> draw_it = & < / span > < span class = cF5 > DrawIt< / span > < span class = cF0 > ;
< a name = "l113" > < / a > < / span > < span class = cF5 > DocClear< / span > < span class = cF0 > ;
< a name = "l114" > < / a > < / span > < span class = cF6 > " Scroll with {CTRL-Left Grab}.\n" < / span > < span class = cF0 > ;
< a name = "l115" > < / a > < / span > < span class = cF1 > try< / span > < span class = cF0 >
< a name = "l116" > < / a > < / span > < span class = cF7 > {< / span > < span class = cF0 >
< a name = "l117" > < / a > TreeNew;
< a name = "l118" > < / a > TreeCommonAncestorFind(root);
< a name = "l119" > < / a > < / span > < span class = cF5 > PressAKey< / span > < span class = cF0 > ;
< a name = "l120" > < / a > < / span > < span class = cF7 > }< / span > < span class = cF0 >
< a name = "l121" > < / a > < / span > < span class = cF1 > catch< / span > < span class = cF0 >
< a name = "l122" > < / a > < / span > < span class = cF5 > PutExcept< / span > < span class = cF0 > ;
< a name = "l123" > < / a > < / span > < span class = cF5 > SettingsPop< / span > < span class = cF0 > ;
2021-07-03 05:07:57 +01:00
< a name = "l124" > < / a > }
< a name = "l125" > < / a >
< a name = "l126" > < / a > TreeCommonAncestor;
< a name = "l127" > < / a > < / span > < span class = cF2 > /*< / span > < span class = cF0 >
< a name = "l128" > < / a > < / span > < span class = cF2 > Be careful with recursive routines in ZealOS< / span > < span class = cF0 >
< a name = "l129" > < / a > < / span > < span class = cF2 > because the stack does not grow and will overflow.< / span > < span class = cF0 >
< a name = "l130" > < / a >
2021-07-26 20:29:49 +01:00
< a name = "l131" > < / a > < / span > < span class = cF2 > See < / span > < a href = "https://zeal-operating-system.github.io/ZealOS/Demo/StackGrow.CC.html#l1" > < span class = cF4 > ::/Demo/StackGrow.CC< / span > < / a > < span class = cF2 > .< / span > < span class = cF0 >
2021-07-03 05:07:57 +01:00
< a name = "l132" > < / a > < / span > < span class = cF2 > */< / span > < span class = cF0 >
< / span > < / pre > < / body >
< / html >