forked from mission-peace/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree.py
More file actions
56 lines (43 loc) · 1.03 KB
/
binary_tree.py
File metadata and controls
56 lines (43 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from collections import namedtuple
Color = namedtuple("Color", "RED BLACK")
class Node:
def __init__(self):
self.color = None
self.height = None
self.lis = None
self.data = None
self.size = None
self.next = None
self.right = None
self.left = None
@staticmethod
def newNode(data):
n = Node()
n.data = data
n.lis = -1
n.height = 1
n.size = 1
n.color = Color.RED
return n
class BinaryTree:
def __init__(self):
pass
@staticmethod
def add_head(data, head):
temp_head = head
n = Node.newNode(data)
if head is None:
head = n
return head
prev = None
while head is not None:
prev = head
if head.data < data:
head = head.right
else:
head = head.left
if prev.data < data:
prev.right = n
else:
prev.left = n
return temp_head