public NodeDic() { this.val = default(T); this.left = null; this.right = null; this.parent = null; this.key = int.MaxValue; }
/// <summary> /// Добавление элемента в словарь по ключу /// </summary> /// <param name="val"></param> /* public void Add(int key, Tval val) * { * bool added; * * if (top == null) * { * NodeDic<Tval> NewNode = new NodeDic<Tval>(val); * top.key = key; * top = NewNode; * count++; * return; * } * * NodeDic<Tval> currentNode = top; * added = false; * * do * { * if ((currentNode.left == null) || (currentNode.right == null)) * { * if ((currentNode.left == null) && (currentNode.right != null)) * { * if (key == currentNode.key) throw new Exception("The key already exists"); * * if (key < currentNode.key) * { * NodeDic<Tval> NewNode = new NodeDic<Tval>(val); * currentNode.left = NewNode; * currentNode.left.key = key; * sw.WriteLine(currentNode.val + "->" + currentNode.left.val + "[color=red]" + ";"); * count++; * added = true; * return; * } * * if (key > currentNode.key) currentNode = currentNode.right; * } * * if ((currentNode.right == null) && (currentNode.left != null)) * { * if (key == currentNode.key) throw new Exception("The key already exists"); * * if (key > currentNode.key) * { * NodeDic<Tval> NewNode = new NodeDic<Tval>(val); * currentNode.right = NewNode; * currentNode.right.key = key; * sw.WriteLine(currentNode.val + "->" + currentNode.right.val + "[color=red]" + ";"); * count++; * added = true; * return; * } * if (key < currentNode.key) currentNode = currentNode.left; * } * * if ((currentNode.left == null) && (currentNode.right == null)) * { * if (key == currentNode.key) throw new Exception("The key already exists"); * * if (key < currentNode.key) * { * NodeDic<Tval> NewNode = new NodeDic<Tval>(val); * currentNode.left = NewNode; * currentNode.left.key = key; * sw.WriteLine(currentNode.val + "->" + currentNode.left.val + "[color=red]" + ";"); * count++; * added = true; * return; * } * if (key > currentNode.key) * { * NodeDic<Tval> NewNode = new NodeDic<Tval>(val); * currentNode.right = NewNode; * currentNode.right.key = key; * sw.WriteLine(currentNode.val + "->" + currentNode.right.val + "[color=red]" + ";"); * count++; * added = true; * return; * } * } * } * else * { * if (key < currentNode.key) * currentNode = currentNode.left; * if (key < currentNode.key) * currentNode = currentNode.right; * } * } while (!added); * * }*/ /// <summary> /// Добавление элемента по ключу /// </summary> /// <param name="nextNode"></param> public void Add(NodeDic <Tval> nextNode) { if (top == null) { top = nextNode; top.key = nextNode.key; top.val = nextNode.val; nextNode.parent = null; count++; return; } NodeDic <Tval> currentNode = top; NodeDic <Tval> previousNode = null; while (currentNode != null) { previousNode = currentNode; if (nextNode.key > currentNode.key) { currentNode = currentNode.right; } else { currentNode = currentNode.left; } } if (nextNode.key < previousNode.key) { previousNode.left = nextNode; sw.WriteLine(previousNode.key + "->" + nextNode.key + "[color=red]" + ";"); } else { previousNode.right = nextNode; sw.WriteLine(previousNode.key + "->" + nextNode.key + "[color=red]" + ";"); } nextNode.parent = previousNode; count++; return; }
/// <summary> /// Поиск элемента по ключу /// </summary> /// <param name="key"></param> /// <returns></returns> public Tval Find(int key) { NodeDic <Tval> currentNode = top; while (currentNode != null) { if (currentNode.key == key) { return(currentNode.val); } if (key < currentNode.key) { currentNode = currentNode.left; } if (key > currentNode.key) { currentNode = currentNode.right; } } throw new Exception("The dictionary is empty"); }
/// <summary> /// (ключ головы, значение головы, вместительность словаря) /// </summary> /// <param name="capacity"></param> public MyDictionary(int key, Tval val, int capacity) { this.top = null; this.capacity = capacity; this.count = 1; }
/// <summary> /// Дефолт конструктор /// </summary> public MyDictionary() { top = null; this.count = 0; this.capacity = 10; }
/// <summary> /// Удаление элемента /// </summary> /// <param name="key"></param> public void Remove(int key) { NodeDic <Tval> currentNode = top; NodeDic <Tval> parent = null; while (currentNode != null && currentNode.key != key) { parent = currentNode; if (key < currentNode.key) { currentNode = currentNode.left; } else { currentNode = currentNode.right; } } if (currentNode != null) { NodeDic <Tval> removed = null; if (currentNode.left == null || currentNode.right == null) { NodeDic <Tval> child = null; removed = currentNode; if (currentNode.left != null) { child = currentNode.left; } else if (currentNode.right != null) { child = currentNode.right; } if (parent == null) { top = child; } else { if (parent.left == currentNode) { parent.left = child; } else { parent.right = child; } } } else { NodeDic <Tval> mostLeft = currentNode.right; NodeDic <Tval> mostLeftParent = currentNode; while (mostLeft.left != null) { mostLeftParent = mostLeft; mostLeft = mostLeft.left; } currentNode.key = mostLeft.key; removed = mostLeft; if (mostLeftParent.left == mostLeft) { mostLeftParent.left = null; } else { mostLeftParent.right = null; } } } }