private void DrawLines( AnnotatedTreeNode node ) { if( node.IsInvisibleNode ) return; if( node.HasChildren ) { // Has children and Is Expanded for (var j = 0; j < node.Children.Count; j++) { if( !node.Children[ j ].IsInvisibleNode ) DrawLine( node.ChildrenConnectorPoint, node.Children[j].ParentConnectorPoint); // Children DrawLines( node.Children[j] ); } } }
public static int TreeMaxHeight( AnnotatedTreeNode node ) { if( node == null ) return 0; int maxHeight = 0; if( node.Children != null ) { for( int i=0; i < node.Children.Count; i++ ) { int branchHeight = TreeMaxHeight( node.Children[i] ); if( maxHeight < branchHeight ) maxHeight = branchHeight; } } return maxHeight + 1; }
public static AnnotatedTreeNode ToAnnotatedBinaryTree( BinaryNode node ) { var annotatedNode = new AnnotatedTreeNode() { Payload = node.Payload, IsCompareSource = node.IsCompareSource, IsCompareTarget = node.IsCompareTarget }; if( node.LeftNode != null || node.RightNode != null ) { annotatedNode.Children = new List<AnnotatedTreeNode>(); if( node.LeftNode != null ) { var leftNode = ToAnnotatedBinaryTree( node.LeftNode ); annotatedNode.Children.Add( leftNode ); } if( node.RightNode != null ) { var rightNode = ToAnnotatedBinaryTree( node.RightNode ); annotatedNode.Children.Add( rightNode ); } // Push an invisible node if( node.LeftNode != null && node.RightNode == null ) { annotatedNode.Children.Add( new AnnotatedTreeNode(){ IsInvisibleNode = true } ); } else if( node.LeftNode == null && node.RightNode != null ) { annotatedNode.Children.Insert( 0, new AnnotatedTreeNode() { IsInvisibleNode = true } ); } } return annotatedNode; }
public static AnnotatedTreeNode ToAnnotatedTree( TreeNode node ) { var annotatedNode = new AnnotatedTreeNode() { Payload = node.Payload, IsCompareSource = node.IsCompareSource, IsCompareTarget = node.IsCompareTarget }; if( node.Children != null ) { annotatedNode.Children = new List<AnnotatedTreeNode>(); for( int i=0; i < node.Children.Count; i++ ) { var child = ToAnnotatedTree( node.Children[ i ] ); annotatedNode.Children.Add( child ); } } return annotatedNode; }
private void PrepareNode( AnnotatedTreeNode node, int level = 0, AnnotatedTreeNode parentNode = null, AnnotatedTreeNode LeftSibling = null, Dictionary<int, AnnotatedTreeNode> rightLimits = null ) { if( rightLimits == null ) rightLimits = new Dictionary<int, AnnotatedTreeNode>(); node.Level = level; node.Parent = parentNode; node.LeftSibling = LeftSibling; if( node.HasChildren ) { for( var i = 0; i < node.Children.Count; i++ ) { AnnotatedTreeNode left = null; if( i == 0 && rightLimits.ContainsKey( level ) ) left = rightLimits[ level ]; if( i > 0 ) left = node.Children[ i - 1 ]; if( i == ( node.Children.Count - 1 ) ) { if( rightLimits.ContainsKey( level ) ) rightLimits.Remove( level ); rightLimits.Add( level, node.Children[ i ] ); } PrepareNode( node.Children[ i ], level + 1, node, left, rightLimits ); } } }
private void PerformLayout( AnnotatedTreeNode node ) { double nodeHeight = 40; double nodeWidth = 40; double nodeMarginLeft = 10; double nodeMarginTop = 20; double nodeLeft = 0; // defaultValue // Before Layout this Node, Layout its children if( node.HasChildren ) { for (var i = 0; i < node.Children.Count; i++) { PerformLayout(node.Children[i]); } } if( node.HasChildren ) // If Has Children and Is Expanded { // My left is in the center of my children var childrenWidth = (node.Children[node.Children.Count-1].Left + node.Children[node.Children.Count-1].Width) - node.Children[0].Left; nodeLeft = (node.Children[0].Left + (childrenWidth / 2)) - (nodeWidth / 2); // Is my left over my left node? // Move it to the right if(node.LeftSibling != null &&((node.LeftSibling.Left+node.LeftSibling.Width+nodeMarginLeft)>nodeLeft)) { double newLeft = node.LeftSibling.Left + node.LeftSibling.Width + nodeMarginLeft; double diff = newLeft - nodeLeft; /// Move also my children MoveRigth(node.Children, diff); nodeLeft = newLeft; } } else { // My left is next to my left sibling if (node.LeftSibling != null ) nodeLeft = node.LeftSibling.Left + node.LeftSibling.Width + nodeMarginLeft; } node.Left = nodeLeft; // The top depends only on the level node.Top = (nodeMarginTop * (node.Level + 1)) + (nodeHeight * (node.Level + 1)); // Size is constant node.Height = nodeHeight; node.Width = nodeWidth; // Calculate Connector Points // Child: Where the lines get out from to connect this node with its children var pointX = nodeLeft + (nodeWidth / 2); var pointY = node.Top + nodeHeight; node.ChildrenConnectorPoint = new Point{ X= pointX, Y= pointY }; // Parent: Where the line that connect this node with its parent end pointX = nodeLeft + (nodeWidth / 2); pointY = node.Top; node.ParentConnectorPoint = new Point { X = pointX, Y = pointY }; }
private void DrawNode( AnnotatedTreeNode node ) { if( node.IsInvisibleNode ) return; var ellipse = new Ellipse(); ellipse.Fill = node.IsCompareSource ? Brushes.Green : ( node.IsCompareTarget ? Brushes.Yellow : Brushes.White ); ellipse.StrokeThickness = 2; ellipse.Stroke = Brushes.Black; ellipse.Width = node.Width; ellipse.Height = node.Height; var text = new TextBlock(); text.Text = string.Format( "{0}", node.Payload.Value1 ); text.TextAlignment = TextAlignment.Center; text.VerticalAlignment = VerticalAlignment.Center; var grid = new Grid(); grid.Width = node.Width; grid.Height = node.Height; grid.Children.Add( ellipse ); grid.Children.Add( text ); Canvas.SetTop( grid, node.Top ); Canvas.SetLeft( grid, node.Left ); DrawingArea.Children.Add( grid ); // Draw children if( node.HasChildren ) // Has Children and is Expanded { for (var i = 0; i < node.Children.Count; i++) { DrawNode( node.Children[i] ); } } }
public static int TreeMaxWidth( AnnotatedTreeNode node ) { if( node.Children == null ) return 1; int branchWidth = 0; for( int i=0; i < node.Children.Count; i++ ) { branchWidth += TreeMaxWidth( node.Children[ i ] ); } return branchWidth; }