Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. #lang racket
  2. (provide ins_beg)
  3.  
  4. (define (ins_beg el lst)
  5. (cons el lst)
  6. )
  7.  
  8. (provide ins_end)
  9.  
  10. (define (ins_end el lst)
  11. (append lst (list el))
  12. )
  13.  
  14. (provide count_top_level)
  15.  
  16. (define (count_top_level li)
  17. (length li)
  18. )
  19.  
  20. (provide count_instances)
  21.  
  22. (define (count_instances el lst)
  23. (if (null? lst)
  24. 0
  25. (if (equal? (car lst) el)
  26. (+ 1 (count_instances el (cdr lst)))
  27. (count_instances el (cdr lst))
  28. )
  29. )
  30. )
  31.  
  32. (provide count_instances_tr)
  33. (provide count_tr)
  34.  
  35. (define (count_instances_tr el lst)
  36. (count_tr el lst 0)
  37. )
  38.  
  39. (define (count_tr el lst total)
  40. (if (null? lst)
  41. total
  42. (if (equal? (car lst) el)
  43. (count_tr el (cdr lst) (+ 1 total))
  44. (count_tr el (cdr lst) total)
  45. )
  46. )
  47. )
  48.  
  49. (provide count_instances_deep)
  50. (provide count_deep)
  51.  
  52. (define (count_instances_deep el lst)
  53. (count_deep el lst 0)
  54. )
  55.  
  56. (define (count_deep el lst total)
  57. (if (null? lst)
  58. total
  59. (begin
  60. (when (list? (car lst))
  61. (set! total (+ total (count_instances_deep el (car lst))))
  62. )
  63. (if (equal? (car lst) el)
  64. (count_deep el (cdr lst) (+ 1 total))
  65. (count_deep el (cdr lst) total)
  66. )
  67. )
  68. )
  69. )
  70.  
  71. ; A list without the last element
  72. ; Essentially the opposite of cdr
  73. (define (rdc lst)
  74. (reverse (cdr (reverse lst)))
  75. )
  76.  
  77. ; Function reate new empty tree with a given value as its root
  78. (define (Tree value)
  79. (list '() value '())
  80. )
  81.  
  82. ; Begin with an empty tree
  83. (define root '())
  84.  
  85. ; Helper function for insert_tree
  86. ; Let the root equal the new tree with the inserted value
  87. (define (insert value)
  88. (set! root (insert_tree value root))
  89. )
  90.  
  91. ; Insert new value into the tree
  92. (define (insert_tree value tree)
  93. (if (null? tree)
  94. ; If the tree is empty make a new tree with the given value as its root
  95. (Tree value)
  96. ; Else check whether the given value is less than the root
  97. (if (< value (cadr tree))
  98. ; If the given value is less than the current root:
  99. (if (null? (car tree))
  100. ; If the left subtree is empty, create a new left subtree with its given value
  101. (cons (Tree value) (cdr tree))
  102. ; Else recursively call insert_tree on the left subtree and add the result of that the new left subtree
  103. (cons (insert_tree value (car tree)) (cdr tree))
  104. )
  105. ; Else the given value is greater than the current root:
  106. (if (null? (caddr tree))
  107. ; If the right subtree is empty, create a new right subtree with its given value
  108. (append (rdc tree) (list (Tree value)))
  109. ; Else recursively call insert_tree on the right subtree and add the result of that the new right subtree
  110. (append (rdc tree) (list (insert_tree value (caddr tree))))
  111. )
  112. )
  113. )
  114. )
  115.  
  116. ; Helper function for inorder_traversal
  117. (define (traverse)
  118. (begin
  119. (printf "Beginning inorder traversal...\n")
  120. (inorder_traversal root)
  121. )
  122. )
  123.  
  124. ; Traverses a given tree
  125. (define (inorder_traversal tree)
  126. (when (not (null? tree))
  127. ; When the tree isn't empty
  128. (begin
  129. ; Traverse left subtree
  130. (inorder_traversal (car tree))
  131. ; Print value
  132. (printf "~a " (cadr tree))
  133. ; Traverse right subtree
  134. (inorder_traversal (caddr tree))
  135. )
  136. )
  137. )
  138.  
  139. ; Global variable to check if item is found
  140. (define found #f)
  141. ; Helper function for search_tree
  142. (define (search item tree)
  143. ; Set found to false each time something is searched
  144. (set! found #f)
  145. (begin
  146. ; Search tree for item
  147. (search_tree item tree)
  148. ; Display value of global variable found
  149. (display found)
  150. )
  151. )
  152.  
  153. ; Recursively search given tree for a given item
  154. (define (search_tree item tree)
  155. ; If given tree is not empty
  156. (when (not (null? tree))
  157. (if (equal? item (cadr tree))
  158. ; If value of current tree is equal to the item being searched for, set global variable found to true
  159. (set! found #t)
  160. ; Else search left and right subtrees
  161. (begin
  162. (search_tree item (car tree))
  163. (search_tree item (caddr tree))
  164. )
  165. )
  166. )
  167. )
  168.  
  169. ; Inserts given list into binary search tree
  170. (define (insert_list lst)
  171. ; Checks that list is not empty
  172. (when (not (null? lst))
  173. ; Adds first item to binary tree and recurses using the remaining list
  174. (begin
  175. (insert (car lst))
  176. (insert_list (cdr lst))
  177. )
  178. )
  179. )
  180.  
  181. ;(insert_list '(8 4 2 6 12 14 10))
  182.  
  183. ; Insert list into tree and then traverse it inorder
  184. (define (sort lst)
  185. (set! root '())
  186. (begin
  187. (insert_list lst)
  188. (traverse)
  189. )
  190. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement