public void addToSelectedNodes(NodeViewModel n) { selectedNodes.Add(n); n.BorderColor = Brushes.DarkBlue; n.BorderThickness = 4; }
public AddLineCommand(ObservableCollection<LineViewModel> _lines, LineViewModel _line, NodeViewModel _fromNode, NodeViewModel _toNode) { lines = _lines; line = _line; fromNode = _fromNode; toNode = _toNode; }
public LineViewModel(NodeViewModel nvm) { this.nvm = nvm; Lines = new ObservableCollection<Line>() { new Line() {From = nvm.BSTNodes[0], To = nvm.BSTNodes[1]} }; }
public Tree(List<NodeViewModel> selNodes, ObservableCollection<LineViewModel> lines) { // Tjek at selectedNodes ikke er 0 før new Tree(selectedNodes) bliver kaldt Lines = lines; if (!(selNodes.Count == 0)) { nodes = selNodes; root = setRoot(); } }
public InsertNodeInTreeCommand(Tree _tree, ObservableCollection<NodeViewModel> _nodes, List<NodeViewModel> _selectedNodes, NodeViewModel _newNode, ObservableCollection<LineViewModel> _lines) { tree = _tree; nodes = _nodes; lines = _lines; newNode = _newNode; foreach(NodeViewModel n in _selectedNodes) { selNodes.Add(n); } tree.nodes = selNodes; }
/// <summary> /// Initializes a new instance of the MainViewModel class. /// </summary> public MainViewModel() { NodeViewModel = new NodeViewModel(LineViewModel); LineViewModel = new LineViewModel(NodeViewModel); ////if (IsInDesignMode) ////{ //// // Code runs in Blend --> create design time data. ////} ////else ////{ //// // Code runs "for real" ////} }
public void addNeighbourAndLineAndUpdatePosition(NodeViewModel n1, NodeViewModel n2) { n1.addNeighbour(n2); Lines.Add(new LineViewModel(new Line()) { From = n1, To = n2 }); if(int.Parse(n1.Key) > int.Parse(n2.Key)) { n1.X = n2.X + X_OFFSET; n1.Y = n2.Y + Y_OFFSET; } else { n1.X = n2.X - X_OFFSET; n1.Y = n2.Y + Y_OFFSET; } }
public void addNeighbourAndLineAndUpdatePosition(NodeViewModel n1, NodeViewModel n2) { n1.addNeighbour(n2); LineViewModel temp = new LineViewModel(new Line()) { From = n1, To = n2 }; Lines.Add(temp); addedLinesAutoBalance.Add(temp); if(int.Parse(n1.TxtOne) > int.Parse(n2.TxtOne)) { n1.X = n2.X + X_OFFSET; n1.Y = n2.Y + Y_OFFSET; } else { n1.X = n2.X - X_OFFSET; n1.Y = n2.Y + Y_OFFSET; } }
public Tree(List<NodeViewModel> selNodes) { // Tjek at selectedNodes ikke er 0 før new Tree(selectedNodes) bliver kaldt if (!(selNodes.Count == 0)) { nodes = selNodes; unmarkNodes(); root = setRoot(); // Sort test works //foreach (NodeViewModel n in nodes) //{ // Console.WriteLine(n.Key); //} //Console.WriteLine("Oh yea"); //nodes.Sort((x, y) => int.Parse(x.Key).CompareTo(int.Parse(y.Key))); //foreach (NodeViewModel n in nodes) //{ // Console.WriteLine(n.Key); //} } }
public void Execute() { tree.nodes.Clear(); tree.nodes.Add(selNodes.ElementAt(0)); foreach (NodeViewModel n in nodes) { prevNodes.Add(new Tuple<string, int, Brush, Brush, Brush>(n.TxtOne, n.ID, n.Color, n.ColorOfText, n.PreColor)); } removeThis = tree.remove(selNodes.ElementAt(0)); foreach (LineViewModel l in lines) { if ((l.From == removeThis || l.To == removeThis) && !removedLines.Contains(l)) { removedLines.Add(l); l.From.removeNeighbour(l.To); } } nodes.Remove(removeThis); foreach (LineViewModel l in removedLines) { lines.Remove(l); } }
public ChangeColorTextCommand(NodeViewModel node, Brush color, Brush preColor) { _node = node; _preColor = preColor; _color = color; }
public AddNodeCommand(ObservableCollection<NodeViewModel> _nodes, NodeViewModel _node) { nodes = _nodes; node = _node; }
// Antaget at træet er korrekt, men virker muligvis stadig alligevel public void insertBST(NodeViewModel newNode, NodeViewModel nvm) { NodeViewModel[] children = getChildren(nvm); //Zero children if (children[0] == null) { addNeighbourAndLineAndUpdatePosition(newNode, nvm); } // One child else if (children[1] == null) { // If both the new node and the one child needs to be on the same side if ((int.Parse(newNode.TxtOne) <= int.Parse(nvm.TxtOne) && int.Parse(children[0].TxtOne) <= int.Parse(nvm.TxtOne)) || (int.Parse(newNode.TxtOne) > int.Parse(nvm.TxtOne) && int.Parse(children[0].TxtOne) > int.Parse(nvm.TxtOne))) { insertBST(newNode, children[0]); } else { addNeighbourAndLineAndUpdatePosition(newNode, nvm); } } // Two children else { if (int.Parse(newNode.TxtOne) <= int.Parse(nvm.TxtOne)) { insertBST(newNode, children[0]); } else { insertBST(newNode, children[1]); } } }
private bool pushAncenstors(NodeViewModel orig) { NodeViewModel parent = getParent(); if (parent == null) { return false; } if (parent.X + X_ONSET - 1 > orig.X && parent.X - X_ONSET + 1 < orig.X) { if (X < parent.X) { getRoot().pushTree(LEFT, parent.X, orig, X_ONSET + (orig.X - parent.X)); orig.move(LEFT, X_ONSET + (orig.X - parent.X)); } else { getRoot().pushTree(RIGHT, parent.X, orig, X_ONSET + (parent.X - orig.X)); orig.move(RIGHT, X_ONSET + (parent.X - orig.X)); } } return parent.pushAncenstors(orig); }
//returns true if tree is valid after remove public void removeNeighbour(NodeViewModel NVM) { //ViewModel neighbours.Remove(NVM); NVM.neighbours.Remove(this); //Model Node.neighbours.Remove(NVM.Node); NVM.Node.neighbours.Remove(Node); }
public NodeViewModel[] getChildren() { NodeViewModel[] children = new NodeViewModel[3]; int i = 0; foreach (NodeViewModel neighbour in neighbours) if (neighbour.Y > Y) { children[i] = neighbour; i++; } if (i == 2 && children[0].X > children[1].X) { NodeViewModel temp = children[0]; children[0] = children[1]; children[1] = temp; } return children; }
public void markNeighbours(NodeViewModel nvm) { foreach(NodeViewModel n in nvm.neighbours) { if(!n.hasBeenFound && nodes.Contains(n)) { n.hasBeenFound = true; markNeighbours(n); } } }
public NodeViewModel removeBST(NodeViewModel _nvm) { NodeViewModel removeNode = null; removeNode = _nvm; NodeViewModel parent = removeNode.getParent(); NodeViewModel[] children = removeNode.getChildren(); NodeViewModel replacementNode; if (children[LEFT] == null) { return removeNode; } else { replacementNode = children[LEFT].getMostRightNode(); for(;;) { children = replacementNode.getChildren(); removeNode.TxtOne = replacementNode.TxtOne; removeNode.Color = replacementNode.Color; removeNode.ColorOfText = replacementNode.ColorOfText; removeNode.PreColor = replacementNode.PreColor; if (children[LEFT] == null) return replacementNode; else { removeNode = replacementNode; replacementNode = children[LEFT].getMostRightNode(); } } } }
public NodeViewModel remove(NodeViewModel _nvm) { return removeBST(_nvm); }
public bool checkChildrenKey(NodeViewModel nvm) { NodeViewModel[] children = getChildren(nvm); foreach (NodeViewModel child in children) { if (child != null) { if (!checkChildrenKey(child)) return false; } } if (!checkCorrectParentToRoot(nvm)) return false; if (children[0] == null) return true; if (children[1] == null) { if (int.Parse(children[0].TxtOne) <= int.Parse(nvm.TxtOne) && children[0].X < nvm.X) return true; if (int.Parse(children[0].TxtOne) > int.Parse(nvm.TxtOne) && children[0].X > nvm.X) return true; return false; } // getChildren already switches such that children[0] is further to the left than children[1]. // Therefore it is enough to check that children[0].TxtOne <= nvm.TxtOne and not also > return (int.Parse(children[0].TxtOne) <= int.Parse(nvm.TxtOne) && int.Parse(children[1].TxtOne) > int.Parse(nvm.TxtOne) && children[0].X < nvm.X && children[1].X > nvm.X); }
public List<LineViewModel> tAutoBalance() { nodes.Sort((x, y) => int.Parse(x.TxtOne).CompareTo(int.Parse(y.TxtOne))); removeLinesAndNeighbours(); root = nodes.ElementAt(nodes.Count / 2); hasBeenInsertedList.Add(root); subFromIndex.Add((nodes.Count / 2 + 1) / 2); balancedInsert(nodes.Count / 2,0); // Inserts the rest of the nodes (they will all be leaves) foreach (NodeViewModel n in nodes) { if (!hasBeenInsertedList.Contains(n)) { insertBST(n, root); } } makePretty(false); return addedLinesAutoBalance; }
public bool checkCorrectParentToRoot(NodeViewModel nvm) { NodeViewModel grandParent = nvm.getParent(); NodeViewModel parent = nvm.getParent(); while (grandParent != root) { if (grandParent == null || grandParent.getParent() == null) break; grandParent = grandParent.getParent(); if (parent.X < grandParent.X && int.Parse(nvm.TxtOne) > int.Parse(grandParent.TxtOne) || parent.X > grandParent.X && int.Parse(nvm.TxtOne) <= int.Parse(grandParent.TxtOne)) return false; parent = parent.getParent(); } return true; }
public void addNeighbour(NodeViewModel NVM) { // ViewModel neighbours.Add(NVM); NVM.neighbours.Add(this); // Model Node.neighbours.Add(NVM.Node); NVM.Node.neighbours.Add(Node); }
public void generateTreeFromOneNode(NodeViewModel nvm) { foreach(NodeViewModel n in nvm.neighbours) { if(!wholeTree.Contains(n)) { wholeTree.Add(n); generateTreeFromOneNode(n); } } }
public bool isChild(NodeViewModel NVM) { return NVM.Y > Y; }
public NodeViewModel[] getChildren(NodeViewModel nvm) { if (nvm != null) { NodeViewModel[] children = new NodeViewModel[2]; int i = 0; foreach (NodeViewModel n in nvm.neighbours) { if (n.Y > nvm.Y) { children[i] = n; i++; } } if (i == 2 && children[0].X > children[1].X) { NodeViewModel temp = children[0]; children[0] = children[1]; children[1] = temp; } return children; } return null; }
private bool moveOffset(NodeViewModel child, int direction) { if (LEFT == direction) child.X = X - X_OFFSET; else child.X = X + X_OFFSET; child.Y = Y + Y_OFFSET; return true; }
public bool hasOneParentAndLessThanThreeChildren(NodeViewModel nvm) { int pCount = 0; foreach (NodeViewModel n in nvm.neighbours) { if (n.Y < nvm.Y) pCount++; } return pCount == 1 && nvm.neighbours.Count <= 3; }
private bool pushTree(int direction, double threshold, NodeViewModel orig, double offset) { NodeViewModel[] children = getChildren(); if (this == orig) return true; if (direction == LEFT && X < threshold) move(LEFT, offset); else if (direction == RIGHT && threshold < X) move(RIGHT, offset); int i = 0; //while (children[i] != null) foreach(NodeViewModel child in children) if(child != null) { children[i].pushTree(direction, threshold, orig, offset); i++; } return true; }
public void insert(NodeViewModel newNode) { insertBST(newNode, root); }