Functions for Tree data structures
-
NULL = createSprite() tree = NULL function create_tree(root, data)//{ root = createSprite() root.data = data root.left = NULL root.right = NULL return root //} function add_item(head, data)//{ while head.left != NULL or head.right != NULL loop if data > head.data then if head.right == NULL then break endif head = head.right else if data < head.data then if head.left == NULL then break endif head = head.left else break endif endif repeat if data > head.data then head.right = createSprite() head = head.right head.data = data head.left = NULL head.right = NULL else if data < head.data then head.left = createSprite() head = head.left head.data = data head.left = NULL head.right = NULL else //Do nothing endif endif return void //} function pre_order_traversal(head)//{ if head != NULL then print(head.data) pre_order_traversal(head.left) pre_order_traversal(head.right) endif return void //} function inorder_traversal(head)//{ if head != NULL then inorder_traversal(head.left) print(head.data) inorder_traversal(head.right) endif return void //} function post_order_traversal(head)//{ if head != NULL then post_order_traversal(head.left) post_order_traversal(head.right) print(head.data) endif return void //} tree = create_tree(tree, random(100)) for x = 0 to 10 loop add_item(tree,random(100)) repeat inorder_traversal(tree) //Prints the contents of the tree from the //lowest value to the highest. update() sleep(4)
Hope you guys find this useful.