public Petal(int d) { data = d; right = null; left = null; parent = null; }
public void Add(int d) { bool flag = false; for (int i = 0; i < arr.Length; i++) { if (d == arr[i]) { flag = true; } } if (!flag) { arr[0] = d; Array.Sort(arr); /*for(int i = 0;i<arr.Length;i++) * Console.Write(arr[i] + " ");*/ } if (root != null) { Petal newPetal = new Petal(d); Petal currentPetal = root; while (currentPetal != null) { if (newPetal.data > currentPetal.data) { if (currentPetal.right != null) { currentPetal = currentPetal.right; } else { currentPetal.right = newPetal; newPetal.parent = currentPetal; break; } } if (newPetal.data < currentPetal.data) { if (currentPetal.left != null) { currentPetal = currentPetal.left; } else { currentPetal.left = newPetal; newPetal.parent = currentPetal; break; } } } } else { root = new Petal(d); } }
public Petal search(int val) { Petal currentPetal = root; while (currentPetal != null) { if (currentPetal.data == val) { return(currentPetal); } else if (val > currentPetal.data) { currentPetal = currentPetal.right; } else { currentPetal = currentPetal.left; } } return(null); }
public void remove(Petal curPetal) { if (curPetal.left == null && curPetal.right == null) { Petal parentPetal = curPetal.parent; if (curPetal == parentPetal.left) { parentPetal.left = null; } if (curPetal == parentPetal.right) { parentPetal.right = null; } } if ((curPetal.left == null && curPetal.right != null) || (curPetal.left != null && curPetal.right == null)) { if (curPetal.parent.left == curPetal) { if (curPetal.left != null) { curPetal.parent.left = curPetal.left; } else { curPetal.parent.left = curPetal.right; } } if (curPetal.parent.right == curPetal) { if (curPetal.right != null) { curPetal.parent.right = curPetal.right; } else { curPetal.parent.right = curPetal.left; } } } if (curPetal.left != null && curPetal.right != null) { if (curPetal.right != null && curPetal.right.left != null) { Petal leftTree = curPetal.left; Petal start = curPetal.right.left; while (start.left != null) { start = start.left; } start.parent.left = null; Petal rightTree = curPetal.right; start.left = leftTree; start.right = rightTree; if (curPetal.parent.left == curPetal) { curPetal.parent.left = start; } else { curPetal.parent.right = start; } return; } if (curPetal.right != null && curPetal.right.left == null) { Console.Write("HERE"); Petal leftChild = curPetal.left; Petal rightChild = curPetal.right; rightChild.left = leftChild; if (curPetal.parent.left == curPetal) { curPetal.parent.left = rightChild; } else { curPetal.parent.right = rightChild; } } } }
public BinTree(int N) { root = null; arr = new int[N]; iCurrent = 0; }
public void Show2(int level, Petal[] curPetals) { if (level == 0) { if (root != null) { int cnt = 0; Console.Write(" " + root.data); Console.Write("\n"); Petal[] pts = new Petal[(level + 1) * 2]; pts[0] = root.left; pts[1] = root.right; Show2(level + 1, pts); } } else { for (int i = 0; i < curPetals.Length; i++) { if (i == (curPetals.Length / 2)) { Console.Write(" "); } if (curPetals[i] != null) { Console.Write(" " + curPetals[i].data); } else { Console.Write(" *"); } } Console.Write("\n"); int j = 0; int cntNull = 0; Petal[] pts = new Petal[(int)(Math.Pow(2, (level + 1)))]; for (int i = 0; i < curPetals.Length; i++) { if (curPetals[i] == null) { cntNull++; pts[j] = null; pts[j + 1] = null; } else { pts[j] = curPetals[i].left; pts[j + 1] = curPetals[i].right; } j += 2; } if (cntNull == curPetals.Length) { return; } else { Show2(level + 1, pts); } } }