2021-07-03 05:07:57 +01:00
<!DOCTYPE HTML>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html;charset=US-ASCII" >
2021-12-11 11:10:58 +00:00
< meta name = "generator" content = "ZealOS V1.07" >
2021-07-03 05:07:57 +01:00
< style type = "text/css" >
2021-10-08 07:06:11 +01:00
body {background-color:#1f1f1f;}
.cF0{color:#e3e3e3;background-color:#1f1f1f;}
.cF1{color:#4f84a6;background-color:#1f1f1f;}
.cF2{color:#73a255;background-color:#1f1f1f;}
.cF3{color:#297582;background-color:#1f1f1f;}
.cF4{color:#b34f4b;background-color:#1f1f1f;}
.cF5{color:#8a52c3;background-color:#1f1f1f;}
.cF6{color:#b7822f;background-color:#1f1f1f;}
.cF7{color:#444444;background-color:#1f1f1f;}
.cF8{color:#6d6d6d;background-color:#1f1f1f;}
.cF9{color:#94bfde;background-color:#1f1f1f;}
.cFA{color:#a1ce97;background-color:#1f1f1f;}
.cFB{color:#6db4be;background-color:#1f1f1f;}
.cFC{color:#e88e88;background-color:#1f1f1f;}
.cFD{color:#ca94e8;background-color:#1f1f1f;}
.cFE{color:#d4b475;background-color:#1f1f1f;}
.cFF{color:#1f1f1f;background-color:#1f1f1f;}
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-03 05:07:57 +01:00
< a name = "l1" > < / a > < span class = cF2 > /***************************************************< / span > < span class = cF0 >
< a name = "l2" > < / a >
< a name = "l3" > < / a > < / span > < span class = cF2 > Binary Search Tree (BST) Implementation< / span > < span class = cF0 >
< a name = "l4" > < / a >
< a name = "l5" > < / a > < / span > < span class = cF2 > ***************************************************/< / span > < span class = cF0 >
< a name = "l6" > < / a >
< a name = "l7" > < / a > < / span > < span class = cF1 > class< / span > < span class = cF0 > CBST
< a name = "l8" > < / a > {
2021-07-04 23:11:34 +01:00
< a name = "l9" > < / a > CBST *left;
< a name = "l10" > < / a > CBST *right;
< a name = "l11" > < / a > < / span > < span class = cF9 > I64< / span > < span class = cF0 > value;
2021-07-03 05:07:57 +01:00
< a name = "l12" > < / a > };
< a name = "l13" > < / a >
< a name = "l14" > < / a > CBST *BSTInit()
< a name = "l15" > < / a > {
2021-07-04 23:11:34 +01:00
< a name = "l16" > < / a > < / span > < span class = cF1 > return< / span > < span class = cF0 > < / span > < span class = cF5 > CAlloc< / span > < span class = cF0 > (< / span > < span class = cF1 > sizeof< / span > < span class = cF7 > (< / span > < span class = cF0 > CBST< / span > < span class = cF7 > )< / span > < span class = cF0 > );
2021-07-03 05:07:57 +01:00
< a name = "l17" > < / a > }
< a name = "l18" > < / a >
< a name = "l19" > < / a > < / span > < span class = cF1 > U0< / span > < span class = cF0 > BSTAdd(CBST *node, CBST *tree)
< a name = "l20" > < / a > { < / span > < span class = cF2 > // using temp and last allows avoiding recursion and non-growing stack issues.< / span > < span class = cF0 >
2021-07-04 23:11:34 +01:00
< a name = "l21" > < / a > CBST *temp_tree = tree;
< a name = "l22" > < / a > CBST *last_tree = temp_tree;
2021-07-03 05:07:57 +01:00
< a name = "l23" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l24" > < / a > < / span > < span class = cF1 > while< / span > < span class = cF0 > (temp_tree)
< a name = "l25" > < / a > < / span > < span class = cF7 > {< / span > < span class = cF0 > < / span > < span class = cF2 > // loop ends when temp_tree hits a NULL node.< / span > < span class = cF0 >
< a name = "l26" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (node-> value < temp_tree-> value)
< a name = "l27" > < / a > { < / span > < span class = cF2 > // if node smaller, go left< / span > < span class = cF0 >
< a name = "l28" > < / a > last_tree = temp_tree;
< a name = "l29" > < / a > temp_tree = temp_tree-> left;
< a name = "l30" > < / a > }
< a name = "l31" > < / a > < / span > < span class = cF1 > else< / span > < span class = cF0 >
< a name = "l32" > < / a > { < / span > < span class = cF2 > // if node equal or larger, go right< / span > < span class = cF0 >
< a name = "l33" > < / a > last_tree = temp_tree;
< a name = "l34" > < / a > temp_tree = temp_tree-> right;
< a name = "l35" > < / a > }
< a name = "l36" > < / a > < / span > < span class = cF7 > }< / span > < span class = cF0 >
< a name = "l37" > < / a > < / span > < span class = cF2 > // once while loop ends, this results in last_tree< / span > < span class = cF0 >
< a name = "l38" > < / a > < / span > < span class = cF2 > // being the resulting tree to store the node inside of.< / 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 > < / span > < span class = cF2 > // recompute the direction and set.< / span > < span class = cF0 >
< a name = "l41" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (node-> value < last_tree-> value)< / span > < span class = cF2 > // if node smaller, go left< / span > < span class = cF0 >
< a name = "l42" > < / a > last_tree-> left = node;
< a name = "l43" > < / a > < / span > < span class = cF1 > else< / span > < span class = cF0 > < / span > < span class = cF2 > // if node equal or larger, go right< / span > < span class = cF0 >
< a name = "l44" > < / a > last_tree-> right = node;
2021-07-03 05:07:57 +01:00
< a name = "l45" > < / a > }
< a name = "l46" > < / a >
< a name = "l47" > < / a > CBST *BSTParamAdd(< / span > < span class = cF9 > I64< / span > < span class = cF0 > value, CBST *tree)
< a name = "l48" > < / a > { < / span > < span class = cF2 > // add a node using params, return pointer to the node< / span > < span class = cF0 >
2021-07-04 23:11:34 +01:00
< a name = "l49" > < / a > CBST *result = BSTInit;
2021-07-03 05:07:57 +01:00
< a name = "l50" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l51" > < / a > result-> value = value;
2021-07-03 05:07:57 +01:00
< a name = "l52" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l53" > < / a > BSTAdd(result, tree);
2021-07-03 05:07:57 +01:00
< a name = "l54" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l55" > < / a > < / span > < span class = cF1 > return< / span > < span class = cF0 > result;
2021-07-03 05:07:57 +01:00
< a name = "l56" > < / a > }
< a name = "l57" > < / a >
< a name = "l58" > < / a > CBST *BSTParamInit(< / span > < span class = cF9 > I64< / span > < span class = cF0 > value)
< a name = "l59" > < / a > {
2021-07-04 23:11:34 +01:00
< a name = "l60" > < / a > CBST *result = BSTInit;
2021-07-03 05:07:57 +01:00
< a name = "l61" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l62" > < / a > result-> value = value;
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 > return< / span > < span class = cF0 > result;
2021-07-03 05:07:57 +01:00
< a name = "l65" > < / a > }
< a name = "l66" > < / a >
< a name = "l67" > < / a > CBST *BSTFind(< / span > < span class = cF9 > I64< / span > < span class = cF0 > value, CBST *tree)
< a name = "l68" > < / a > {
2021-07-04 23:11:34 +01:00
< a name = "l69" > < / a > CBST *temp_tree = tree;
2021-07-03 05:07:57 +01:00
< a name = "l70" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l71" > < / a > < / span > < span class = cF1 > while< / span > < span class = cF0 > (temp_tree)
< a name = "l72" > < / a > < / span > < span class = cF7 > {< / span > < span class = cF0 >
< a name = "l73" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (value < temp_tree-> value) < / span > < span class = cF2 > // if value smaller, go left< / span > < span class = cF0 >
< a name = "l74" > < / a > temp_tree = temp_tree-> left;
< a name = "l75" > < / a > < / span > < span class = cF1 > else< / span > < span class = cF0 > < / span > < span class = cF1 > if< / span > < span class = cF0 > (value > temp_tree-> value) < / span > < span class = cF2 > // if value larger, go right< / span > < span class = cF0 >
< a name = "l76" > < / a > temp_tree = temp_tree-> right;
< a name = "l77" > < / a > < / span > < span class = cF1 > else< / span > < span class = cF0 > < / span > < span class = cF2 > // if value equal, match found.< / span > < span class = cF0 >
< a name = "l78" > < / a > < / span > < span class = cF1 > break< / span > < span class = cF0 > ;
< a name = "l79" > < / a > < / span > < span class = cF7 > }< / span > < span class = cF0 >
< a name = "l80" > < / a > < / span > < span class = cF1 > return< / span > < span class = cF0 > temp_tree; < / span > < span class = cF2 > // ! NULL if not found.< / span > < span class = cF0 >
2021-07-03 05:07:57 +01:00
< a name = "l81" > < / a > }
< a name = "l82" > < / a >
< a name = "l83" > < / a > CBST *BSTPop(< / span > < span class = cF9 > I64< / span > < span class = cF0 > value, CBST *tree)
< a name = "l84" > < / a > { < / span > < span class = cF2 > // mimics TreeNodeFind. pops whole sub-tree, original tree loses whole branch.< / span > < span class = cF0 >
2021-07-04 23:11:34 +01:00
< a name = "l85" > < / a > CBST *parent_tree = tree;
< a name = "l86" > < / a > CBST *temp_tree = parent_tree;
< a name = "l87" > < / a > < / span > < span class = cF1 > Bool< / span > < span class = cF0 > is_left = < / span > < span class = cF3 > FALSE< / span > < span class = cF0 > ;
< a name = "l88" > < / a > < / span > < span class = cF1 > Bool< / span > < span class = cF0 > is_right = < / span > < span class = cF3 > FALSE< / span > < span class = cF0 > ;
2021-07-03 05:07:57 +01:00
< a name = "l89" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l90" > < / a > < / span > < span class = cF1 > while< / span > < span class = cF0 > (temp_tree)
< a name = "l91" > < / a > < / span > < span class = cF7 > {< / span > < span class = cF0 >
< a name = "l92" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (value < temp_tree-> value)
< a name = "l93" > < / a > {
< a name = "l94" > < / a > parent_tree = temp_tree;
< a name = "l95" > < / a > temp_tree = temp_tree-> left;
< a name = "l96" > < / a > is_right = < / span > < span class = cF3 > FALSE< / span > < span class = cF0 > ;
< a name = "l97" > < / a > is_left = < / span > < span class = cF3 > TRUE< / span > < span class = cF0 > ;
< a name = "l98" > < / a > }
< a name = "l99" > < / a > < / span > < span class = cF1 > else< / span > < span class = cF0 > < / span > < span class = cF1 > if< / span > < span class = cF0 > (value > temp_tree-> value)
< a name = "l100" > < / a > {
< a name = "l101" > < / a > parent_tree = temp_tree;
< a name = "l102" > < / a > temp_tree = temp_tree-> right;
< a name = "l103" > < / a > is_right = < / span > < span class = cF3 > TRUE< / span > < span class = cF0 > ;
< a name = "l104" > < / a > is_left = < / span > < span class = cF3 > FALSE< / span > < span class = cF0 > ;
< a name = "l105" > < / a > }
< a name = "l106" > < / a > < / span > < span class = cF1 > else< / span > < span class = cF0 > < / span > < span class = cF2 > // if value equal, match found.< / span > < span class = cF0 >
< a name = "l107" > < / a > < / span > < span class = cF1 > break< / span > < span class = cF0 > ;
< a name = "l108" > < / a > < / span > < span class = cF7 > }< / span > < span class = cF0 >
2021-07-03 05:07:57 +01:00
< a name = "l109" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l110" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (temp_tree)
< a name = "l111" > < / a > < / span > < span class = cF7 > {< / span > < span class = cF0 > < / span > < span class = cF2 > //if we found it, clear its parents link to the node< / span > < span class = cF0 >
< a name = "l112" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (is_left)
< a name = "l113" > < / a > {
< a name = "l114" > < / a > parent_tree-> left = < / span > < span class = cF3 > NULL< / span > < span class = cF0 > ;
< a name = "l115" > < / a > }
< a name = "l116" > < / a > < / span > < span class = cF1 > else< / span > < span class = cF0 > < / span > < span class = cF1 > if< / span > < span class = cF0 > (is_right)
< a name = "l117" > < / a > {
< a name = "l118" > < / a > parent_tree-> right = < / span > < span class = cF3 > NULL< / span > < span class = cF0 > ;
< a name = "l119" > < / a > }
< a name = "l120" > < / a > < / span > < span class = cF7 > }< / span > < span class = cF0 >
2021-07-03 05:07:57 +01:00
< a name = "l121" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l122" > < / a > < / span > < span class = cF1 > return< / span > < span class = cF0 > temp_tree; < / span > < span class = cF2 > // NULL if not found.< / span > < span class = cF0 >
2021-07-03 05:07:57 +01:00
< a name = "l123" > < / a > }
< a name = "l124" > < / a >
< a name = "l125" > < / a > CBST *BSTSinglePop(< / span > < span class = cF9 > I64< / span > < span class = cF0 > value, CBST *tree)
2021-07-04 23:11:34 +01:00
< a name = "l126" > < / a > { < / span > < span class = cF2 > // pop a tree off, then add back in its sub-trees to main tree.< / span > < span class = cF0 >
< a name = "l127" > < / a > < / span > < span class = cF2 > // original node sub-trees are cleared.< / span > < span class = cF0 >
2021-07-03 05:07:57 +01:00
< a name = "l128" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l129" > < / a > CBST *node = BSTPop(value, tree);
< a name = "l130" > < / a > CBST *left = node-> left;
< a name = "l131" > < / a > CBST *right = node-> right;
2021-07-03 05:07:57 +01:00
< a name = "l132" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l133" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (node)
< a name = "l134" > < / a > < / span > < span class = cF7 > {< / span > < span class = cF0 >
< a name = "l135" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (left)
< a name = "l136" > < / a > { < / span > < span class = cF2 > // if node has left tree, add the tree< / span > < span class = cF0 >
< a name = "l137" > < / a > BSTAdd(left, tree);
< a name = "l138" > < / a > node-> left = < / span > < span class = cF3 > NULL< / span > < span class = cF0 > ;
< a name = "l139" > < / a > }
< a name = "l140" > < / a > < / span > < span class = cF1 > if< / span > < span class = cF0 > (right)
< a name = "l141" > < / a > { < / span > < span class = cF2 > // if node has right tree, add the tree.< / span > < span class = cF0 >
< a name = "l142" > < / a > BSTAdd(right, tree);
< a name = "l143" > < / a > node-> right = < / span > < span class = cF3 > NULL< / span > < span class = cF0 > ;
< a name = "l144" > < / a > }
< a name = "l145" > < / a > < / span > < span class = cF7 > }< / span > < span class = cF0 >
2021-07-03 05:07:57 +01:00
< a name = "l146" > < / a >
2021-07-04 23:11:34 +01:00
< a name = "l147" > < / a > < / span > < span class = cF1 > return< / span > < span class = cF0 > node;
2021-07-03 05:07:57 +01:00
< a name = "l148" > < / a > }< / span > < / pre > < / body >
< / html >