public void CollectionInitializerImplementation() { // Arrange var tree = new ValueTreeNode <string>("Operating Systems") { new ValueTreeNode <string>("Linux") { new ValueTreeNode <string>("Ubuntu"), new ValueTreeNode <string>("Fedora"), new ValueTreeNode <string>("CentOS") }, new ValueTreeNode <string>("Windows") { new ValueTreeNode <string>("Windows 7"), new ValueTreeNode <string>("Windows XP") { new ValueTreeNode <string>("Home Edition"), new ValueTreeNode <string>("Professional Edition") } } }; // Act var xp = tree[1][1]; var root = xp.Parent.Parent; // Assert Assert.AreEqual("Windows XP", xp.Value); Assert.AreEqual(tree, root); }
/// <summary> /// Removes the first occurrence of a specific object from the BinarySearchTree<T>. /// </summary> /// <param name="item">The object to be removed.</param> /// <returns>true if item was successfully removed from the BinarySearchTree<T>; otherwise, false.</returns> public virtual bool Remove(T item) { ValueTreeNode current = this.Find(item); 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 { ValueTreeNode replace = (ValueTreeNode)this.Predecessor(current); if (replace == null) { replace = (ValueTreeNode)this.Successor(current); } current.Value = replace.Value; current.Count = replace.Count; this.Delete(replace); } this.Count--; return(true); }
public void DynamicImplementation() { // Arrange var tree = new ValueTreeNode <string>("Operating Systems"); // Act tree.Add( new ValueTreeNode <string>("Linux", new ValueTreeNode <string>("Ubuntu"), new ValueTreeNode <string>("Fedora"), new ValueTreeNode <string>("CentOS") )); tree.Add( new ValueTreeNode <string>("Windows", new ValueTreeNode <string>("Windows 7"), new ValueTreeNode <string>("Windows XP", new ValueTreeNode <string>("Home Edition"), new ValueTreeNode <string>("Professional Edition") ))); var xp = tree[1][1]; var root = xp.Parent.Parent; Tools.PrintAllNodes(tree, 0); // Assert Assert.AreEqual("Windows XP", xp.Value); Assert.AreEqual(tree, root); }
/// <summary> /// Adds the specified value to the SplayTree<T>. /// </summary> /// <param name="item">The value to add.</param> public override void Add(T item) { ValueTreeNode node = new ValueTreeNode(item); node = base.Add(node); this.Splay(node); }
private void Rebalance(ValueTreeNode 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; } } }
public bool MoveNext() { ValueTreeNode successor; if (tree.root == null) { return(false); } if (currentNode == null) { currentNode = (ValueTreeNode)tree.root; while (currentNode.Left != null) { currentNode = (ValueTreeNode)currentNode.Left; } return(true); } if (currentNode.Count > currentCount) { currentCount++; return(true); } if ((successor = (ValueTreeNode)tree.Successor(currentNode)) != null) { currentNode = successor; currentCount = 1; return(true); } return(false); }
protected override ValueTreeNode Find(T item) { ValueTreeNode current = (ValueTreeNode)this.root, previous = null; while (current != null) { int c = item.CompareTo(current.Value); previous = current; if (c < 0) { current = current.Left; } else if (c > 0) { current = current.Right; } else { break; } } this.Splay(previous); return(current); }
private int Size(ValueTreeNode node) { if (node == null) { return(0); } return(this.Size(node.Left) + this.Size(node.Right) + 1); }
protected override ValueTreeNode Add(ValueTreeNode node) { int depth = 0; this.Count++; if (this.root == null) { this.root = node; this.size++; return(node); } ValueTreeNode current = (ValueTreeNode)this.root; while (current != null) { depth++; int c = node.Value.CompareTo(current.Value); 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); } } this.size++; this.maxSize = Math.Max(size, maxSize); node.Parent = current; if (depth > Math.Log(this.size, 1 / alpha)) { this.Rebalance(node); } return(node); }
private void Flatten(ValueTreeNode node, ValueTreeNode[] 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); }
/// <summary> /// Removes the first occurrence of a specific object from the SplayTree<T>. /// </summary> /// <param name="item">The object to be removed.</param> /// <returns>true if item was successfully removed from the SplayTree<T>; otherwise, false.</returns> public override bool Remove(T item) { ValueTreeNode current = (ValueTreeNode)this.root, previous = null; while (current != null) { previous = current; if (item.CompareTo(current.Value) < 0) { current = current.Left; } else if (item.CompareTo(current.Value) > 0) { current = current.Right; } else { break; } } if (current == null) { this.Splay(previous); return(false); } if (current.Count > 1) { this.Splay(current); current.Count--; return(true); } if (current.Left == null || current.Right == null) { this.Delete(current); } else { ValueTreeNode replace = (ValueTreeNode)this.Predecessor(current); if (replace == null) { replace = (ValueTreeNode)this.Successor(current); } current.Value = replace.Value; current.Count = replace.Count; this.Delete(replace); } this.Count--; return(true); }
private void AddAt(ValueTreeNode node, ValueTreeNode root, bool left) { if (left) { root.Left = node; } else { root.Right = node; } node.Parent = root; }
private void Flatten(ValueTreeNode node, int size) { if (node == null) { return; } ValueTreeNode[] nodes = new ValueTreeNode[size]; int index = 0; this.Flatten(node, nodes, ref index); this.InsertMedians(nodes, node.Parent); }
internal static void PrintAllNodes <T>(ValueTreeNode <T> node, uint level) { for (var i = 0; i < level; i++) { Console.Write("-"); } Console.WriteLine(node.Value); foreach (var child in node) { PrintAllNodes(child, level + 1); } }
private void InsertMedians(ValueTreeNode[] array, ValueTreeNode root, int start, int end, bool left) { if (end == start) { return; } int median = (end - start) / 2 + start; ValueTreeNode node = new ValueTreeNode(array[median].Value); node.Count = array[median].Count; this.AddAt(node, root, left); this.InsertMedians(array, node, start, median, true); this.InsertMedians(array, node, median + 1, end, false); }
private void Splay(ValueTreeNode 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); }
protected virtual ValueTreeNode Add(ValueTreeNode node) { this.Count++; if (this.root == null) { this.root = node; return(node); } ValueTreeNode current = (ValueTreeNode)this.root; while (current != null) { int c = node.Value.CompareTo(current.Value); 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 specified value from the ScapeGoatTree<T>. /// </summary> /// <param name="item">The value to remove.</param> /// <returns>true if item was successfully removed from the ScapeGoatTree<T>; otherwise, false.</returns> public override bool Remove(T item) { ValueTreeNode current = this.Find(item); 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 { ValueTreeNode replace = (ValueTreeNode)this.Predecessor(current); if (replace == null) { replace = (ValueTreeNode)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((ValueTreeNode)this.root, this.size); this.maxSize = this.size; } return(true); }
/// <summary> /// Override lookup filter data tree. /// /// Requirement summary: /// If logged user has role VP --> show all region /// If logged user has role Manager --> show some regions ("South America", "North America") /// If logged user has role Employy --> show only one region ("South America") /// </summary> /// <param name="fieldInfo"></param> /// <returns></returns> public override List <ValueTreeNode> OnLoadFilterDataTree(QuerySourceFieldInfo fieldInfo) { var result = new List <ValueTreeNode>(); if (fieldInfo.QuerySourceName == "OrderDetailsByRegion" && fieldInfo.Name == "CountryRegionName" && (HttpContext.Current.User.IsInRole("Manager") || HttpContext.Current.User.IsInRole("Employee"))) { //Node [All] and [Blank] are required for UI to render. var rootNode = new ValueTreeNode { Text = "[All]", Value = "[All]" }; rootNode.Nodes = new List <ValueTreeNode>(); //rootNode.Nodes.Add(new ValueTreeNode { Text = "[Blank]", Value = "[Blank]" }); if (HttpContext.Current.User.IsInRole("Manager")) { rootNode.Nodes.Add(new ValueTreeNode { Text = "South America", Value = "South America" }); rootNode.Nodes.Add(new ValueTreeNode { Text = "North America", Value = "North America" }); } if (HttpContext.Current.User.IsInRole("Employee")) { rootNode.Nodes.Add(new ValueTreeNode { Text = "South America", Value = "South America" }); } //else //{ // rootNode.Nodes.Add(new ValueTreeNode { Text = "Europe", Value = "Europe" }); // rootNode.Nodes.Add(new ValueTreeNode { Text = "South America", Value = "South America" }); // rootNode.Nodes.Add(new ValueTreeNode { Text = "North America", Value = "North America" }); //} result.Add(rootNode); } return(result); }
private void InsertMedians(ValueTreeNode[] array, ValueTreeNode root) { int median = array.Length / 2; ValueTreeNode medianNode = new ValueTreeNode(array[median].Value); medianNode.Count = array[median].Count; medianNode.Parent = root; if (root == null) { this.root = medianNode; } else if (medianNode.Value.CompareTo(root.Value) < 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 ValueTreeNode Find(T item) { ValueTreeNode current = (ValueTreeNode)this.root; while (current != null) { int c = item.CompareTo(current.Value); if (c < 0) { current = current.Left; } else if (c > 0) { current = current.Right; } else { break; } } return(current); }
public void Reset() { this.currentNode = null; this.currentCount = 1; }
public BSTEnumerator(BinarySearchTree <T> tree) { this.tree = tree; this.currentNode = null; this.currentCount = 1; }