Skip to content

Commit def7a32

Browse files
committed
BASIC DATA STRUCTURE AND ALGORITHM
1 parent 30793fa commit def7a32

File tree

105 files changed

+185
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+185
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from transversal_BST_recursively import BSTwithTransversalRecursively
2+
3+
def find_ancestor(path, low_value, high_value):
4+
while path:
5+
current_value = path[0]
6+
if current_value < low_value:
7+
try:
8+
path = path[2:]
9+
except:
10+
return current_value
11+
elif current_value > high_value:
12+
try:
13+
path = path[1:]
14+
except:
15+
return current_value
16+
elif low_value <= current_value <= high_value:
17+
return current_value
18+
19+
20+
if __name__ == "__main__":
21+
bst = BSTwithTransversalRecursively()
22+
l = [10,5,6,3,8,2,1,11,9,4]
23+
for i in l:
24+
bst.add_node(i)
25+
path = bst.preorder()
26+
print("전위 순회: ", path)
27+
28+
print("1과 6의 최소 공통 조상 :", find_ancestor(path, 1, 6))
29+
print("1과 11의 최소 공통 조상 :", find_ancestor(path, 1, 11))
30+
print("1과 4의 최소 공통 조상 :", find_ancestor(path, 1, 4))
31+
print("8과 9의 최소 공통 조상 : ", find_ancestor(path, 8, 9))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from collections import deque
2+
from binary_search_tree import BinarySearchTree, NodeBST
3+
4+
class BSTwithTransversalIterative(BinarySearchTree):
5+
6+
def inorder(self):
7+
current = self.root
8+
nodes, stack = [], []
9+
while stack or current:
10+
if current:
11+
stack.append(current)
12+
current = current.left
13+
else:
14+
current = stack.pop()
15+
nodes.append(current.value)
16+
current = current.right
17+
return nodes
18+
19+
def preorder(self):
20+
current = self.root
21+
nodes, stack = [], []
22+
while stack or current:
23+
if current:
24+
nodes.append(current.value)
25+
stack.append(current)
26+
current = current.left
27+
else:
28+
current = stack.pop()
29+
current = current.right
30+
return nodes
31+
32+
def preorder2(self):
33+
nodes = []
34+
stack = [self.root]
35+
while stack:
36+
current = stack.pop()
37+
if current:
38+
nodes.append(current.value)
39+
stack.append(current.right)
40+
stack.append(current.left)
41+
return nodes
42+
43+
def BFT(self):
44+
current = self.root
45+
nodes = []
46+
queue = deque()
47+
queue.append(current)
48+
while queue:
49+
current = queue.popleft()
50+
nodes.append(current.value)
51+
if current.left:
52+
queue.append(current.left)
53+
if current.right:
54+
queue.append(current.right)
55+
return nodes
56+
57+
58+
if __name__ == "__main__":
59+
bst = BSTwithTransversalIterative()
60+
l = [10,5,6,3,8,2,1,11,9,4]
61+
for i in l:
62+
bst.add_node(i)
63+
64+
print("노드 8은 말단 노드입니까? ", bst.is_leaf(8))
65+
print("노드 8의 레벨은? ", bst.get_node_level(8))
66+
print("노드 10은 루트 노드입니까? ", bst.is_root(10))
67+
print("노드 1은 루트 노드입니까? ", bst.is_root(1))
68+
print("트리의 높이는? ", bst.get_height())
69+
print("이진 탐색 트리입니까? ", bst.is_bst())
70+
print("균형 트리입니까? ", bst.is_balanced())
71+
72+
print("전위 순회: ", bst.preorder())
73+
print("전위 순회2: ", bst.preorder2())
74+
print("중위 순회: ", bst.inorder())
75+
print("너비 우선 탐색: ", bst.BFT())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from binary_search_tree import BinarySearchTree, NodeBST
2+
3+
4+
class BSTwithTransversalRecursively(BinarySearchTree):
5+
6+
def __init__(self):
7+
self.root = None
8+
self.nodes_BFS = []
9+
self.nodes_pre = []
10+
self.nodes_post = []
11+
self.nodes_in = []
12+
13+
def BFT(self):
14+
self.root.level = 1
15+
queue = [self.root]
16+
current_level = self.root.level
17+
18+
while len(queue) > 0:
19+
current_node = queue.pop(0)
20+
if current_node.level > current_level:
21+
current_level += 1
22+
self.nodes_BFS.append(current_node.value)
23+
24+
if current_node.left:
25+
current_node.left.level = current_level + 1
26+
queue.append(current_node.left)
27+
28+
if current_node.right:
29+
current_node.right.level = current_level + 1
30+
queue.append(current_node.right)
31+
32+
return self.nodes_BFS
33+
34+
def inorder(self, node=None, level=1):
35+
if not node and level == 1:
36+
node = self.root
37+
if node:
38+
self.inorder(node.left, level+1)
39+
self.nodes_in.append(node.value)
40+
self.inorder(node.right, level+1)
41+
return self.nodes_in
42+
43+
def preorder(self, node=None, level=1):
44+
if not node and level == 1:
45+
node = self.root
46+
if node:
47+
self.nodes_pre.append(node.value)
48+
self.preorder(node.left, level+1)
49+
self.preorder(node.right, level+1)
50+
return self.nodes_pre
51+
52+
def postorder(self, node=None, level=1):
53+
if not node and level == 1:
54+
node = self.root
55+
if node:
56+
self.postorder(node.left, level+1)
57+
self.postorder(node.right, level+1)
58+
self.nodes_post.append(node.value)
59+
return self.nodes_post
60+
61+
62+
if __name__ == "__main__":
63+
bst = BSTwithTransversalRecursively()
64+
l = [10,5,6,3,8,2,1,11,9,4]
65+
for i in l:
66+
bst.add_node(i)
67+
68+
print("노드 8은 말단 노드입니까? ", bst.is_leaf(8))
69+
print("노드 8의 레벨은? ", bst.get_node_level(8))
70+
print("노드 10은 루트 노드입니까? ", bst.is_root(10))
71+
print("노드 1은 루트 노드입니까? ", bst.is_root(1))
72+
print("트리의 높이는? ", bst.get_height())
73+
print("이진 탐색 트리입니까? ", bst.is_bst())
74+
print("균형 트리입니까? ", bst.is_balanced())
75+
76+
print("전위 순회: ", bst.preorder())
77+
print("후위 순회: ", bst.postorder())
78+
print("중위 순회: ", bst.inorder())
79+
print("너비 우선 탐색: ", bst.BFT())
File renamed without changes.
2.74 KB
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)