binary-search-tree

Why are few Testcases of finding Valid BST are failing here in python?

Why are few Testcases of finding Valid BST are failing here in python? Question: So, I am trying to find ,Given the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as follows: The left subtree of a node contains only nodes with keys …

Total answers: 1

Can i use yield to get the result of all the result in nested function?

Can i use yield to get the result of all the result in nested function? Question: I found many code to verify a valid binary tree, but I tried to creat a simple one, however it rise true all the time! Sorry I don’t understand the recursion very well. class TreeNode(object): def __init__(self, x): self.val …

Total answers: 2

Comparison fails when trying to find closest value in binary search tree

Comparison fails when trying to find closest value in binary search tree Question: I tried to get the closest value to a target value inside a binary tree with this programme and it didn’t work, although it worked well in manually (static) inserted tree !!! def clos(self,target): if self.root: self._clos(self.root,target) def _clos(self,curnode,value): a = curnode.val …

Total answers: 1

print all leaves in BST: why there should be two base cases?

print all leaves in BST: why there should be two base cases? Question: I’m looking at the solution of BST problem, printing all leaves in BST as below: def printLeaf(root): if not root: # why this line of code is needed if the if statement below returns as base case? return if not root.left and …

Total answers: 2

Implement an AVL-balanced search tree dictionary that only stores values in leaves

Implement an AVL-balanced search tree dictionary that only stores values in leaves Question: There was a task in school: to implement a dictionary based on an AVL-balanced search tree, in which data is stored in leaves. I wrote a dictionary class: an AVL-balanced search tree in which values are stored in nodes. How can I …

Total answers: 1

find a path to a given node in binary tree – Python

find a path to a given node in binary tree – Python Question: I’m stuck finding the path of the given node so I can find the right side but not the left side. class Node: def __init__(self, key): self.left = None self.right = None self.val = key def path(self, k): if not self.val: return …

Total answers: 1

How do I return the children of recursive nodes in a python binary tree?

How do I return the children of recursive nodes in a python binary tree? Question: I currently have my code set up so I have a class that creates the first node of a binary search tree. Then, using a create_node method, I can add whatever node. How can I return the children of each …

Total answers: 1

Error in Level Order Traversal of Binary Tree

Error in Level Order Traversal of Binary Tree Question: What mistake have i done here ? def levelOrder(root): #Write your code here que = [] que.append(root) while que != []: coot = que.pop() print(coot.data,end=" ") if coot.left is not None: que.append(coot.left) if coot.right is not None: que.append(coot.right) OutPut Expected:1 2 5 3 6 4 MY_output: …

Total answers: 1

Binary search tree lazy deletion Python

Binary search tree lazy deletion Python Question: I’ve created a binary search tree class and I’m trying to get a grasp on how "lazy deletion" works. I’ve set a flag in order to show if a node has been removed. When I search for a value that I want to remove, self.removed would be marked …

Total answers: 1

BST for storing student information and strategy pattern implementation

BST for storing student information and strategy pattern implementation Question: I’m trying to implement a binary search tree for storing student data such as first name, last name, id, and GPA and apply some strategy patterns to them. I’m not able to understand how to insert students’ data objects in the BST. Is there any …

Total answers: 1