/// <summary> /// Метод добавления элементов в дерево /// </summary> /// <param name="val"></param> public void Add(int val) { bool added; if (top == null) { Node NewNode = new Node(val); top = NewNode; count++; return; } Node currentNode = top; added = false; Random rnd = new Random(); int n = 10; do { n = rnd.Next(0, 1); // if (val < currentNode.val) //{ if ((currentNode.left == null) || (currentNode.right == null)) { if (currentNode.left == null) { Node NewNode = new Node(val); currentNode.left = NewNode; sw.WriteLine(currentNode.val + "->" + currentNode.left.val + "[color=red]" + ";"); count++; added = true; return; } if (currentNode.right == null) { Node NewNode = new Node(val); currentNode.right = NewNode; sw.WriteLine(currentNode.val + "->" + currentNode.right.val + "[color=red]" + ";"); count++; added = true; return; } } else { if ((currentNode.right != top) && (currentNode.left != top)) { if ((currentNode.left != currentNode) && (currentNode.right != currentNode)) { if (currentNode.left != currentNode.right) { switch (n) { case 0: currentNode = currentNode.left; break; case 1: currentNode = currentNode.right; break; } } } } } n = 10; //} } while (!added); }
public MyTree(int val) { top = new Node(val); }
public Node() { this.val = 0; this.left = null; this.right = null; this.parent = null; }
//public static MyQueue queueNode = new MyQueue(); public MyTree() { top = null; }
public Node(int val) { this.val = val; this.left = null; this.right = null; this.parent = null; }
//Вертикальный обход СДЕЛАТЬ СО СВОИМ СПИСКОМ! public void leavesToList(Node currentNode) { if ((currentNode.left != null) || (currentNode.right != null)) { if (currentNode.left != null) { leavesToList(currentNode.left); } if (currentNode.right != null) { leavesToList(currentNode.right); } } else { listOfLeaves.Add(currentNode.val); countLeaves++; Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("Leave {0}: {1}", countLeaves, currentNode.val); } }