I solved an exercise where I had to apply a recursive algorithm to a tree that's so defined:
class GenericTree:
""" A tree in which each node can have any number of children.
Each node is linked to its parent and to its immediate sibling on the right
"""
def __init__(self, data):
self._data = data
self._child = None
self._sibling = None
self._parent = None
I had to concatenate the data of the leaves with the data of the parents and so on until we arrive to the root that will have the sum of all the leaves data. I solved it in this way and it works but it seems very tortuous and mechanic:
def marvelous(self):
""" MODIFIES each node data replacing it with the concatenation
of its leaves data
- MUST USE a recursive solution
- assume node data is always a string
"""
if not self._child: #If there isn't any child
self._data=self._data #the value remains the same
if self._child: #If there are children
if self._child._child: #if there are niece
self._child.marvelous() #reapply the function to them
else: #if not nieces
self._data=self._child._data #initializing the name of our root node with the name of its 1st son
#if there are other sons, we'll add them to the root name
if self._child._sibling: #check
current=self._child._sibling #iterating through the sons-siblings line
while current:
current.marvelous() #we reapplying the function to them to replacing them with their concatenation (bottom-up process)
self._data+=current._data #we sum the sibling content to the node data
current=current._sibling #next for the iteration
#To add the new names to the new root node name:
self._data="" #initializing the root str value
current=self._child #having the child that through recursion have the correct str values, i can sum all them to the root node
while current:
self._data+=current._data
current=current._sibling
if self._sibling: #if there are siblings, they need to go through the function themselves
self._sibling.marvelous()
Basically I check if the node tree passed has children: if not, it remains with the same data.
If there are children, I check if there are nieces: in this case I restart the algorithm until I can some the leaves to the pre-terminal nodes, and I sum the leaves values to put that sum to their parents'data.
Then, I act on the root node with the code after the first while loop, so to put its name as the sum of all the leaves.
The final piece of code serves as to make the code ok for the siblings in each step.
How can I improve it?
It seems to me that your method performs a lot of redundant recursive calls.
For example this loop in your code:
while current:
current.marvelous()
self._data += current._data
current = current._sibling
is useless because the recursive call will be anyway performed by the last
instruction in your method (self._sibling.marvelous()). Besides,
you update self._data and then right after the loop you reset
self._data to "".
I tried to simplify it and came up with this solution that seems to
work.
def marvelous(self):
if self.child:
self.child.marvelous()
# at that point we know that the data for all the tree
# rooted in self have been computed. we collect these
self.data = ""
current = self.child
while current:
self.data += current.data
current = current.sibling
if self.sibling:
self.sibling.marvelous()
And here is a simpler solution:
def marvelous2(self):
if not self.child:
result = self.data
else:
result = self.child.marvelous2()
self.data = result
if self.sibling:
result += self.sibling.marvelous2()
return result
marvelous2 returns the data computed for a node and all its siblings. This avoids performing the while loop of the previous solution.
Related
I am trying to understand why does inserting the same node twice in a singly linked list causes an infinite loop.
I tried to insert the last node as the new head node. But when I run the code, it's starting an infinite loop I can see since I am calling a method to print nodes at the end. Here's my code.
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insertLast(self, newNode):
if self.head is None:
self.head = newNode
else:
lastNode = self.head
while True:
if lastNode.next is None:
break
lastNode = lastNode.next
lastNode.next = newNode
def insertHead(self, newNode):
# x, y ,z. => new head, x,y,z
if self.head is None:
print("List is empy please call inserlast()")
else:
currentHead = self.head
self.head = newNode
self.head.next = currentHead
def printList(self):
if self.head is None:
print("EMPTY List. No Data found!")
return
else:
currentNode = self.head
while True:
print(currentNode.data)
currentNode = currentNode.next
if currentNode is None:
break
node1 = Node("Head")
node2 = Node("Some Data")
node3 = Node("Some More Data")
# I am adding this node at the end of the list
newnode1 = Node("New Head")
linkedList = LinkedList()
# create a new linked list by inserting at end
linkedList.insertLast(node1)
linkedList.insertLast(node2)
linkedList.insertLast(newnode1)
# using a node i have already added in the list as New head of the list
linkedList.insertHead(newnode1)
linkedList.printList()
When you re-add an existing node to the list, you don't do anything about existing nodes that already referenced the re-added node. In your case, you now have a node at the end of the list whose next points back to the node which has just become the head, and so now your list is circular, which is going to cause any function which tries to traverse the list to infaloop.
There are three ways I can think of to solve this, and I'll save the best for last:
Add cycle detection logic to protect you from infalooping when your list becomes circular (i.e. break the cycles or just stop traversing once you get back to someplace you've already been). This is nontrivial and IMO not a great solution; better to keep the list from getting broken in the first place than have to constantly check to see if it needs fixing.
Add a removeNode function to ensure that a given node has been fully removed from the list (i.e. by scanning through the whole list to see what other nodes, if any, reference the given node, and adjusting pointers to skip over it). Then you can safely re-insert a node by first removing it. Note: if the caller passes you a Node that belongs to a DIFFERENT list, you can still end up in trouble, because you'll have no way of finding that other list! Circular structures will still be possible by building lists and then inserting the nodes of each into the other.
Don't allow callers of your list class to access the actual nodes at all; have them insert values (rather than nodes) so that you can ensure that they're never giving you a node with weird pointer values (a node that belongs to another list, a node that points to itself, etc). The pointers should be completely internal to your linked list class rather than something that the caller can mess up.
Briefly, according to your code, the infinite loop you face results from the fact that The linked list does not arrange the nodes physically, it just tells each node which node is next.
Let us explain it in details:
As shown, each node has a next attribute which is set by LinkedList to the next one.
after initiating the Nodes
node1.next mentions to None
node2.next mentions to None
newnode1.next mentions to None
after creating the linked list
node1.next mention to Node2
node2.next mention to Newnode1
newnode1.next mention to None
after inserting Newnode1 again as a head, the linked list set Nownode1.next to mention to Node1, and the hole figure becomes:
node1.next mention to Node2
node2.next mention to Newnode1
newnode1.next mention to Node1
Therefor, it becomes a closed loop
The linked list does not arrange the nodes physically, it just tells each node which node is next.
That is the explanation of the infinite loop you face.
Good Luck
I had a quiz recently and this is what the question looked like:-
You may use the following Node class:
class Node:
"""Lightweight, nonpublic class for storing a singly linked node."""
__slots__ = 'element', 'next' # streamline memory usage
def __init__(self, element, next): # initialize node's fields
self.element = element # reference to user's element
self.next = next # reference to next node
Assume you have a singly-linked list of unique integers. Write a Python method that traverses this list to find the smallest element, removes the node that contains that value, and inserts the smallest value in a new node at the front of the list. Finally, return the head pointer. For simplicity, you may assume that the node containing the smallest value is not already at the head of the list (ie, you will never have to remove the head node and re-add it again).
Your method will be passed the head of the list as a parameter (of type Node), as in the following method signature:
def moveSmallest(head):
You may use only the Node class; no other methods (like size(), etc) are available. Furthermore, the only pointer you have is head (passed in as a parameter); you do not have access to a tail pointer.
For example, if the list contains:
5 → 2 → 1 → 3
the resulting list will contain:
1 → 5 → 2 → 3
Hint 1: There are several parts to this question; break the problem down and think about how to do each part separately.
Hint 2: If you need to exit from a loop early, you can use the break command.
Hint 3: For an empty list or a list with only one element, there is nothing to do!
My answer:
def moveSmallest(h):
if h==None or h.next==None:
return h
# find part
temp=h
myList=[]
while temp!=None:
myList.append(temp.element)
temp=temp.next
myList.sort()
sv=myList[0]
# remove part
if h.element==sv and h.next!=None:
h=h.next
else:
start=h
while start!=None:
if start.next.element==sv and start.next.next!=None:
start.next=start.next.next
break
if start.next.element==sv and start.next.next==None:
start.next=None
break
start=start.next
# Insert part
newNode=Node(sv)
newNode.next=h
h=newNode
return h
Mark received=10/30
Feedback on my answer:
"Not supposed to use sorting; searching the list should be the way we've covered in class.
You're advancing too far ahead in the list without checking whether nodes exist.
Review the 'singly-linked list' slides and answer this question as the examples suggest."
As you can see I am finding the element in the list and removing it and then adding it to the list as a head node. I ran this code and it works fine. As you can see in the feedback he says "You're advancing too far ahead in the list without checking whether nodes exist." which is taken care by the first if statement in my answer and for "Not supposed to use sorting; searching the list should be the way we've covered in class." I believe my mistake was to use the list at the first place but given the code the final score should be or be more than 20/30. Can you guys please check this or give your opinion on this feedback?
As you can see in the feedback he says "You're advancing too far ahead
in the list without checking whether nodes exist." which is taken care
by the first if statement in my answer
It's not taken care of. The first if-statement of your function just checks to see if the head exists and that the node following the head exists as well (basically, asserting that you have at least two nodes in the linked list).
What you have:
if h.element==sv and h.next!=None:
h=h.next
else:
start=h
while start!=None:
if start.next.element==sv and start.next.next!=None:
If you enter the while loop, you only know the following things:
Your linked list has at least two elements
h.element != sv or h.next == None
The current node is not None
The node following the current node (start.next), however, may be None at some point - when you reach the end of your linked list. Therefore, you are trying to access a node that doesn't exist.
Here's how I would have done it. I haven't tested this, but I'm pretty sure this works:
def moveSmallest(head):
if head is None or head.next is None:
return head
# First, determine the smallest element.
# To do this, we need to visit each node once (except the head).
# Create a cursor that "iterates" through the nodes
# (The cursor can start at the 2nd element because we're guaranteed the head will never have the smallest element.)
cursor = head.next
current_minimum = head.next.element
while cursor is not None:
if current_minimum > cursor.element:
# We've found the new minimum.
current_minimum = cursor.element
cursor = cursor.next
# At this point, current_minimum is the smallest element.
# Next, go through the linked list again until right before we reach the node with the smallest element.
cursor = head
while cursor.next is not None:
if cursor.next.element == current_minimum:
# We want to reconnect the arrows.
if cursor.next.next is None:
cursor.next = None
else:
cursor.next = cursor.next.next
break
new_node = Node(current_minimum, head)
head = new_node
return head
I have this Binary Tree Structure:
# A Node is an object
# - value : Number
# - children : List of Nodes
class Node:
def __init__(self, value, children):
self.value = value
self.children = children
I can easily sum the Nodes, recursively:
def sumNodesRec(root):
sumOfNodes = 0
for child in root.children:
sumOfNodes += sumNodesRec(child)
return root.value + sumOfNodes
Example Tree:
exampleTree = Node(1,[Node(2,[]),Node(3,[Node(4,[Node(5,[]),Node(6,[Node(7,[])])])])])
sumNodesRec(exampleTree)
> 28
However, I'm having difficulty figuring out how to sum all the nodes iteratively. Normally, with a binary tree that has 'left' and 'right' in the definition, I can find the sum. But, this definition is tripping me up a bit when thinking about it iteratively.
Any help or explanation would be great. I'm trying to make sure I'm not always doing things recursively, so I'm trying to practice creating normally recursive functions as iterative types, instead.
If we're talking iteration, this is a good use case for a queue.
total = 0
queue = [exampleTree]
while queue:
v = queue.pop(0)
queue.extend(v.children)
total += v.value
print(total)
28
This is a common idiom. Iterative graph traversal algorithms also work in this manner.
You can simulate stacks/queues using python's vanilla lists. Other (better) alternatives would be the collections.deque structure in the standard library. I should explicitly mention that its enque/deque operations are more efficient than what you'd expect from a vanilla list.
Iteratively you can create a list, stack, queue, or other structure that can hold the items you run through. Put the root into it. Start going through the list, take an element and add its children into the list also. Add the value to the sum. Take next element and repeat. This way there’s no recursion but performance and memory usage may be worse.
In response to the first answer:
def sumNodes(root):
current = [root]
nodeList = []
while current:
next_level = []
for n in current:
nodeList.append(n.value)
next_level.extend(n.children)
current = next_level
return sum(nodeList)
Thank you! That explanation helped me think through it more clearly.
What is wrong with my insert function? I'm passing along the tr and the element el that I wish to insert, but I keep getting errors...
def insert( tr,el ):
""" Inserts an element into a BST -- returns an updated tree """
if tr == None:
return createEyecuBST( el,None )
else:
if el > tr.value:
tr.left = createEyecuBST( el,tr )
else:
tr.right = createEyecuBST( el,tr )
return EyecuBST( tr.left,tr.right,tr)
Thanks in advance.
ERROR:
ValueError: Not expected BST with 2 elements
It's a test function that basically tells me whether or not what I'm putting in is what I want out.
So, the way insertion in a binary tree usually works is that you start at the root node, and then decide which side, i.e. which subtree, you want to insert your element. Once you have made that decision, you are recursively inserting the element into that subtree, treating its root node as the new root node.
However, what you are doing in your function is that instead of going down towards the tree’s leaves, you are just creating a new subtree with the new value immediately (and generally mess up the existing tree).
Ideally, an binary tree insert should look like this:
def insert (tree, value):
if not tree:
# The subtree we entered doesn’t actually exist. So create a
# new tree with no left or right child.
return Node(value, None, None)
# Otherwise, the subtree does exist, so let’s see where we have
# to insert the value
if value < tree.value:
# Insert the value in the left subtree
tree.left = insert(tree.left, value)
else:
# Insert the value in the right subtree
tree.right = insert(tree.right, value)
# Since you want to return the changed tree, and since we expect
# that in our recursive calls, return this subtree (where the
# insertion has happened by now!).
return tree
Note, that this modifies the existing tree. It’s also possible that you treat a tree as an immutable state, where inserting an element creates a completely new tree without touching the old one. Since you are using createEyecuBST all the time, it is possible that this was your original intention.
To do that, you want to always return a newly created subtree representing the changed state of that subtree. It looks like this:
def insert (tree, value):
if tree is None:
# As before, if the subtree does not exist, create a new one
return Node(value, None, None)
if value < tree.value:
# Insert in the left subtree, so re-build the left subtree and
# return the new subtree at this level
return Node(tree.value, insert(tree.left, value), tree.right)
elif value > tree.value:
# Insert in the right subtree and rebuild it
return Node(tree.value, tree.left, insert(tree.right, value))
# Final case is that `tree.value == value`; in that case, we don’t
# need to change anything
return tree
Note: Since I didn’t know what’s the difference in your createEyecuBST function and the EyecuBST type is, I’m just using a type Node here which constructer accepts the value as the first parameter, and then the left and right subtree as the second and third.
Since the binary doesn't have the need to balance out anything , you can write as simple logic as possible while traversing at each step .
--> Compare with root value.
--> Is it less than root then go to left node.
--> Not greater than root , then go to right node.
--> Node exists ? Make it new root and repeat , else add the new node with the value
def insert(self, val):
treeNode = Node(val)
placed = 0
tmp = self.root
if not self.root:
self.root = treeNode
else:
while(not placed):
if val<tmp.info:
if not tmp.left:
tmp.left = treeNode
placed = 1
else:
tmp = tmp.left
else:
if not tmp.right:
tmp.right = treeNode
placed = 1
else:
tmp = tmp.right
return
You can also make the function recursive , but it shouldn't return anything. It will just attach the node in the innermost call .
I'm having trouble trying to implement a linked List without using classes(we're not there yet in my course), and google hasn't helpful at all. Every linked list example uses classes, which I haven't covered. I can create a linked list that adds a value to the beginning of the linked list, but I don't know how to traverse the list and add value after a specific node. Any help would be appreciated. The hardest part for me is figuring out how to traverse the list.
def addValue(linkedSet, value):
"""
Adds a new element to a set.
Parameters:
the set to be changed (address of the first node)
the new value to add to the set
Return result: pointer to the head of the modified set. This is
usually the same as the linkedSet parameter, unless the value
added is less than any value already in the set.
If the value is already in the set, return the set unchanged.
This is not an error.
"""
newNode={}
newNode['data']=value
node=linkedSet
if linkedSet==None:
newNode['next']=None
return newNode
if member(linkedSet,value)==True:
return linkedSet
elif linkedSet['next']==None:
newNode['next']=None
linkedSet['next']=newNode
elif linkedSet['next']!=None:
return linkedSet
Just as a general outline of what I think your addValue() function might look like...
def addValue(linkedSet, value):
newNode={
'data': value,
'next': None
}
# if linkedSet is None, then you can just return this newNode
# if linkedSet isnt None...
# if linkedSets next is None, then it should just point to this newNode
# (append)
# otherwise, you should set its current next to the next of this newnode,
# and then set its next to this newNode (insert)
This is for a general linked list. It seems like you are suggesting that yours is a more specialized version that maintains a value sort, and always expects to be passed the head node of the list. Yours would require constant looping over each 'next' until it found one where the value was greater than the current, and then insert itself by shifting around the 'next' references of the following (and possibly previous) elements.
unless the value
added is less than any value already in the set sounds like this list is supposed to be sorted. So you go through the list, find the first item larger than your value and splice it in there.
You traverse the list by keeping track of the current node:
def addValue(linkedSet, value):
newNode={}
newNode['data']=value
newNode['next'] = None
#traverse list
current = linkedSet
while True:
if current['value'] == value:
return linkedSet # it was already in that list
if current['value'] > value:
# new node is the new head
newNode['next'] = linkedSet
return newNode # new head
if current['next'] is None:
# new tail
current['next'] = new_node
return linkedSet
if current['next']['value'] > value:
# new node belongs here, splice it in
next_node = current['next']
current['next'] = newNode
newNode['next'] = next_node
return linkedSet
# didnt return so far, try the next element:
current = current['next']
How about using dictionary as linked list descriptor?
Something like:
linkedSet = {'first':firstNode, 'last':lastNode}
Then when you need to travers you could always start from first node,
and when you need to add you have your end node.