@@ -328,6 +328,94 @@ def main():
328
328
main()
329
329
```
330
330
331
+ #### Python 2
332
+ ``` python
333
+ # Depth-First and Breadth-First Traversal
334
+ # Submitted by Matthew Giallourakis
335
+ from collections import deque
336
+
337
+ class node ():
338
+ " Create the node structure"
339
+ def __init__ (self , value , left , right ):
340
+ self .value = value
341
+ self .left = left
342
+ self .right = right
343
+
344
+ def make_tree (root ,nums ):
345
+ " Makes a binary search tree from a list of numbers"
346
+ for num in nums:
347
+ temp_node = root
348
+ new_node = node(value = num, left = None , right = None )
349
+ while True :
350
+ if num < temp_node.value:
351
+ if temp_node.left is None :
352
+ temp_node.left = new_node
353
+ break
354
+ else :
355
+ temp_node = temp_node.left
356
+ elif num > temp_node.value:
357
+ if temp_node.right is None :
358
+ temp_node.right = new_node
359
+ break
360
+ else :
361
+ temp_node = temp_node.right
362
+
363
+ def depth_first_search (root ):
364
+ " Traverses through a tree depth-first by putting nodes on a stack"
365
+ stack = deque()
366
+ result_list = []
367
+ stack.append(root)
368
+ while len (stack) != 0 :
369
+ # Take off the node at the top of the stack, add it to the list
370
+ temp_node = stack.pop()
371
+ result_list.append(temp_node.value)
372
+ # Add the children of that node to the top of the stack
373
+ if temp_node.right is not None :
374
+ stack.append(temp_node.right)
375
+ if temp_node.left is not None :
376
+ stack.append(temp_node.left)
377
+ return result_list
378
+
379
+
380
+ def breadth_first_search (root ):
381
+ " Traverses through a tree breadth-first by putting nodes into a queue"
382
+ queue = deque()
383
+ result_list = []
384
+ queue.append(root)
385
+ while len (queue) != 0 :
386
+ # Take off the node at the top of the queue, add it to the list
387
+ temp_node = queue.pop()
388
+ result_list.append(temp_node.value)
389
+ # Add the children of that node to the bottom of the queue
390
+ if temp_node.left is not None :
391
+ queue.appendleft(temp_node.left)
392
+ if temp_node.right is not None :
393
+ queue.appendleft(temp_node.right)
394
+ return result_list
395
+
396
+
397
+ def main ():
398
+ nums = [5 ,8 ,6 ,9 ,2 ,1 ,3 ]
399
+ root = node(value = nums.pop(0 ),left = None ,right = None )
400
+ make_tree(root,nums)
401
+ # Tree Structure
402
+ # 5
403
+ # 2 8
404
+ # 1 3 6 9
405
+ #
406
+
407
+ print (" Depth First:" ,depth_first_search(root))
408
+ # prints [5, 2, 1, 3, 8, 6, 9]
409
+
410
+ print (" Breadth First:" ,breadth_first_search(root))
411
+ # prints [5, 2, 8, 1, 3, 6, 9]
412
+
413
+
414
+ if __name__ == ' __main__' :
415
+ main()
416
+
417
+ ```
418
+
331
419
### C
332
420
333
421
``` c
@@ -672,3 +760,43 @@ BFS_queue(root);
672
760
</body >
673
761
<html >
674
762
```
763
+
764
+ #### Haskell
765
+ ``` hs
766
+ --- Submitted by Jie
767
+ data Tree a = Empty
768
+ | Node { node :: a ,
769
+ forest :: [Tree a ]}
770
+ deriving (Show )
771
+
772
+ dfs :: Tree a -> [a ]
773
+ dfs Empty = []
774
+ dfs (Node x ts) = x : concatMap dfs ts
775
+
776
+ bfs :: Tree a -> [a ]
777
+ bfs Empty = []
778
+ bfs (Node x ts) = x : go ts
779
+ where go [] = []
780
+ go ts = map node ts ++ go (concatMap forest ts)
781
+
782
+ main = do
783
+ print $ dfs testTree
784
+ print $ bfs testTree
785
+
786
+ testTree :: Tree Int
787
+ testTree = Node 1 [Node 2 [Node 3 [] ,
788
+ Node 4 [ Node 5 [] ]],
789
+ Node 6 [Node 7 [] ,
790
+ Node 8 [ Node 9 [Node 10 [Node 11 [] ],
791
+ Node 12 [] ]],
792
+ Node 13 [Node 14 [] ]],
793
+ Node 15 [] ]
794
+
795
+ ```
796
+
797
+ #### Scratch
798
+ Submitted by Jie
799
+
800
+ [ https://scratch.mit.edu/projects/174017753/ ] ( https://scratch.mit.edu/projects/174017753/ )
801
+
802
+ ![ Scratch Tree Traversal] ( scratch_tree.png )
0 commit comments