templeos-info/public/Wb/Demo/Graphics/CommonAncestor.HC.HTML

152 lines
9.8 KiB
Plaintext
Raw Normal View History

2024-03-24 21:24:44 +00:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="TempleOS V5.03">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/style/templeos.css">
<script src="/script/templeos.js"></script>
<style type="text/css">
.cF0{color:#000000;background-color:#ffffff;}
.cF1{color:#0000aa;background-color:#ffffff;}
.cF2{color:#00aa00;background-color:#ffffff;}
.cF3{color:#00aaaa;background-color:#ffffff;}
.cF4{color:#aa0000;background-color:#ffffff;}
.cF5{color:#aa00aa;background-color:#ffffff;}
.cF6{color:#aa5500;background-color:#ffffff;}
.cF7{color:#aaaaaa;background-color:#ffffff;}
.cF8{color:#555555;background-color:#ffffff;}
.cF9{color:#5555ff;background-color:#ffffff;}
.cFA{color:#55ff55;background-color:#ffffff;}
.cFB{color:#55ffff;background-color:#ffffff;}
.cFC{color:#ff5555;background-color:#ffffff;}
.cFD{color:#ff55ff;background-color:#ffffff;}
.cFE{color:#ffff55;background-color:#ffffff;}
.cFF{color:#ffffff;background-color:#ffffff;}
</style>
</head>
<body>
<pre id="content">
<a name="l1"></a><span class=cF0>#</span><span class=cF1>define</span><span class=cF0> N 32
<a name="l2"></a>
<a name="l3"></a></span><span class=cF1>class</span><span class=cF0> Node
<a name="l4"></a>{
<a name="l5"></a> Node *left,*right;
<a name="l6"></a> </span><span class=cF9>I64</span><span class=cF0> n;
<a name="l7"></a>};
<a name="l8"></a>
<a name="l9"></a></span><span class=cF9>I64</span><span class=cF0> n1,n2,common_ancestor;
<a name="l10"></a>Node *root;
<a name="l11"></a>
<a name="l12"></a>#</span><span class=cF1>define</span><span class=cF0> X_SPACING 16
<a name="l13"></a>#</span><span class=cF1>define</span><span class=cF0> Y_SPACING 45
<a name="l14"></a>#</span><span class=cF1>define</span><span class=cF0> ARROW_SPACING 3
<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>{
<a name="l18"></a> </span><span class=cF9>I64</span><span class=cF0> node_x;
<a name="l19"></a> </span><span class=cF1>if</span><span class=cF0> (tmpn) </span><span class=cF7>{</span><span class=cF0>
<a name="l20"></a> </span><span class=cF1>if</span><span class=cF0> (tmpn-&gt;left) {
<a name="l21"></a> ShowTree(dc,tmpn-&gt;left,&amp;node_x,_tree_x,y+Y_SPACING);
<a name="l22"></a> dc-&gt;color=</span><span class=cF3>BLUE</span><span class=cF0>;
<a name="l23"></a> </span><span class=cF5>GrArrow3</span><span class=cF0>(dc,*_tree_x,y,0,
<a name="l24"></a> node_x+ARROW_SPACING,y+Y_SPACING-ARROW_SPACING,0);
<a name="l25"></a> }
<a name="l26"></a> </span><span class=cF1>if</span><span class=cF0> (tmpn-&gt;n==n1 || tmpn-&gt;n==n2) {
<a name="l27"></a> </span><span class=cF1>if</span><span class=cF0> (tmpn-&gt;n==common_ancestor)
<a name="l28"></a> dc-&gt;color=</span><span class=cF3>YELLOW</span><span class=cF0>;
<a name="l29"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l30"></a> dc-&gt;color=</span><span class=cF3>RED</span><span class=cF0>;
<a name="l31"></a> } </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (tmpn-&gt;n==common_ancestor)
<a name="l32"></a> dc-&gt;color=</span><span class=cF3>GREEN</span><span class=cF0>;
<a name="l33"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l34"></a> dc-&gt;color=</span><span class=cF3>BLUE</span><span class=cF0>;
<a name="l35"></a>
<a name="l36"></a> *_node_x=*_tree_x;
<a name="l37"></a> </span><span class=cF5>GrPrint</span><span class=cF0>(dc,*_node_x,y,</span><span class=cF6>&quot;%d&quot;</span><span class=cF0>,tmpn-&gt;n);
<a name="l38"></a> *_tree_x+=X_SPACING;
<a name="l39"></a>
<a name="l40"></a> </span><span class=cF1>if</span><span class=cF0> (tmpn-&gt;right) {
<a name="l41"></a> ShowTree(dc,tmpn-&gt;right,&amp;node_x,_tree_x,y+Y_SPACING);
<a name="l42"></a> dc-&gt;color=</span><span class=cF3>BLUE</span><span class=cF0>;
<a name="l43"></a> </span><span class=cF5>GrArrow3</span><span class=cF0>(dc,*_node_x,y,0,
<a name="l44"></a> node_x-ARROW_SPACING,y+Y_SPACING-ARROW_SPACING,0);
<a name="l45"></a> }
<a name="l46"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l47"></a>}
<a name="l48"></a>
<a name="l49"></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="l50"></a>{
<a name="l51"></a> </span><span class=cF9>I64</span><span class=cF0> node_x=0,tree_x=0;
<a name="l52"></a> ShowTree(dc,root,&amp;node_x,&amp;tree_x,20);
<a name="l53"></a>}
<a name="l54"></a>
<a name="l55"></a></span><span class=cF1>U0</span><span class=cF0> TreeAdd(Node **_root,Node *tmpn)
<a name="l56"></a>{
<a name="l57"></a> Node *root=*_root;
<a name="l58"></a> </span><span class=cF1>if</span><span class=cF0> (!root)
<a name="l59"></a> *_root=tmpn;
<a name="l60"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (tmpn-&gt;n==root-&gt;n)
<a name="l61"></a> </span><span class=cF5>Free</span><span class=cF0>(tmpn);
<a name="l62"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (tmpn-&gt;n&lt;root-&gt;n)
<a name="l63"></a> TreeAdd(&amp;root-&gt;left,tmpn);
<a name="l64"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l65"></a> TreeAdd(&amp;root-&gt;right,tmpn);
<a name="l66"></a>}
<a name="l67"></a>
<a name="l68"></a></span><span class=cF1>U0</span><span class=cF0> TreeNew()
<a name="l69"></a>{
<a name="l70"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l71"></a> Node *tmpn;
<a name="l72"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;N;i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l73"></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="l74"></a> tmpn-&gt;n=</span><span class=cF5>RandU16</span><span class=cF0>%N;
<a name="l75"></a>
<a name="l76"></a> </span><span class=cF1>if</span><span class=cF0> (i==N-1)
<a name="l77"></a> n1=tmpn-&gt;n;
<a name="l78"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (i==N-2)
<a name="l79"></a> n2=tmpn-&gt;n;
<a name="l80"></a>
<a name="l81"></a> TreeAdd(&amp;root,tmpn);
<a name="l82"></a> </span><span class=cF5>Sleep</span><span class=cF0>(50);
<a name="l83"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l84"></a>}
<a name="l85"></a>
<a name="l86"></a></span><span class=cF1>U0</span><span class=cF0> TreeCommonAncestorFind(Node *root)
<a name="l87"></a>{
<a name="l88"></a> </span><span class=cF1>if</span><span class=cF0> (root &amp;&amp; root-&gt;n!=n1 &amp;&amp; root-&gt;n!=n2) </span><span class=cF7>{</span><span class=cF0>
<a name="l89"></a> common_ancestor=root-&gt;n;
<a name="l90"></a> </span><span class=cF1>if</span><span class=cF0> (n1&lt;root-&gt;n &amp;&amp; n2&lt;root-&gt;n)
<a name="l91"></a> TreeCommonAncestorFind(root-&gt;left);
<a name="l92"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (n1&gt;root-&gt;n &amp;&amp; n2&gt;root-&gt;n)
<a name="l93"></a> TreeCommonAncestorFind(root-&gt;right);
<a name="l94"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l95"></a>}
<a name="l96"></a>
<a name="l97"></a></span><span class=cF1>U0</span><span class=cF0> TreeCommonAncestor()
<a name="l98"></a>{</span><span class=cF2>//Make tree and find common ancestor to n1 &amp; n2.</span><span class=cF0>
<a name="l99"></a> root=</span><span class=cF3>NULL</span><span class=cF0>;
<a name="l100"></a> n1=n2=common_ancestor=0;
<a name="l101"></a>
<a name="l102"></a> </span><span class=cF5>SettingsPush</span><span class=cF0>; </span><span class=cF2>//See </span><a href="/Wb/Adam/TaskSettings.HC.HTML#l3"><span class=cF4>SettingsPush</span></a><span class=cF0>
<a name="l103"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;draw_it=&amp;</span><span class=cF5>DrawIt</span><span class=cF0>;
<a name="l104"></a> </span><span class=cF5>DocClear</span><span class=cF0>;
<a name="l105"></a> </span><span class=cF6>&quot;Scroll with {CTRL-Left Grab}.\n&quot;</span><span class=cF0>;
<a name="l106"></a> </span><span class=cF1>try</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l107"></a> TreeNew;
<a name="l108"></a> TreeCommonAncestorFind(root);
<a name="l109"></a> </span><span class=cF5>PressAKey</span><span class=cF0>;
<a name="l110"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>catch</span><span class=cF0>
<a name="l111"></a> </span><span class=cF5>PutExcept</span><span class=cF0>;
<a name="l112"></a> </span><span class=cF5>SettingsPop</span><span class=cF0>;
<a name="l113"></a>}
<a name="l114"></a>
<a name="l115"></a>TreeCommonAncestor;
<a name="l116"></a></span><span class=cF2>/*Be careful with recursive routines in TempleOS</span><span class=cF0>
<a name="l117"></a></span><span class=cF2>because the stack does not grow and will overflow.</span><span class=cF0>
<a name="l118"></a>
<a name="l119"></a></span><span class=cF2>See </span><a href="/Wb/Demo/StkGrow.HC.HTML#l1"><span class=cF4>::/Demo/StkGrow.HC</span></a><span class=cF2>.</span><span class=cF0>
<a name="l120"></a></span><span class=cF2>*/</span><span class=cF0>
</span></pre></body>
</html>