Skip to content

Commit 2f85f53

Browse files
committed
parse tree
1 parent 27e9841 commit 2f85f53

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ but it also contains other data structures, algorithms and problems.
7575
### [Trees]()
7676
- [List of lists representation](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/blob/master/trees/list-representation.py)
7777
- [Class representation](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/blob/master/trees/class-representation.py)
78+
- [Parse tree]()
7879

7980
### [Sorting](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/sorting)
8081
- [Bubble sort](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/blob/master/sorting/bubble-sort.py)
1.1 KB
Binary file not shown.

trees/parse-tree.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
from stack import Stack
3+
import operator
4+
5+
class TreeNode:
6+
def __init__(self, key=None, left=None, right=None):
7+
self.key = key
8+
self.left = left
9+
self.right = right
10+
11+
def insert_root_value(self, key=None):
12+
self.key = key
13+
14+
def insert_left(self, key=None):
15+
self.left = TreeNode(key, self.left)
16+
17+
def insert_right(self, key=None):
18+
self.right = TreeNode(key, None, self.right)
19+
20+
def get_root_value(self):
21+
return self.key
22+
23+
def get_left_child(self):
24+
return self.left
25+
26+
def get_right_child(self):
27+
return self.right
28+
29+
30+
opers = {'+':operator.add, '-':operator.sub, '*':operator.mul, '/':operator.truediv}
31+
32+
import re
33+
pattern = re.compile("[0-9]")
34+
35+
string = "( ( 10 + 5 ) * 3 )"
36+
37+
38+
def tree_parser(s: str) -> TreeNode:
39+
arr: list = s.split()
40+
stack: Stack = Stack()
41+
node: TreeNode = TreeNode()
42+
current_node: TreeNode = node
43+
stack.push(node)
44+
for e in arr:
45+
if e == "(":
46+
current_node.insert_left()
47+
stack.push(current_node)
48+
current_node = current_node.get_left_child()
49+
elif e in "+-*/":
50+
current_node.insert_root_value(e)
51+
current_node.insert_right()
52+
stack.push(current_node)
53+
current_node = current_node.get_right_child()
54+
elif pattern.match(e):
55+
current_node.insert_root_value(int(e))
56+
current_node = stack.pop()
57+
elif e == ")":
58+
current_node = stack.pop()
59+
else:
60+
raise Exception()
61+
return node
62+
63+
64+
tree_node: TreeNode = tree_parser(string)
65+
66+
67+
def evaluate(node: TreeNode) -> int:
68+
if node.get_left_child() is not None and node.get_right_child() is not None:
69+
f = opers[node.get_root_value()]
70+
return f(evaluate(node.get_left_child()), evaluate(node.get_right_child()))
71+
else:
72+
return node.get_root_value()
73+
74+
75+
print(evaluate(tree_node)) # 45

trees/stack.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Stack:
2+
def __init__(self):
3+
self.items = []
4+
5+
def is_empty(self):
6+
return self.items == []
7+
8+
def push(self, item):
9+
self.items.append(item)
10+
11+
def pop(self):
12+
return self.items.pop()
13+
14+
def peek(self):
15+
return self.items[len(self.items) - 1]
16+
17+
def size(self):
18+
return len(self.items)

0 commit comments

Comments
 (0)