1. BTNodes
Binary tree code has two children which are in order, the left one and the right one.
eg:
b = BTNode(8, BTNode(4, BTNode(2, None, None), BTNode(6, None, None)), BTNode(12, None, BTNode(14, None, None)))
print(b)
14
12
8
6
4
2
So when we write some recursions linked with BTNodes, we need to pay much attention to the children. What we usually do is operating the left tree and operating the right tree and combining them. And when it comes to list_between(node, start, end), we do not have to compare all the data in the node because data in left subtree are less than node.data and data in right subtree are more than node.data .
2. LLNode
Linked lists are lists made up of an item (value) and a sub-list (rest). And the objects (nodes) have a value and a reference to other similar objects.
eg
>>> n = LLNode(5, LLNode(7))
>>> print(n)
5 -> 7 ->|
In order to make a reference to the node and move it along the node, what we usually do is using the codes below:
The Linked list nodes can be quite confusing and we shall always draw pictures to make the situations much more clear. And what can't be ignored is that once we change the LinkedList, we need to change last node, former last node, back, size and possibly front.
eg:
b = BTNode(8, BTNode(4, BTNode(2, None, None), BTNode(6, None, None)), BTNode(12, None, BTNode(14, None, None)))
print(b)
14
12
8
6
4
2
So when we write some recursions linked with BTNodes, we need to pay much attention to the children. What we usually do is operating the left tree and operating the right tree and combining them. And when it comes to list_between(node, start, end), we do not have to compare all the data in the node because data in left subtree are less than node.data and data in right subtree are more than node.data .
2. LLNode
Linked lists are lists made up of an item (value) and a sub-list (rest). And the objects (nodes) have a value and a reference to other similar objects.
eg
>>> n = LLNode(5, LLNode(7))
>>> print(n)
5 -> 7 ->|
In order to make a reference to the node and move it along the node, what we usually do is using the codes below:
cur_node = self.front
while <some condition here...>:
# do something here...
cur_node = cur_node.nxt
没有评论:
发表评论