public IIterator Next() { while (nodesStack.Count > 0) { if (currentNode != null && currentNode != currentBackupNode) //当前结点不空并且没有被访问过 { nodesStack.Push(currentNode); currentNode = currentNode.LeftChild; } else { currentNode = nodesStack.Pop() as IBiTreeNode; //当前currentNode保存最左边结点 if (currentNode.RightChild != null && currentNode.RightChild != currentBackupNode) //当前结点的右结点不空并且没有被访问过 { nodesStack.Push(currentNode); currentNode = currentNode.RightChild; } else { currentBackupNode = currentNode; break; } } } return(this); }
public IIterator Next() { while(nodesStack.Count > 0) { if(currentNode != null && currentNode != currentBackupNode) //当前结点不空并且没有被访问过 { nodesStack.Push(currentNode); currentNode = currentNode.LeftChild; } else { currentNode = nodesStack.Pop() as IBiTreeNode; //当前currentNode保存最左边结点 if(currentNode.RightChild != null && currentNode.RightChild != currentBackupNode) //当前结点的右结点不空并且没有被访问过 { nodesStack.Push(currentNode); currentNode = currentNode.RightChild; } else { currentBackupNode = currentNode; break; } } } return this; }
public IIterator First() { nodesStack.Clear(); currentBackupNode = null; //注意,这里不能忘了,第一次不用保存刚刚被遍历的结点 currentNode = rootNode; //先指向根结点 nodesStack.Push(currentNode); while (nodesStack.Count > 0) { if (currentNode != null && currentNode != currentBackupNode) //当前结点不空并且没有被访问过 { nodesStack.Push(currentNode); currentNode = currentNode.LeftChild; } else { currentNode = nodesStack.Pop() as IBiTreeNode; //当前currentNode保存最左边结点 if (currentNode.RightChild != null && currentNode.RightChild != currentBackupNode) //当前结点的右结点不空并且没有被访问过 { nodesStack.Push(currentNode); currentNode = currentNode.RightChild; } else { currentBackupNode = currentNode; break; } } } return(this); }
public IIterator First() { nodesStack.Clear(); currentNode = rootNode; return(this); }
public IIterator First() { nodesStack.Clear(); currentBackupNode = null; //注意,这里不能忘了,第一次不用保存刚刚被遍历的结点 currentNode = rootNode; //先指向根结点 nodesStack.Push(currentNode); while(nodesStack.Count > 0) { if(currentNode != null && currentNode != currentBackupNode) //当前结点不空并且没有被访问过 { nodesStack.Push(currentNode); currentNode = currentNode.LeftChild; } else { currentNode = nodesStack.Pop() as IBiTreeNode; //当前currentNode保存最左边结点 if(currentNode.RightChild != null && currentNode.RightChild != currentBackupNode) //当前结点的右结点不空并且没有被访问过 { nodesStack.Push(currentNode); currentNode = currentNode.RightChild; } else { currentBackupNode = currentNode; break; } } } return this; }
public IIterator First() { nodesStack.Clear(); currentNode = rootNode; return this; }
BiTreeLineNode CreateLineNode(IBiTreeNode treeNode1, IBiTreeNode treeNode2) { int x1, y1, x2, y2; if (treeNode1 == treeNode2) //如果是根结点,就不画出来 { return(new BiTreeLineNode(1, 1, 1, 1, 1, Color.Transparent)); } x1 = treeNode1.Bounds.X + treeNode1.Bounds.Width / 2; x2 = treeNode2.Bounds.X + treeNode2.Bounds.Width / 2; y1 = treeNode1.Bounds.Y + treeNode1.Bounds.Height; y2 = treeNode2.Bounds.Y; return(new BiTreeLineNode(x1, y1, x2, y2, 2, lineColor)); }
public IIterator Next() { currentNode = currentNode.RightChild; while (currentNode != null || nodesStack.Count > 0) { if (currentNode != null) { nodesStack.Push(currentNode); currentNode = currentNode.LeftChild; } else { currentNode = nodesStack.Pop() as IBiTreeNode; break; } } return(this); }
public IIterator Next() { currentNode = currentNode.RightChild; while(currentNode != null || nodesStack.Count > 0) { if(currentNode != null) { nodesStack.Push(currentNode); currentNode = currentNode.LeftChild; } else { currentNode = nodesStack.Pop() as IBiTreeNode; break; } } return this; }
public BiTreeInOrderIterator(IBiTreeNode rootNode) { this.rootNode = rootNode; }
public void PopupToCurrentNode() { currentNode = (IBiTreeNode)nodesStack.Pop(); }
public void PopupToCurrentNode() { currentNode = (IBiTreeNode)nodesStack.Pop(); }
public void SetToRightChild() { currentNode = currentNode.RightChild; }
public void SetToLeftChild() { currentNode = currentNode.LeftChild; }
public void SetToNewNode(IBiTreeNode newNode) { currentNode = newNode; }
public BiTreePreOrderIterator(IBiTreeNode rootNode) { this.rootNode = rootNode; }
public void SetToRootNode() { currentNode = rootNode; }
public void SetToLeftChild() { currentNode = currentNode.LeftChild; }
public void GenerateTree1(int diameter, Color backColor) { leftSpan = 20; topSpan = 5; colWidth = 30; rowHeight = 30; AssignArrayValues(); //先创建根结点(在二维数组中的位置为[0,7]) rootNode = CreateBiTreeNode(0, 7, diameter, backColor); IBiTreeNode currentNode; IBiTreeNode parentNode = rootNode; IBiTreeNode tempParentNode0 = rootNode; //用来暂时保存根结点 rootLineNode = CreateLineNode(rootNode, rootNode); IBiTreeNode currentLineNode; IBiTreeNode parentLineNode = rootLineNode; IBiTreeNode tempParentLineNode0 = rootLineNode; //用来暂时保存根结点 if (flagArray[2, 3] == true) { currentNode = CreateBiTreeNode(2, 3, diameter, backColor); currentLineNode = CreateLineNode(parentNode, currentNode); parentNode.LeftChild = currentNode; parentNode = currentNode; IBiTreeNode tempParentNode1 = currentNode; parentLineNode.LeftChild = currentLineNode; parentLineNode = currentLineNode; IBiTreeNode tempParentLineNode1 = currentLineNode; //同样用来保存某个结点 if (flagArray[4, 1] == true) { currentNode = CreateBiTreeNode(4, 1, diameter, backColor); currentLineNode = CreateLineNode(parentNode, currentNode); parentNode.LeftChild = currentNode; parentNode = currentNode; parentLineNode.LeftChild = currentLineNode; parentLineNode = currentLineNode; if (flagArray[6, 0] == true) { currentNode = CreateBiTreeNode(6, 0, diameter, backColor); currentLineNode = CreateLineNode(parentNode, currentNode); parentNode.LeftChild = currentNode; parentLineNode.LeftChild = currentLineNode; } if (flagArray[6, 2] == true) { currentNode = CreateBiTreeNode(6, 2, diameter, backColor); currentLineNode = CreateLineNode(parentNode, currentNode); parentNode.RightChild = currentNode; parentLineNode.RightChild = currentLineNode; } } if (flagArray[4, 5] == true) { currentNode = CreateBiTreeNode(4, 5, diameter, backColor); parentNode = tempParentNode1; parentLineNode = tempParentLineNode1; currentLineNode = CreateLineNode(parentNode, currentNode); parentNode.RightChild = currentNode; parentNode = currentNode; parentLineNode.RightChild = currentLineNode; parentLineNode = currentLineNode; if (flagArray[6, 4] == true) { currentNode = CreateBiTreeNode(6, 4, diameter, backColor); currentLineNode = CreateLineNode(parentNode, currentNode); parentNode.LeftChild = currentNode; parentLineNode.LeftChild = currentLineNode; } if (flagArray[6, 6] == true) { currentNode = CreateBiTreeNode(6, 6, diameter, backColor); currentLineNode = CreateLineNode(parentNode, currentNode); parentNode.RightChild = currentNode; parentLineNode.RightChild = currentLineNode; } } } if (flagArray[2, 11] == true) { currentNode = CreateBiTreeNode(2, 11, diameter, backColor); parentNode = tempParentNode0; parentLineNode = tempParentLineNode0; currentLineNode = CreateLineNode(parentNode, currentNode); parentNode.RightChild = currentNode; parentLineNode.RightChild = currentLineNode; parentNode = currentNode; parentLineNode = currentLineNode; IBiTreeNode tempParentNode2 = currentNode; //同样用来保存某个结点 IBiTreeNode tempParentLineNode2 = currentLineNode; //同样用来保存某个结点 if (flagArray[4, 9] == true) { currentNode = CreateBiTreeNode(4, 9, diameter, backColor); currentLineNode = CreateLineNode(parentNode, currentNode); parentNode.LeftChild = currentNode; parentLineNode.LeftChild = currentLineNode; parentNode = currentNode; parentLineNode = currentLineNode; if (flagArray[6, 8] == true) { currentNode = CreateBiTreeNode(6, 8, diameter, backColor); currentLineNode = CreateLineNode(parentNode, currentNode); parentNode.LeftChild = currentNode; parentLineNode.LeftChild = currentLineNode; } if (flagArray[6, 10] == true) { currentNode = CreateBiTreeNode(6, 10, diameter, backColor); currentLineNode = CreateLineNode(parentNode, currentNode); parentNode.RightChild = currentNode; parentLineNode.RightChild = currentLineNode; } } if (flagArray[4, 13] == true) { currentNode = CreateBiTreeNode(4, 13, diameter, backColor); parentNode = tempParentNode2; parentLineNode = tempParentLineNode2; currentLineNode = CreateLineNode(parentNode, currentNode); parentNode.RightChild = currentNode; parentNode = currentNode; parentLineNode.RightChild = currentLineNode; parentLineNode = currentLineNode; if (flagArray[6, 12] == true) { currentNode = CreateBiTreeNode(6, 12, diameter, backColor); currentLineNode = CreateLineNode(parentNode, currentNode); parentNode.LeftChild = currentNode; parentLineNode.LeftChild = currentLineNode; } if (flagArray[6, 14] == true) { currentNode = CreateBiTreeNode(6, 14, diameter, backColor); currentLineNode = CreateLineNode(parentNode, currentNode); parentNode.RightChild = currentNode; parentLineNode.RightChild = currentLineNode; } } } for (int i = 0; i < 7; i++) { for (int j = 0; j < 15; j++) { Console.Write(charArray[i, j] + " "); } Console.WriteLine(); } }
public void SetToNewNode(IBiTreeNode newNode) { currentNode = newNode; }
public void SetToRootNode() { currentNode = rootNode; }
BiTreeLineNode CreateLineNode(IBiTreeNode treeNode1,IBiTreeNode treeNode2) { int x1,y1,x2,y2; if(treeNode1 == treeNode2) //如果是根结点,就不画出来 { return new BiTreeLineNode(1,1,1,1,1,Color.Transparent); } x1 = treeNode1.Bounds.X + treeNode1.Bounds.Width / 2; x2 = treeNode2.Bounds.X + treeNode2.Bounds.Width / 2; y1 = treeNode1.Bounds.Y + treeNode1.Bounds.Height; y2 = treeNode2.Bounds.Y; return new BiTreeLineNode(x1,y1,x2,y2,2,lineColor); }
public void SetToRightChild() { currentNode = currentNode.RightChild; }
public void GenerateTree1(int diameter,Color backColor) { leftSpan = 20; topSpan = 5; colWidth = 30; rowHeight = 30; AssignArrayValues(); //先创建根结点(在二维数组中的位置为[0,7]) rootNode = CreateBiTreeNode(0,7,diameter,backColor); IBiTreeNode currentNode; IBiTreeNode parentNode = rootNode; IBiTreeNode tempParentNode0 = rootNode; //用来暂时保存根结点 rootLineNode = CreateLineNode(rootNode,rootNode); IBiTreeNode currentLineNode; IBiTreeNode parentLineNode = rootLineNode; IBiTreeNode tempParentLineNode0 = rootLineNode; //用来暂时保存根结点 if(flagArray[2,3] == true) { currentNode = CreateBiTreeNode(2,3,diameter,backColor); currentLineNode = CreateLineNode(parentNode,currentNode); parentNode.LeftChild = currentNode; parentNode = currentNode; IBiTreeNode tempParentNode1 = currentNode; parentLineNode.LeftChild = currentLineNode; parentLineNode = currentLineNode; IBiTreeNode tempParentLineNode1 = currentLineNode; //同样用来保存某个结点 if(flagArray[4,1] == true) { currentNode = CreateBiTreeNode(4,1,diameter,backColor); currentLineNode = CreateLineNode(parentNode,currentNode); parentNode.LeftChild = currentNode; parentNode = currentNode; parentLineNode.LeftChild = currentLineNode; parentLineNode = currentLineNode; if(flagArray[6,0] == true) { currentNode = CreateBiTreeNode(6,0,diameter,backColor); currentLineNode = CreateLineNode(parentNode,currentNode); parentNode.LeftChild = currentNode; parentLineNode.LeftChild = currentLineNode; } if(flagArray[6,2] == true) { currentNode = CreateBiTreeNode(6,2,diameter,backColor); currentLineNode = CreateLineNode(parentNode,currentNode); parentNode.RightChild = currentNode; parentLineNode.RightChild = currentLineNode; } } if(flagArray[4,5] == true) { currentNode = CreateBiTreeNode(4,5,diameter,backColor); parentNode = tempParentNode1; parentLineNode = tempParentLineNode1; currentLineNode = CreateLineNode(parentNode,currentNode); parentNode.RightChild = currentNode; parentNode = currentNode; parentLineNode.RightChild = currentLineNode; parentLineNode = currentLineNode; if(flagArray[6,4] == true) { currentNode = CreateBiTreeNode(6,4,diameter,backColor); currentLineNode = CreateLineNode(parentNode,currentNode); parentNode.LeftChild = currentNode; parentLineNode.LeftChild = currentLineNode; } if(flagArray[6,6] == true) { currentNode = CreateBiTreeNode(6,6,diameter,backColor); currentLineNode = CreateLineNode(parentNode,currentNode); parentNode.RightChild = currentNode; parentLineNode.RightChild = currentLineNode; } } } if(flagArray[2,11] == true) { currentNode = CreateBiTreeNode(2,11,diameter,backColor); parentNode = tempParentNode0; parentLineNode = tempParentLineNode0; currentLineNode = CreateLineNode(parentNode,currentNode); parentNode.RightChild = currentNode; parentLineNode.RightChild = currentLineNode; parentNode = currentNode; parentLineNode = currentLineNode; IBiTreeNode tempParentNode2 = currentNode; //同样用来保存某个结点 IBiTreeNode tempParentLineNode2 = currentLineNode; //同样用来保存某个结点 if(flagArray[4,9] == true) { currentNode = CreateBiTreeNode(4,9,diameter,backColor); currentLineNode = CreateLineNode(parentNode,currentNode); parentNode.LeftChild = currentNode; parentLineNode.LeftChild = currentLineNode; parentNode = currentNode; parentLineNode = currentLineNode; if(flagArray[6,8] == true) { currentNode = CreateBiTreeNode(6,8,diameter,backColor); currentLineNode = CreateLineNode(parentNode,currentNode); parentNode.LeftChild = currentNode; parentLineNode.LeftChild = currentLineNode; } if(flagArray[6,10] == true) { currentNode = CreateBiTreeNode(6,10,diameter,backColor); currentLineNode = CreateLineNode(parentNode,currentNode); parentNode.RightChild = currentNode; parentLineNode.RightChild = currentLineNode; } } if(flagArray[4,13] == true) { currentNode = CreateBiTreeNode(4,13,diameter,backColor); parentNode = tempParentNode2; parentLineNode = tempParentLineNode2; currentLineNode = CreateLineNode(parentNode,currentNode); parentNode.RightChild = currentNode; parentNode = currentNode; parentLineNode.RightChild = currentLineNode; parentLineNode = currentLineNode; if(flagArray[6,12] == true) { currentNode = CreateBiTreeNode(6,12,diameter,backColor); currentLineNode = CreateLineNode(parentNode,currentNode); parentNode.LeftChild = currentNode; parentLineNode.LeftChild = currentLineNode; } if(flagArray[6,14] == true) { currentNode = CreateBiTreeNode(6,14,diameter,backColor); currentLineNode = CreateLineNode(parentNode,currentNode); parentNode.RightChild = currentNode; parentLineNode.RightChild = currentLineNode; } } } for(int i = 0;i < 7;i++) { for(int j = 0;j < 15;j++) { Console.Write(charArray[i,j] + " "); } Console.WriteLine(); } }