二叉树节点类

二叉树节点类

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
class Node():
def __init__(self, value=None, root_node=None, left_node=None, right_node=None):
self.value = value
self.root = root_node
self.left = left_node
self.right = right_node

def set(self, value=None, root_node=None, left_node=None, right_node=None):
if value is not None:
self.value = value
if root is not None:
self.root = root_node
if left is not None:
self.left = left_node
if right is not None:
self.right = right_node

def get_root(self):
return self.root

def get_left_node(self):
return self.left

def get_right_node(self):
return self.right

def is_leaf_node(self):
if self.left is None and self.left is None:
return True
return False

def is_root_node(self):
if self.root is None:
return True
return False

root = Node(5)
left = Node(3, root)
right = Node(6, root)
root.set(left_node=left, right_node=right)
print(root.value)
print(root.root)
print(root.left.value)
print(root.right.value)
print(root.is_leaf_node())
print(root.is_root_node())
5
None
3
6
False
True
1
2