2021-07-03 05:07:57 +01:00
< ! DOCTYPE HTML >
< html >
< head >
< meta http - equiv = " Content-Type " content = " text/html;charset=US-ASCII " >
< meta name = " generator " content = " ZealOS V0.05 " >
< style type = " text/css " >
body { background - color : # 000000 ; }
. cF0 { color : # ffffff ; background - color : # 000000 ; }
. cF1 { color : # 3465 a4 ; background - color : # 000000 ; }
. cF2 { color : # 4e9 a06 ; background - color : # 000000 ; }
. cF3 { color : # 06 989 a ; background - color : # 000000 ; }
. cF4 { color : # a24444 ; background - color : # 000000 ; }
. cF5 { color : # 75507 b ; background - color : # 000000 ; }
. cF6 { color : # ce982f ; background - color : # 000000 ; }
. cF7 { color : # bcc0b9 ; background - color : # 000000 ; }
. cF8 { color : # 555753 ; background - color : # 000000 ; }
. cF9 { color : # 729f cf ; background - color : # 000000 ; }
. cFA { color : # 82 bc49 ; background - color : # 000000 ; }
. cFB { color : # 34e2 e2 ; background - color : # 000000 ; }
. cFC { color : # ac3535 ; background - color : # 000000 ; }
. cFD { color : # ad7fa8 ; background - color : # 000000 ; }
. cFE { color : # fce94f ; background - color : # 000000 ; }
. cFF { color : # 000000 ; background - color : # 000000 ; }
< / 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 - & gt ; value & lt ; temp_tree - & gt ; 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 - & gt ; 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 - & gt ; 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 - & gt ; value & lt ; last_tree - & gt ; value ) < / span > < span class = cF2 > // if node smaller, go left</span><span class=cF0>
< a name = " l42 " > < / a > last_tree - & gt ; 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 - & gt ; 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 - & gt ; 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 - & gt ; 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 & lt ; temp_tree - & gt ; value ) < / span > < span class = cF2 > // if value smaller, go left</span><span class=cF0>
< a name = " l74 " > < / a > temp_tree = temp_tree - & gt ; left ;
< a name = " l75 " > < / a > < / span > < span class = cF1 > else < / span > < span class = cF0 > < / span > < span class = cF1 > if < / span > < span class = cF0 > ( value & gt ; temp_tree - & gt ; value ) < / span > < span class = cF2 > // if value larger, go right</span><span class=cF0>
< a name = " l76 " > < / a > temp_tree = temp_tree - & gt ; 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 & lt ; temp_tree - & gt ; value )
< a name = " l93 " > < / a > {
< a name = " l94 " > < / a > parent_tree = temp_tree ;
< a name = " l95 " > < / a > temp_tree = temp_tree - & gt ; 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 & gt ; temp_tree - & gt ; value )
< a name = " l100 " > < / a > {
< a name = " l101 " > < / a > parent_tree = temp_tree ;
< a name = " l102 " > < / a > temp_tree = temp_tree - & gt ; 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 - & gt ; 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 - & gt ; 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 - & gt ; left ;
< a name = " l131 " > < / a > CBST * right = node - & gt ; 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 - & gt ; 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 - & gt ; 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 >