''' mbinary ######################################################################### # File : splayTree.py # Author: mbinary # Mail: zhuheqin1@gmail.com # Blog: https://mbinary.xyz # Github: https://github.com/mbinary # Created Time: 2018-05-19 23:06 # Description: ######################################################################### ''' from collections import deque,Iterable # use isinstance(obj,Iterable) to judge if an obj is iterable class node: def __init__(self,val = None,left=None,right=None,parent=None): self.val = val if val :self.freq = 1 else :self.freq = 0 self.left = left self.right = right self.parent = parent def getChild(self,s=0): if isinstance(s,int):s =[s] last = self for i in s: if not last:return None if i == 0: last = last.left else:last = last.right return last def setChild(self,child,s=0): if isinstance(s,Iterable): i = s[0] del s[0] if i == 0:self.left.setChild(child,s) else:self.right.setChild(child,s) elif s:self.right = child else:self.left = child class splayTree: def __init__(self,s=[]): s = list(s) self.root = None s = sorted(s,reverse = True) for i in s: self.insert(self.root,i) def insert(self,k): if not self.root :self.root = node(k) else:self._insert(self.root,k) def _insert(self,root,k): if root.val == k : root.freq +=1 elif root.val k: return self._find(root.left,k) elif root.val