-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.py
103 lines (85 loc) · 2.68 KB
/
node.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from binary_tree_exception import NodeTypeError
class NodeTree:
def __init__(self, inf=None):
self.__info = inf
self.__left: NodeTree or None = None
self.__right: NodeTree or None = None
self.__parent: NodeTree or None = None
def getInfo(self):
return self.__info
def setInfo(self, info):
self.__info = info
def getParent(self):
return self.__parent
def __setParent(self, father):
if isinstance(father, NodeTree) or father is None:
self.__parent = father
else:
raise NodeTypeError(father)
def getRight(self):
return self.__right
def setRight(self, node):
if not isinstance(node, NodeTree) and node is not None:
raise NodeTypeError(node)
self.__right = node
if node is not None:
self.__right.__setParent(self)
def getLeft(self):
return self.__left
def setLeft(self, node):
if not isinstance(node, NodeTree) and node is not None:
raise NodeTypeError(node)
self.__left = node
if node is not None:
self.__left.__setParent(self)
def isSon(self, node) -> bool:
if not isinstance(node, NodeTree):
raise NodeTypeError(node)
if node is not None:
if self.__parent == node:
return True
else:
return False
else:
return False
def isBrother(self, node) -> bool:
if not isinstance(node, NodeTree):
raise NodeTypeError(node)
if node is not None:
if node.getParent() == self.__parent and (node.getParent() is not None and self.__parent is not None):
return True
else:
return False
else:
return False
def isLeft(self) -> bool:
if self.__parent is None:
return False
else:
if self.__parent.getLeft() == self:
return True
else:
return False
def isRight(self) -> bool:
if self.__parent is None:
return False
else:
if self.__parent.getRight() == self:
return True
else:
return False
def isLeaf(self) -> bool:
if self.__left is None and self.__right is None:
return True
else:
return False
def hasNodeLeft(self) -> bool:
if isinstance(self.__left, NodeTree):
return True
else:
return False
def hasNodeRight(self):
if isinstance(self.__right, NodeTree):
return True
else:
return False