Skip to content

Commit 7d97574

Browse files
committed
adding new implementations of Tree Traversal and file for euclidean algorithm
1 parent ce1c4e6 commit 7d97574

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* [Taylor Series](chapters/mathematical_background/taylor_series.md)
1414
* [Fundamental Algorithms](chapters/fundamental_algorithms/fundamental_algorithms.md)
1515
* [Tree Traversal](chapters/fundamental_algorithms/tree_traversal.md)
16+
* [Euclidean Algorithm](chapters/fundamental_algorithms/euclidean_algorithm/euclidean.md)
1617
* [Computational Mathematics](chapters/computational_mathematics/computational_mathematics.md)
1718
* [Matrix Methods](chapters/computational_mathematics/matrix_methods/matrix_methods.md)
1819
* [Gaussian Elimination](chapters/computational_mathematics/matrix_methods/gaussian_elimination.md)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<script>
2+
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
3+
</script>
4+
$$
5+
\newcommand{\d}{\mathrm{d}}
6+
\newcommand{\bff}{\boldsymbol{f}}
7+
\newcommand{\bfg}{\boldsymbol{g}}
8+
\newcommand{\bfp}{\boldsymbol{p}}
9+
\newcommand{\bfq}{\boldsymbol{q}}
10+
\newcommand{\bfx}{\boldsymbol{x}}
11+
\newcommand{\bfu}{\boldsymbol{u}}
12+
\newcommand{\bfv}{\boldsymbol{v}}
13+
\newcommand{\bfA}{\boldsymbol{A}}
14+
\newcommand{\bfB}{\boldsymbol{B}}
15+
\newcommand{\bfC}{\boldsymbol{C}}
16+
\newcommand{\bfM}{\boldsymbol{M}}
17+
\newcommand{\bfJ}{\boldsymbol{J}}
18+
\newcommand{\bfR}{\boldsymbol{R}}
19+
\newcommand{\bfT}{\boldsymbol{T}}
20+
\newcommand{\bfomega}{\boldsymbol{\omega}}
21+
\newcommand{\bftau}{\boldsymbol{\tau}}
22+
$$
23+
24+
# Euclidean Algorithm
25+
Loading

chapters/fundamental_algorithms/tree_traversal.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,94 @@ def main():
328328
main()
329329
```
330330

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+
331419
### C
332420

333421
```c
@@ -672,3 +760,43 @@ BFS_queue(root);
672760
</body>
673761
<html>
674762
```
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

Comments
 (0)