private TreeItem FindWithParent(K key, out TreeItem parent) { // looking item TreeItem current = _root; parent = null; while (current != null) { int result = current.CompareTo(key); if (result > 0) { // item < 0 (left) parent = current; current = current.Left; } else if (result < 0) { // item > 0 (right) parent = current; current = current.Right; } else { break; } } return(current); }
private TreeItem <Type> RecursiveSearch(TreeItem <Type> item, TreeItem <Type> root) { if (root == null) { return(null); } if (item.CompareTo(root) == 0) { return(root); } else if (item.CompareTo(root) < 0) { return(RecursiveSearch(item, root.Left)); } else { return(RecursiveSearch(item, root.Right)); } }
private TreeItem <Type> RecursiveRemove(TreeItem <Type> who, TreeItem <Type> where, out TreeItem <Type> output) { output = null; if (where == null) { return(null); } if (who.CompareTo(where) < 0) { where.Left = RecursiveRemove(who, where.Left, out output); } else if (who.CompareTo(where) > 0) { where.Right = RecursiveRemove(who, where.Right, out output); } else { output = new TreeItem <Type>(where.Value); int rate = where.Rate; switch (rate) { case 0: return(null); case -1: return(where.Left); case 1: return(where.Right); case 2: // Corrigir pois predec = where.predecessor. Codigo assim pra fazer funcionar o resto :p TreeItem <Type> predec = where; where.Value = predec.Value; where.Left = RecursiveRemove(predec, where.Left, out output); break; } } return(where); }
private TreeItem <Type> RecursiveInsert(TreeItem <Type> item, TreeItem <Type> root) { if (root == null) { return(item); } if (item.CompareTo(root) < 0) { root.Left = RecursiveInsert(item, root.Left); } else { root.Right = RecursiveInsert(item, root.Right); } return(root); }