/// <summary> /// Gets or sets the value associated with the specified key. /// </summary> /// <param name="key">The key of the value to get or set.</param> /// <value>The value associated with the specified key. If the specified key is not found, a get operation throws a KeyNotFoundException, and a set operation creates a new element with the specified key.</value> public TValue this[TKey key] { get { if (key == null) { throw new ArgumentNullException("key"); } KeyValueTreeNode node = this.Find(key); if (node == null) { throw new KeyNotFoundException("The given key was not present in the collection."); } return(node.Value); } set { if (key == null) { throw new ArgumentNullException("key"); } KeyValueTreeNode node = this.Find(key); if (node == null) { node = this.Add(new KeyValueTreeNode(key, value)); } node.Value = value; } }
public bool MoveNext() { KeyValueTreeNode successor; if (tree.root == null) { return(false); } if (currentNode == null) { currentNode = (KeyValueTreeNode)tree.root; while (currentNode.Left != null) { currentNode = currentNode.Left; } return(true); } if ((successor = (KeyValueTreeNode)tree.Successor(currentNode)) != null) { currentNode = successor; return(true); } return(false); }
protected override KeyValueTreeNode Find(TKey key) { KeyValueTreeNode current = (KeyValueTreeNode)this.root, previous = null; while (current != null) { int c = key.CompareTo(current.Key); previous = current; if (c < 0) { current = current.Left; } else if (c > 0) { current = current.Right; } else { break; } } this.Splay(previous); return(current); }
/// <summary> /// Adds the specified key and value to the SplayTree<TKey, TValue>. /// </summary> /// <param name="key">The key of the element to add.</param> /// <param name="value">The value of the element to add.</param> public override void Add(TKey key, TValue value) { KeyValueTreeNode node = new KeyValueTreeNode(key, value); node = base.Add(node); this.Splay(node); }
private void Rebalance(KeyValueTreeNode node) { int size = 1, leftSize, rightSize; bool leftC; while (node.Parent != null) { leftC = node.Parent.Left == node; node = node.Parent; if (leftC) { rightSize = this.Size(node.Right); leftSize = size; } else { leftSize = this.Size(node.Left); rightSize = size; } size = leftSize + rightSize + 1; if (leftSize > alpha * size || rightSize > alpha * size) { this.Flatten(node, size); return; } } }
private int Size(KeyValueTreeNode node) { if (node == null) { return(0); } return(this.Size(node.Left) + this.Size(node.Right) + 1); }
protected override KeyValueTreeNode Add(KeyValueTreeNode node) { int depth = 0; this.Count++; if (this.root == null) { this.root = node; this.size++; return(node); } KeyValueTreeNode current = (KeyValueTreeNode)this.root; while (current != null) { depth++; int c = node.Key.CompareTo(current.Key); if (c < 0) { if (current.Left != null) { current = current.Left; } else { current.Left = node; break; } } else if (c > 0) { if (current.Right != null) { current = current.Right; } else { current.Right = node; break; } } else { return(null); } } this.size++; this.maxSize = Math.Max(size, maxSize); node.Parent = current; if (depth > Math.Log(this.size, 1 / alpha)) { this.Rebalance(node); } return(node); }
public ListItem CreateItem(KeyValueTreeNode node) { ListItem newitem = new ListItem(node.Value.Key, node.Value.Value, this._parent.ControlFormatting, this._parent); foreach (KeyValueTreeNode subnode in node.Nodes) { newitem.ItemsList.Add(this.CreateItem(subnode)); } return(newitem); }
private void Flatten(KeyValueTreeNode node, KeyValueTreeNode[] array, ref int index) { if (node == null) { return; } this.Flatten(node.Left, array, ref index); array[index++] = node; this.Flatten(node.Right, array, ref index); }
private void AddAt(KeyValueTreeNode node, KeyValueTreeNode root, bool left) { if (left) { root.Left = node; } else { root.Right = node; } node.Parent = root; }
private void InsertMedians(KeyValueTreeNode[] array, KeyValueTreeNode root, int start, int end, bool left) { if (end == start) { return; } int median = (end - start) / 2 + start; KeyValueTreeNode node = new KeyValueTreeNode(array[median].Key, array[median].Value); this.AddAt(node, root, left); this.InsertMedians(array, node, start, median, true); this.InsertMedians(array, node, median + 1, end, false); }
private void Flatten(KeyValueTreeNode node, int size) { if (node == null) { return; } KeyValueTreeNode[] nodes = new KeyValueTreeNode[size]; int index = 0; this.Flatten(node, nodes, ref index); this.InsertMedians(nodes, node.Parent); }
/// <summary> /// Adds the specified key and value to the RedBlackTree<TKey, TValue>. /// </summary> /// <param name="key">The key of the element to add.</param> /// <param name="value">The value of the element to add.</param> public virtual void Add(TKey key, TValue value) { if (key == null) { throw new ArgumentNullException("key"); } KeyValueTreeNode node = this.Add(new KeyValueTreeNode(key, value)); if (node == null) { throw new ArgumentException("An item with the same key already exists in the collection."); } }
private KeyValueTreeNode CreateKeyValueTreeNode(Result result) { KeyValueTreeNode newnode = new KeyValueTreeNode(); string value = result.KeyProperty.Value; string concatlist = this.ConcatenatePropertyValues(result.Properties, Separator); newnode.Value = new KeyValuePair <string, string>(value, concatlist); foreach (Result subresult in result.SubResults) { newnode.Nodes.Add(CreateKeyValueTreeNode(subresult)); } return(newnode); }
private void Splay(KeyValueTreeNode start) { bool leftC, leftPC; if (this.root == start || start == null) { return; } leftC = start == start.Parent.Left; if (start.Parent.Parent == null) { if (leftC) { this.RotateRight(start.Parent); } else { this.RotateLeft(start.Parent); } return; } leftPC = start.Parent == start.Parent.Parent.Left; if (leftC && leftPC) { this.RotateRight(start.Parent.Parent); this.RotateRight(start.Parent); } else if (!leftC && !leftPC) { this.RotateLeft(start.Parent.Parent); this.RotateLeft(start.Parent); } else if (!leftC && leftPC) { this.RotateLeft(start.Parent); this.RotateRight(start.Parent); } else { this.RotateRight(start.Parent); this.RotateLeft(start.Parent); } this.Splay(start); }
/// <summary> /// Gets the value associated with the specified key. /// </summary> /// <param name="key">The key of the value to get.</param> /// <param name="value">When this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter.</param> /// <returns>true if the BinarySearchTree<TKey, TValue> contains an element with the specified key; otherwise, false.</returns> public bool TryGetValue(TKey key, out TValue value) { if (key == null) { throw new ArgumentNullException("key"); } KeyValueTreeNode node = this.Find(key); if (node == null) { value = default(TValue); return(false); } value = node.Value; return(true); }
protected virtual KeyValueTreeNode Add(KeyValueTreeNode node) { this.Count++; if (this.root == null) { this.root = node; return(node); } KeyValueTreeNode current = (KeyValueTreeNode)this.root; while (current != null) { int c = node.Key.CompareTo(current.Key); if (c < 0) { if (current.Left != null) { current = current.Left; } else { current.Left = node; break; } } else if (c > 0) { if (current.Right != null) { current = current.Right; } else { current.Right = node; break; } } else { current.Count++; return(current); } } node.Parent = current; return(node); }
/// <summary> /// Removes the value with the specified key from the SplayTree<TKey, TValue>. /// </summary> /// <param name="key">The key of the element to remove.</param> /// <returns>true if the element is successfully found and removed; otherwise, false.</returns> public override bool Remove(TKey key) { KeyValueTreeNode current = (KeyValueTreeNode)this.root, previous = null; while (current != null) { previous = current; if (key.CompareTo(current.Key) < 0) { current = current.Left; } else if (key.CompareTo(current.Key) > 0) { current = current.Right; } else { break; } } if (current == null) { this.Splay(previous); return(false); } if (current.Left == null || current.Right == null) { this.Delete(current); } else { KeyValueTreeNode replace = (KeyValueTreeNode)this.Predecessor(current); if (replace == null) { replace = (KeyValueTreeNode)this.Successor(current); } current.Value = replace.Value; this.Delete(replace); } this.Count--; return(true); }
/// <summary> /// Removes the value with the specified key from the ScapeGoatTree<TKey, TValue>. /// </summary> /// <param name="key">The key of the element to remove.</param> /// <returns>true if the element is successfully found and removed; otherwise, false.</returns> public override bool Remove(TKey key) { KeyValueTreeNode current = this.Find(key); if (current == null) { return(false); } if (current.Count > 1) { current.Count--; this.Count--; return(true); } if (current.Left == null || current.Right == null) { this.Delete(current); } else { KeyValueTreeNode replace = (KeyValueTreeNode)this.Predecessor(current); if (replace == null) { replace = (KeyValueTreeNode)this.Successor(current); } current.Value = replace.Value; current.Count = replace.Count; this.Delete(replace); } this.Count--; this.size--; if (this.size <= this.maxSize / 2) { this.Flatten((KeyValueTreeNode)this.root, this.size); this.maxSize = this.size; } return(true); }
private void InsertMedians(KeyValueTreeNode[] array, KeyValueTreeNode root) { int median = array.Length / 2; KeyValueTreeNode medianNode = new KeyValueTreeNode(array[median].Key, array[median].Value); medianNode.Parent = root; if (root == null) { this.root = medianNode; } else if (medianNode.Key.CompareTo(root.Key) < 0) { root.Left = medianNode; } else { root.Right = medianNode; } this.InsertMedians(array, medianNode, 0, median, true); this.InsertMedians(array, medianNode, median + 1, array.Length, false); }
protected virtual KeyValueTreeNode Find(TKey key) { KeyValueTreeNode current = (KeyValueTreeNode)this.root; while (current != null) { int c = key.CompareTo(current.KeyValuePair.Key); if (c < 0) { current = current.Left; } else if (c > 0) { current = current.Right; } else { break; } } return(current); }
/// <summary> /// Removes the value with the specified key from the RedBlackTree<TKey, TValue>. /// </summary> /// <param name="key">The key of the element to remove.</param> /// <returns>true if the element is successfully found and removed; otherwise, false.</returns> public virtual bool Remove(TKey key) { KeyValueTreeNode current = this.Find(key); if (current == null) { return(false); } if (current.Count > 1) { current.Count--; this.Count--; return(true); } if (current.Left == null || current.Right == null) { this.Delete(current); } else { KeyValueTreeNode replace = (KeyValueTreeNode)this.Predecessor(current); if (replace == null) { replace = (KeyValueTreeNode)this.Successor(current); } current.KeyValuePair = replace.KeyValuePair; current.Count = replace.Count; this.Delete(replace); } this.Count--; return(true); }
public BSTEnumerator(BinarySearchTree <TKey, TValue> tree) { this.tree = tree; this.currentNode = null; }
public void Reset() { this.currentNode = null; }