Re: Call to c_func() short-circuited
- Posted by irv Dec 19, 2018
- 1341 views
The code below I translated from: https://www.tutorialspoint.com/data_structures_algorithms/tree_traversal_in_c.htm
Note that the node[DATA] item can be any Euphoria object (sequence or nested sequences), so that any kind of data can be stored there. You just need to select which portion of the data is to be considered in the insert and compare functions. e.g. compare(word[1],node[DATA][1]) if the data item is a sequence such as {"key",{moredata...}}
include std/console.e include std/text.e include std/io.e include std/filesys.e object root = 0 enum DATA, LEFTCHILD, RIGHTCHILD enum PRE, ORD, POST ------------------------------------------------ function insert(object node, sequence word) ------------------------------------------------ integer x if length(node) <= 1 then -- create new node node = {word,{},{}} else x = compare(word,node[DATA]) if x = 0 then -- reject dupes return node end if if x < 0 then node[LEFTCHILD] = insert(node[LEFTCHILD], word) else node[RIGHTCHILD] = insert(node[RIGHTCHILD], word) end if end if return node end function ---------------------------------- function search(object searchdata) ---------------------------------- object current = root display("visiting elements:") while not equal(current[DATA],searchdata) do display("\t[1]",current[DATA]) if compare(current[DATA],searchdata) > 0 then current = current[LEFTCHILD] else current = current[RIGHTCHILD] end if if length(current)=0 then return sprintf("%s not found",{searchdata}) end if end while return sprintf("Found %s",{searchdata}) end function ---------------------------------- function find(object searchdata) ---------------------------------- object current = root integer steps = 0 while not equal(current[DATA],searchdata) do if compare(current[DATA],searchdata) > 0 then current = current[LEFTCHILD] else current = current[RIGHTCHILD] end if steps += 1 if length(current)=0 then return 0 end if display("\t[1]",{current[DATA]}) end while return format("Found [] in [] steps",{searchdata,steps}) end function global object list = {} --------------------------------------------------------------- procedure transverse(object node, integer ord=ORD) -- recursive --------------------------------------------------------------- if length(node) = 0 then return -- nothing to print end if if ord = PRE then list &= text:format("[1]\n",node) end if transverse(node[LEFTCHILD],ord) if ord = ORD then list &= text:format("[1]\n",node) end if transverse(node[RIGHTCHILD],ord) if ord = POST then list &= text:format("[1]\n",node) end if end procedure ----------------------------------------------------- function transverse_list(object node, integer ord=2) ----------------------------------------------------- list = {} transverse(node,ord) return list end function ------------------------------------------------------------------------------ object words = {"Zap","Barn","App","Job","Foo","Bar","App","Jam", "Jim","Bar","Bla","Bar","Zed","Baz","Fred","Joe"} for i = 1 to length(words) do root = insert(root,words[i]) end for display(transverse_list(root,ORD)) display(find("Joe"))