How to implement a binary search tree in Python?

Here is a quick example of a binary insert: class Node: def __init__(self, val): self.l_child = None self.r_child = None self.data = val def binary_insert(root, node): if root is None: root = node else: if root.data > node.data: if root.l_child is None: root.l_child = node else: binary_insert(root.l_child, node) else: if root.r_child is None: root.r_child = … Read more

Java generics issue: Class “not within bounds of type-variable” error.

In MySearchTree the K of the base type is Course. So K must “extend” Comparable<Keyable<Course>> & Keyable<Course>. But it doesn’t, it extends Comparable<Keyable<DataElement>> & Keyable<DataElement>. I guess DataElement should be generified in a similar manner to Comparable or Enum. public interface Keyable <T> {public String getKey();} public interface DataElement<THIS extends DataElement<THIS>> extends Comparable<Keyable<THIS>>, Keyable<THIS>, Serializable … Read more

PHP daylight saving time detection

As Jimmy points out you can use timezone transitions, but this is not available on PHP <5.3. as dateTimeZone() is PHP>=5.2.2 but getTransitions() with arguments is not! In that case here is a function that can give you timezone data, including whether in DST or not. function timezonez($timezone=”Europe/London”){ $tz = new DateTimeZone($timezone); $transitions = $tz->getTransitions(); … Read more

How do you validate a binary search tree?

Actually that is the mistake everybody does in an interview. Leftchild must be checked against (minLimitof node,node.value) Rightchild must be checked against (node.value,MaxLimit of node) IsValidBST(root,-infinity,infinity); bool IsValidBST(BinaryNode node, int MIN, int MAX) { if(node == null) return true; if(node.element > MIN && node.element < MAX && IsValidBST(node.left,MIN,node.element) && IsValidBST(node.right,node.element,MAX)) return true; else return false; … Read more