Skip to content

Commit 57536ed

Browse files
committed
feat: implement OBST
1 parent a8ea852 commit 57536ed

File tree

9 files changed

+101172
-0
lines changed

9 files changed

+101172
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

Node.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Node:
2+
def __init__(self, value, word):
3+
self.value = value
4+
self.word = word
5+
self.left = None
6+
self.right = None
7+
8+
def __repr__(self):
9+
"""Use word property when trying to print Node class"""
10+
11+
return self.word

buildTree.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from Node import Node
2+
3+
4+
def buildTree(root_table, n, words):
5+
"""Create nodes of the OBST based on the root table"""
6+
7+
root_ = root_table[1][n]
8+
9+
root_node = Node(root_, words[root_])
10+
s = []
11+
12+
s.append((root_node, 1, n))
13+
14+
while(len(s) != 0):
15+
(u, i, j) = s.pop()
16+
l = root_table[i][j]
17+
18+
if l < j:
19+
# build the right tree
20+
v = Node(root_table[l+1][j], words[root_table[l+1][j]])
21+
u.right = v
22+
s.append((v, l+1, j))
23+
if i < l:
24+
# build left subtree
25+
v = Node(root_table[i][l-1], words[root_table[i][l-1]])
26+
u.left = v
27+
s.append((v, i, l-1))
28+
29+
return root_node

0 commit comments

Comments
 (0)