public override void CreateChildren() { // Do nothing, if children are already created if (Children.Count >= 9) { return; } int children_layer = layerNum + 1; int children_size = getNodeSize() / 2; Dictionary <int, PartitionNode> added_children = new Dictionary <int, PartitionNode>(); for (int i = 0; i < 9; i++) { if (Children.ContainsKey(i) && Children[i] != null) { continue; } else { PartitionNode new_child = new PartitionNode(children_layer, children_size, NodeHelperFuncs.GetChildCenter(this, i), capacity); new_child.AddParent(this, NodeHelperFuncs.GetParentPosition(i)); Children.Add(i, new_child); added_children.Add(i, new_child); } } ConnectSharedChildren(added_children); }
private void ConnectSharedChildren(Dictionary <int, PartitionNode> added_children) { // For created children finds and connects other parents sharing the same children. Dictionary <int, PartitionNode> all_siblings = GetMySiblings(); foreach (KeyValuePair <int, PartitionNode> child in added_children) { foreach (int sibling_num in NodeHelperFuncs.getRelevantSiblings(child.Key)) { if (all_siblings.ContainsKey(sibling_num) && all_siblings[sibling_num] != null) { int sChildNum = NodeHelperFuncs.getSharedSiblingChildNumber(child.Key, sibling_num); all_siblings[sibling_num].SetSingleChild(sChildNum, child.Value); child.Value.AddParent(all_siblings[sibling_num], NodeHelperFuncs.GetParentPosition(sChildNum)); } } } }
public Dictionary <int, PartitionNode> GetMySiblings() { // To get my siblings, need to go 1 level up and look at all parents, and identify which position I am located with respect to // other children, then idenify a neighboring sibling and translate its number back relative to me. Dictionary <int, PartitionNode> siblings = new Dictionary <int, PartitionNode>(); if (IsRootNode()) { return(siblings); } foreach (KeyValuePair <int, PartitionNode> parent in Parents) { Dictionary <int, PartitionNode> par_children = parent.Value.getChildern(); foreach (KeyValuePair <int, PartitionNode> par_child in par_children) { if (par_child.Value.Equals(this)) { int sib_offset = NodeHelperFuncs.ComputeSiblingShiftForChildNum(par_child.Key); foreach (int sibling_num in NodeHelperFuncs.getChildSiblings(par_child.Key)) { if (par_children.ContainsKey(sibling_num)) { int my_sib_num = sib_offset + sibling_num; if (siblings.ContainsKey(my_sib_num)) { siblings[my_sib_num] = par_children[sibling_num]; } else { siblings.Add(my_sib_num, par_children[sibling_num]); } } } break; } } } return(siblings); }
public void RemoveChild(int child, bool check_siblings = true) { if (!Children.ContainsKey(child)) { return; } if (IsRootNode() || !check_siblings) { Children.Remove(child); return; } Dictionary <int, PartitionNode> siblings = GetMySiblings(); foreach (int sibling_num in NodeHelperFuncs.getRelevantSiblings(child)) { if (siblings.ContainsKey(sibling_num) && siblings[sibling_num] != null) { int sib_child_num = NodeHelperFuncs.getSharedSiblingChildNumber(child, sibling_num); siblings[sibling_num].RemoveChild(sib_child_num, false); } } Children.Remove(child); }