Classic problem of LCA in BT: Given a Binary Tree find the lowest common ancestor.
I found a Python2 solution but don't know how to fix it in Python3.
# This is Python2, but having error in Python3 such that
# parent += node if all(subs) else max(subs),
# TypeError: '>' not supported between instances of 'NoneType' and 'NoneType'`
# lca.py
class Solution:
def lowestCommonAncestor(self, root, p, q):
answer = []
stack = [[root, answer]]
while stack:
top = stack.pop()
(node, parent), subs = top[:2], top[2:]
if node in (None, p, q):
parent += node,
elif not subs:
stack += top, [node.right, top], [node.left, top]
else:
parent += node if all(subs) else max(subs), # this is the problem in Python3
return answer[0]
s = Solution()
root = tree.create_tree3()
p = tree.find(root, 6)
q = tree.find(root, 1)
print(f'p = {p}, q = {q}')
lca = s.lowestCommonAncestor(root, p, q)
print(f"lca = {lca}")
# tree.py
class TreeNode:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def __repr__(self):
return str(self.val)
def find(root, val):
if root is None:
return None
if root.val == val:
return root
left = find(root.left, val)
right = find(root.right, val)
if left is not None:
return left
else:
return right
"""
3
5 1
6 2 0 8
7 4
"""
def create_tree3():
n3 = TreeNode(3)
n5 = TreeNode(5)
n1 = TreeNode(1)
n6 = TreeNode(6)
n2 = TreeNode(2)
n0 = TreeNode(0)
n8 = TreeNode(8)
n7 = TreeNode(7)
n4 = TreeNode(4)
n3.left, n3.right = n5, n1
n5.left, n5.right = n6, n2
n1.left, n1.right = n0, n8
n2.left, n2.right = n7, n4
return n3
I'm a newbie of Python3 and trying to fix this error in Python3, and I'm trying below, but not working.
class Solution:
def lowestCommonAncestor(self, root, p, q):
answer = []
stack = [[root, answer]]
while stack:
top = stack.pop()
(node, parent), subs = top[:2], top[2:]
if node in (None, p, q):
parent += node,
elif not subs:
stack += top, [node.right, top], [node.left, top]
else:
if subs[0] and subs[1]:
parent += node
else: # not sure how to fix the below part which is not working either
if subs[0] and not subs[1]:
parent += subs[0]
elif not subs[0] and subs[1]:
parent += subs[1]
elif not subs[0] and not subs[1]:
parent += None
return answer[0]
Related
I want to implement a huffman coding using AVL tree. I perform some insertion, then I delete some node and everything is fine, but suddenly after another deletion the tree delete every nodes on the right side from root. It happens when the tree looks like this (the right side may be a bit different, but the height is correct):
after deleting the minim node (M) the tree has only nodes (S, R).
I think that it is because of my mistake in rotation function. Could you help me understand where is the mistake.
import random
from collections import Counter
class HuffmanCoding:
def __init__(self, data=None, space=False):
# Transform data to the form of dictionary
if data is None:
data = input("Enter string for coding: ")
self.data = Counter(data)
elif isinstance(data, str):
self.data = Counter(data)
elif isinstance(data, dict):
self.data = data
else:
raise TypeError("Type of data is incorrect!")
# Check whether to delete the space or not
if not space:
self.data.pop(' ', None)
self.my_tree = AVL_Tree()
self.root = None
for k, v in self.data.items():
self.root = self.my_tree.insert(self.root, TreeNode(k, v))
def sth(self):
n1 = self.my_tree.delete_min(self.root)
n2 = self.my_tree.delete_min(self.root)
self._create_new_internal_node(n1, n2)
def _create_new_internal_node(self, n1, n2):
new_node = HuffmanNode(n1, n2)
self.my_tree.insert(self.root, new_node)
class HuffmanNode:
def __init__(self, n1, n2):
self.character = n1.character + n2.character
self.freq = n1.freq + n2.freq
self.left = None
self.right = None
self.height = 1
class TreeNode:
def __init__(self, character, freq):
self.character = character
self.freq = freq
self.left = None
self.right = None
self.height = 1
class AVL_Tree:
def insert(self, root, n):
character = n.character
freq = n.freq
# Normal insert, like in BST
if not root:
return TreeNode(character, freq)
elif freq < root.freq:
root.left = self.insert(root.left, n)
else:
root.right = self.insert(root.right, n)
# Update the height of the ancestor node
root.height = 1 + max(self.get_height(root.left), self.get_height(root.right))
# Get the balance
balance = self.get_balance(root)
# Left left
if balance > 1 and freq < root.left.freq:
return self.right_rotate(root)
# Right right
if balance < -1 and freq > root.right.freq:
return self.left_rotate(root)
# Left right
if balance > 1 and freq > root.left.freq:
root.left = self.left_rotate(root.left)
return self.right_rotate(root)
# Right left
if balance < -1 and freq < root.right.freq:
root.right = self.right_rotate(root.right)
return self.left_rotate(root)
return root
def delete_min(self, root):
if not root:
return root
# Get parent of most left node
min_parent = self.get_parent_of_min_value_node(root)
min_node = min_parent.left
# Check if most left node has a right son
if min_parent.left.right is not None:
min_parent.left = min_parent.left.right
else:
min_parent.left = None
# Update the height
root.height = 1 + max(self.get_height(root.left), self.get_height(root.right))
balance = self.get_balance(root)
# Left left
if balance > 1 and min_parent.freq < root.left.freq:
self.right_rotate(root)
# Right right
if balance < -1 and min_parent.freq > root.right.freq:
self.left_rotate(root)
# Left right
if balance > 1 and min_parent.freq > root.left.freq:
root.left = self.left_rotate(root.left)
self.right_rotate(root)
# Right left
if balance < -1 and min_parent.freq < root.right.freq:
root.right = self.right_rotate(root.right)
self.left_rotate(root)
return min_node
def get_height(self, root):
if not root:
return 0
return root.height
def get_balance(self, root):
if not root:
return 0
return self.get_height(root.left) - self.get_height(root.right)
def right_rotate(self, z):
y = z.left
T3 = y.right
y.right = z
z.left = T3
z.height = 1 + max(self.get_height(z.left), self.get_height(z.right))
y.height = 1 + max(self.get_height(y.left), self.get_height(y.right))
return y
def left_rotate(self, z):
y = z.right
T2 = y.left
# Perform rotation
y.left = z
z.right = T2
# Update heights
z.height = 1 + max(self.get_height(z.left), self.get_height(z.right))
y.height = 1 + max(self.get_height(y.left), self.get_height(y.right))
# Return the new root
return y
def pre_order(self, root):
if not root:
return
print("C: {0}, F: {1}".format(root.character, root.freq))
self.pre_order(root.left)
self.pre_order(root.right)
def in_order(self, root):
if not root:
return
self.in_order(root.left)
print("C: {0}, F: {1}".format(root.character, root.freq))
self.in_order(root.right)
def post_order(self, root):
if not root:
return
self.post_order(root.left)
self.post_order(root.right)
print("C: {0}, F: {1}".format(root.character, root.freq))
def get_min_value_node(self, root):
if root is None or root.left is None:
return root
return self.get_min_value_node(root.left)
def get_parent_of_min_value_node(self, root):
if root.left is None or root.left.left is None:
return root
return self.get_parent_of_min_value_node(root.left)
if __name__ == "__main__":
huffman_coding = HuffmanCoding("Blogoslawieni milosierni")
huffman_coding.my_tree.in_order(huffman_coding.root)
print(huffman_coding.my_tree.delete_min(huffman_coding.root).character)
print(huffman_coding.my_tree.delete_min(huffman_coding.root).character)
print(huffman_coding.my_tree.delete_min(huffman_coding.root).character)
print(huffman_coding.my_tree.delete_min(huffman_coding.root).character)
huffman_coding.my_tree.in_order(huffman_coding.root)
print(huffman_coding.my_tree.delete_min(huffman_coding.root).character)
huffman_coding.my_tree.in_order(huffman_coding.root)
x = huffman_coding.root
Did some research, but could only find examples where there was a key - say '5' and they count the occurrences of '5' in the linked list. I want to count each occurrence of each string in a llist. Say I have a linked list with ' a, a, a, b, d, f'. I want the output to say a - 3 b - 1 d -1 f -1.
I have built the list but the only way I can think of doing it is initializing a count variable, however I can't figure out how to reset it as I print the entire list after everything is done so right now my output looks like: a - 3 b -3 d -3 f -3.
Here is the code:
class Linked_List:
def __init__(self):
self.head = None
self.count = 0
def print(self):
p = self.head
while p is not None:
print(p.data, ' -', self.count)
p = p.next
def insert(self, x):
""""""
p = self.head
q = None
done = False
while not done:
if self.head == x:
done = True
elif p == None:
head = Linked_List_node(x)
q.next = head
done = True
elif x == p.data:
# head = Linked_List_node(x)
# self.head = head
self.count += 1
done = True
elif x < p.data:
if self.head == p:
head = Linked_List_node(x)
head.next = p
self.head = head
done = True
else:
head = Linked_List_node(x)
head.next = p
q.next = head
done = True
q = p
if p is not None:
p = p.next
class Linked_List_node:
def __init__(self, value):
self.data = value
self.next = None
Revised Code:
def print(self):
p = self.head
head = Linked_List_node(p.data)
while p is not None:
print(p.data, '-', self.count(p.data))
p = p.next
def count(self, x):
# loop thru list for all x, if find x add 1 to count. Assign final count to that word.
with open('cleaned_test.txt', 'r') as f:
for line in f:
for word in line.split():
if word == x:
self.count += 1
Since you want your count function to be able to count the frequencies of each word, I would create a function similar to print called count in class Linked_List, which iterates through the list, and updates the frequency dictionary.
def count(self):
dct = {}
p = self.head
while p is not None:
if p.data in dct:
dct[p.data] += 1
else:
dct[p.data] = 1
p = p.next
return dct
The output will look like.
head = Linked_List_node('a')
ll = Linked_List()
ll.head = head
for item in ['a', 'a', 'b', 'd', 'f']:
ll.insert(item)
print(ll.count())
#{'a': 3, 'b': 1, 'd': 1, 'f': 1}
I'm writing a singly linked list that counts the number of words that get imported from a test file.
I initialized count inside of my class.
I defined the print function, that prints out each of the nodes, after they are sorted.
I defined a count class to iterate through the list and count up all the occurences of a word fed to it.
I want to pass in every node into count to count how many times it appears in my text file. When I try to do that I end up with 'int' object is not callable. Here is my code
class Linked_List:
def __init__(self):
self.head = None
self.count = 1
def print(self):
p = self.head
head = Linked_List_node(p.data)
while p is not None:
print(p.data, '-', self.count(p.data)) # This is where the error appears
p = p.next
def count(self, x):
# loop thru list for all x, if find x add 1 to count. Assign final count to that word.
with open('cleaned_test.txt', 'r') as f:
for line in f:
for word in line.split():
if word == x:
self.count += 1
def insert(self, x):
""""""
p = self.head
q = None
done = False
while not done:
if self.head == x:
done = True
elif p == None:
head = Linked_List_node(x)
q.next = head
done = True
elif x == p.data:
#head = Linked_List_node(x)
#head.counter += 1
done = True
elif x < p.data:
if self.head == p:
head = Linked_List_node(x)
head.next = p
self.head = head
done = True
else:
head = Linked_List_node(x)
head.next = p
q.next = head
done = True
q = p
if p is not None:
p = p.next
class Linked_List_node:
def __init__(self, value):
self.data = value
self.next = None
Relevant part from your code is:
class Linked_List:
def __init__(self):
# ...
self.count = 1
def count(self, x):
# ...
The self.count = 1 assignment overwrites the value of self.count for every Linked_List object that's created, so it refers to 1 instead of the method you defined on the class. Rename either the variable or the function.
I am trying to create the GUI code for my connect four boardgame but error's keep on coming up that I don't know how to correct. Can anyone help? The error:
TypeError: init() missing 1 required positional argument: 'buttns_list'
Code:
def __init__(self):
self.mw = tkinter.Tk()
self.mw.title = ("Connect Four")
self.rows = 6
self.cols = 7
self.buttons_2d_list = []
for i in range (self.rows):
self.rows = ['']*self.cols
self.buttons_2d_list.append(self.rows)
self.gboard = ConnectFourBoard()
p1 = HumanPlayer("X")
p2 = ComputerPlayer("O", self.buttns_list)
self.players_1st = (p1, p2)
self.currnt_player_index = 0
self.winner = False
def clicked_btn(self, x, y):
p = self.players_1st[self.currnt_player_index]
button = self.buttons_2d_list[x][y]
if button["text"] == "":
button["text"] = p.get_player_symbol()
self.gboard.MakeMove(x, y, p.get_player_symbol())
winner = self.gboard.CheckForWin()
is_full = self.gboard.FullBoard()
if winner == True:
win_message = ("Player %s is the winner!" %p.get_player_symbol())
messagebox.showinfo("Winner Info", win_messge)
self.mw.destroy()
exit()
elif is_full == True:
messagebox.showinfo("Winner Info", "The game is a draw")
self.mw.destroy()
exit()
else:
pass
if self.currnt_player_index == 1:
self.currnt_player_index = 0
else:
self.currnt_player_index += 1
p = self.players_1st[self.currnt_player_index]
p.play()
import random
class ComputerPlayer(Player):
def __init__(self, letter, buttns_list):
Player.__init__(self, letter)
self.buttons_2d_list = buttns_list
def play(self):
pass
It's not clear to me from the rest of the code exactly what you should be passing here, but your init calls for a letter, which you do have, and a buttns_list, which you don't:
def __init__(self, letter, buttns_list):
So the error comes from this line:
p2 = ComputerPlayer("O")
Either:
1) Pass in a buttns_list if your ComputerPlayer class needs it
p2 = ComputerPlayer("O", self.buttons_2d_list)` # in GameGUI init
2) Get rid of it if this was added by mistake:
class ComputerPlayer(Player):
def __init__(self, letter):
Player.__init__(self, letter)
I am trying to print the path to every single leaf in this AVL Tree. My question is: why does the program spit out the memory locations and file names of the element instead of the element?
from BinaryTree import BinaryTree
from BinaryTree import TreeNode
class AVLTree(BinaryTree):
def __init__(self):
BinaryTree.__init__(self)
# Override the createNewNode method to create an AVLTreeNode
def createNewNode(self, e):
return AVLTreeNode(e)
def getPath(self, o):
path = BinaryTree.path(self, o);
return path
# Override the insert method to balance the tree if necessary
def insert(self, o):
successful = BinaryTree.insert(self, o)
if not successful:
return False # o is already in the tree
else:
self.balancePath(o) # Balance from o to the root if necessary
return True # o is inserted
# Update the height of a specified node
def updateHeight(self, node):
if node.left == None and node.right == None: # node is a leaf
node.height = 0
elif node.left == None: # node has no left subtree
node.height = 1 + (node.right).height
elif node.right == None: # node has no right subtree
node.height = 1 + (node.left).height
else:
node.height = 1 + max((node.right).height, (node.left).height)
# Balance the nodes in the path from the specified
# node to the root if necessary
def balancePath(self, o):
path = BinaryTree.path(self, o)
for i in range(len(path) - 1, -1, -1):
A = path[i]
self.updateHeight(A)
parentOfA = None if (A == self.root) else path[i - 1]
if self.balanceFactor(A) == -2:
if self.balanceFactor(A.left) <= 0:
self.balanceLL(A, parentOfA) # Perform LL rotation
else:
self.balanceLR(A, parentOfA) # Perform LR rotation
elif self.balanceFactor(A) == +2:
if self.balanceFactor(A.right) >= 0:
self.balanceRR(A, parentOfA) # Perform RR rotation
else:
self.balanceRL(A, parentOfA) # Perform RL rotation
# Return the balance factor of the node
def balanceFactor(self, node):
if node.right == None: # node has no right subtree
return -node.height
elif node.left == None: # node has no left subtree
return +node.height
else:
return (node.right).height - (node.left).height
# Balance LL (see Figure 14.2)
def balanceLL(self, A, parentOfA):
B = A.left # A is left-heavy and B is left-heavy
if A == self.root:
self.root = B
else:
if parentOfA.left == A:
parentOfA.left = B
else:
parentOfA.right = B
A.left = B.right # Make T2 the left subtree of A
B.right = A # Make A the left child of B
self.updateHeight(A)
self.updateHeight(B)
def getSearch(self, o):
return BinaryTree.search(self, o)
# Balance LR (see Figure 14.2(c))
def balanceLR(self, A, parentOfA):
B = A.left # A is left-heavy
C = B.right # B is right-heavy
if A == self.root:
self.root = C
else:
if parentOfA.left == A:
parentOfA.left = C
else:
parentOfA.right = C
A.left = C.right # Make T3 the left subtree of A
B.right = C.left # Make T2 the right subtree of B
C.left = B
C.right = A
# Adjust heights
self.updateHeight(A)
self.updateHeight(B)
self.updateHeight(C)
# Balance RR (see Figure 14.2(b))
def balanceRR(self, A, parentOfA):
B = A.right # A is right-heavy and B is right-heavy
if A == self.root:
self.root = B
else:
if parentOfA.left == A:
parentOfA.left = B
else:
parentOfA.right = B
A.right = B.left # Make T2 the right subtree of A
B.left = A
self.updateHeight(A)
self.updateHeight(B)
# Balance RL (see Figure 14.2(d))
def balanceRL(self, A, parentOfA):
B = A.right # A is right-heavy
C = B.left # B is left-heavy
if A == self.root:
self.root = C
else:
if parentOfA.left == A:
parentOfA.left = C
else:
parentOfA.right = C
A.right = C.left # Make T2 the right subtree of A
B.left = C.right # Make T3 the left subtree of B
C.left = A
C.right = B
# Adjust heights
self.updateHeight(A)
self.updateHeight(B)
self.updateHeight(C)
# Delete an element from the binary tree.
# Return True if the element is deleted successfully
# Return False if the element is not in the tree
def delete(self, element):
if self.root == None:
return False # Element is not in the tree
# Locate the node to be deleted and also locate its parent node
parent = None
current = self.root
while current != None:
if element < current.element:
parent = current
current = current.left
elif element > current.element:
parent = current
current = current.right
else:
break # Element is in the tree pointed by current
if current == None:
return False # Element is not in the tree
# Case 1: current has no left children (See Figure 23.6)
if current.left == None:
# Connect the parent with the right child of the current node
if parent == None:
root = current.right
else:
if element < parent.element:
parent.left = current.right
else:
parent.right = current.right
# Balance the tree if necessary
self.balancePath(parent.element)
else:
# Case 2: The current node has a left child
# Locate the rightmost node in the left subtree of
# the current node and also its parent
parentOfRightMost = current
rightMost = current.left
while rightMost.right != None:
parentOfRightMost = rightMost
rightMost = rightMost.right # Keep going to the right
# Replace the element in current by the element in rightMost
current.element = rightMost.element
# Eliminate rightmost node
if parentOfRightMost.right == rightMost:
parentOfRightMost.right = rightMost.left
else:
# Special case: parentOfRightMost is current
parentOfRightMost.left = rightMost.left
# Balance the tree if necessary
self.balancePath(parentOfRightMost.element)
self.size -= 1 # One element deleted
return True # Element inserted
def getLeafNodes(self):
return BinaryTree.leafNodes(self)
# AVLTreeNode is TreeNode plus height
class AVLTreeNode(TreeNode):
def __init__(self, e):
self.height = 0 # New data field
TreeNode.__init__(self, e)
this is the binary tree it calls
class BinaryTree:
def __init__(self):
self.counter = -1
self.root = None
self.size = 0
# Return True if the element is in the tree
def search(self, e):
current = self.root # Start from the root
while current != None:
if e < current.element:
current = current.left
elif e > current.element:
current = current.right
else: # element matches current.element
return True # Element is found
return False
def __iter__(self):
return self
def __next__(self):
x = self.inorder()
self.num = []
for i in range(x.getSize()):
self.num.append(x.pop())
self.num.reverse()
while True:
self.counter += 1
break
return self.num[self.counter]
# Insert element e into the binary search tree
# Return True if the element is inserted successfully
def insert(self, e):
if self.root == None:
self.root = self.createNewNode(e) # Create a new root
else:
# Locate the parent node
parent = None
current = self.root
while current != None:
if e < current.element:
parent = current
current = current.left
elif e > current.element:
parent = current
current = current.right
else:
return False # Duplicate node not inserted
# Create the new node and attach it to the parent node
if e < parent.element:
parent.left = self.createNewNode(e)
else:
parent.right = self.createNewNode(e)
self.size += 1 # Increase tree size
return True # Element inserted
# Create a new TreeNode for element e
def createNewNode(self, e):
return TreeNode(e)
# Return the size of the tree
def getSize(self):
return self.size
# Inorder traversal from the root
def inorder(self):
self.inorderHelper(self.root)
# Inorder traversal from a subtree
def inorderHelper(self, r):
if r != None:
self.inorderHelper(r.left)
print(r.element, end = " ")
self.inorderHelper(r.right)
# Postorder traversal from the root
def postorder(self):
self.postorderHelper(self.root)
# Postorder traversal from a subtree
def postorderHelper(self, root):
if root != None:
self.postorderHelper(root.left)
self.postorderHelper(root.right)
print(root.element, end = " ")
# Preorder traversal from the root
def preorder(self):
self.preorderHelper(self.root)
# Preorder traversal from a subtree
def preorderHelper(self, root):
if root != None:
print(root.element, end = " ")
self.preorderHelper(root.left)
self.preorderHelper(root.right)
# Returns a path from the root leading to the specified element
def path(self, e):
path = []
current = self.root # Start from the root
while current != None:
path.append(current) # Add the node to the list
if e < current.element:
current = current.left
elif e > current.element:
current = current.right
else:
break
return path # Return an array of nodes
# Delete an element from the binary search tree.
# Return True if the element is deleted successfully
# Return False if the element is not in the tree
def delete(self, e):
# Locate the node to be deleted and its parent node
parent = None
current = self.root
while current != None:
if e < current.element:
parent = current
current = current.left
elif e > current.element:
parent = current
current = current.right
else:
break # Element is in the tree pointed by current
if current == None:
return False # Element is not in the tree
# Case 1: current has no left children
if current.left == None:
# Connect the parent with the right child of the current node
if parent == None:
self.root = current.right
else:
if e < parent.element:
parent.left = current.right
else:
parent.right = current.right
else:
# Case 2: The current node has a left child
# Locate the rightmost node in the left subtree of
# the current node and also its parent
parentOfRightMost = current
rightMost = current.left
while rightMost.right != None:
parentOfRightMost = rightMost
rightMost = rightMost.right # Keep going to the right
# Replace the element in current by the element in rightMost
current.element = rightMost.element
# Eliminate rightmost node
if parentOfRightMost.right == rightMost:
parentOfRightMost.right = rightMost.left
else:
# Special case: parentOfRightMost == current
parentOfRightMost.left = rightMost.left
self.size -= 1
return True # Element deleted
def leafNodes(self):
self.leaves = []
return self.leafNodesHelper(self.root)
def leafNodesHelper(self, root):
if root != None:
if root.left == None and root.right == None:
self.leaves.append(root.element)
else:
self.leafNodesHelper(root.left)
self.leafNodesHelper(root.right)
return self.leaves
# Return true if the tree is empty
def isEmpty(self):
return self.size == 0
# Remove all elements from the tree
def clear(self):
self.root == None
self.size == 0
# Return the root of the tree
def getRoot(self):
return self.root
class TreeNode:
def __init__(self, e):
self.element = e
self.left = None # Point to the left node, default None
self.right = None # Point to the right node, default None
this is the test program that is frustrating me
from AVLTree20_3 import AVLTree
tree = AVLTree()
for i in range(1, 101):
tree.insert(i)
x = tree.getLeafNodes()
for i in range(len(x)):
path = tree.getPath(x[i])
print(path)
Please help me figure out why this won't work. I have tried a debugger and printing from all three files but none seem to print out properly why is this?
Your problem is that the path method returns a list of nodes.
have two solutions define the 1st override method (str) in the node class.
Or cross it as follows:
from AVLTree20_3 import AVLTree
tree = AVLTree()
for i in range(1, 101):
tree.insert(i)
x = tree.getLeafNodes()
for i in range(len(x)):
path = [x.element for x in tree.getPath(x[i])]
print(path,"\n---------------------")