public kiwinode(kiwinode <T> childl, kiwinode <T> childr, T key, kiwinode <T> parent) { this.childl = childl; this.childr = childr; this.key = key; this.parent = parent; }
public void insert(T value) { count++; kiwinode <T> current = root; if (root == null) { root = new kiwinode <T>(null, null, value, null); } while (current != null) { if (value.CompareTo(current.key) < 0) { if (current.childl == null) { current.childl = new kiwinode <T>(null, null, value, current); break; } current = current.childl; } else { if (current.childr == null) { current.childr = new kiwinode <T>(null, null, value, current); break; } current = current.childr; } } }
private kiwinode <T> max(kiwinode <T> node) { while (node.childr != null) { node = node.childr; } return(node); }
private kiwinode <T> min(kiwinode <T> node) { while (node.childl != null) { node = node.childl; } return(node); }
public bool deletion(T val) { kiwinode <T> todel = finder(val); if (todel == null) { return(false); } delete(todel); count--; return(true); }
private void delete(kiwinode <T> node) { if (node.childcount() == 2) { kiwinode <T> candy = min(node.childr); node.key = candy.key; node = candy; } if (node.childcount() == 1) { kiwinode <T> child; if (node.childl == null) { child = node.childr; } else { child = node.childl; } if (node == root) { root = child; } else if (node.ischildr()) { node.parent.childr = child; } else if (!node.ischildr()) { node.parent.childl = child; } } if (node.childcount() == 0) { if (node == root) { root = null; } else if (node.ischildr()) { node.parent.childr = null; } else if (!node.ischildr()) { node.parent.childl = null; } } }
public IEnumerable <T> preorder() { LinkedList <T> nodes = new LinkedList <T>(); bool finished = false; bool same; while (true) { kiwinode <T> current = root; same = false; for (int i = 0; i < nodes.Count; i++) { LinkedListNode <T> node = nodes.First; if (node.Value.CompareTo(current.key) == 0) { same = true; } } if (current.childl != null && !same) { nodes.AddLast(current.key); current = current.childl; } else if (current.childr != null && !same) { nodes.AddLast(current.key); current = current.childr; } else if (current.parent == null) { break; } else { current = current.parent; } } return(nodes); }
private kiwinode <T> finder(T key) { kiwinode <T> current = root; while (current != null) { int comp = key.CompareTo(current.key); if (comp < 0) { current = current.childl; } if (comp > 0) { current = current.childr; } if (comp == 0) { return(current); } } return(null); }