private BinTreeNode treeSuccessor(final BinTreeNode x_param) { BinTreeNode x = x_param; BinTreeNode result; if (x.right != null) { { /*$goal 11 reachable*/ } result = treeMinimum(x.right); } else { { /*$goal 12 unreachable*/ } BinTreeNode y = x.parent; while (y != null && x == y.right) { { /*$goal 13 unreachable*/ } x = y; y = y.parent; } result = y; } { /*$goal 14 reachable*/ } return(result); }
public void Add(int x) { if (Key == x) { return; } if (x < Key) { if (Left == null) { Left = new BinTreeNode(x); } else { Left.Add(x); } } else { if (Right == null) { Right = new BinTreeNode(x); } else { Right.Add(x); } } }
public boolean find(int x) { BinTreeNode current = root; while (current != null) { { /*$goal 0 reachable*/ } if (current.key == x) { { /*$goal 1 reachable*/ } return(true); } if (x < current.key) { { /*$goal 2 reachable*/ } current = current.left; } else { { /*$goal 3 reachable*/ } current = current.right; } } { /*$goal 4 reachable*/ } return(false); }
private boolean repOK_isOrdered(BinTreeNode n, int min, int max) { if ((n.key <= (min)) || (n.key >= (max))) { return(false); } if (n.left != null) { if (!repOK_isOrdered(n.left, min, n.key)) { return(false); } } if (n.right != null) { if (!repOK_isOrdered(n.right, n.key, max)) { return(false); } } return(true); }
//מקבלת עץ ביטוי //מחזירה את ערכו כפעולת חשבון public static int ExpretionTreeValue(BinTreeNode <char> t) { if (!IfLeaf(t)) { switch (t.GetInfo()) { case '+': return(ExpretionTreeValue(t.GetLeft()) + ExpretionTreeValue(t.GetRight())); break; case '-': return(ExpretionTreeValue(t.GetLeft()) - ExpretionTreeValue(t.GetRight())); break; case '*': return(ExpretionTreeValue(t.GetLeft()) * ExpretionTreeValue(t.GetRight())); break; case '/': return(ExpretionTreeValue(t.GetLeft()) / ExpretionTreeValue(t.GetRight())); break; default: break; } } return(t.GetInfo() - 48); }
public void Add(int x) { if (_root == null) _root = new BinTreeNode(x); else _root.Add(x); }
//מקבלת עץ //מחזירה את כמות הבנים היחידים שערכם אי זוגי public static int SumLonlyIzugi(BinTreeNode <int> t) { if (t != null) { if (t.GetRight() != null && t.GetLeft() == null) { if (t.GetRight().GetInfo() % 2 != 0) { return(t.GetRight().GetInfo() + SumLonlyIzugi(t.GetLeft()) + SumLonlyIzugi(t.GetRight())); } return(SumLonlyIzugi(t.GetLeft()) + SumLonlyIzugi(t.GetRight())); } else { if (t.GetLeft() != null && t.GetRight() == null) { if (t.GetLeft().GetInfo() % 2 != 0) { return(t.GetLeft().GetInfo() + SumLonlyIzugi(t.GetLeft()) + SumLonlyIzugi(t.GetRight())); } } return(SumLonlyIzugi(t.GetLeft()) + SumLonlyIzugi(t.GetRight())); } } return(0); }
private boolean repOK_isOrdered(BinTreeNode n) { int min = repOK_isOrderedMin(n); int max = repOK_isOrderedMax(n); return(repOK_isOrdered(n, min, max)); }
// Löschen mit 2 KindElementen void deleteWith2Children(BinTreeNode foundElement, BinTreeNode fatherNode, bool isChildLeft) { if (fatherNode != null) { int symPredecessor = findSymmetricalPredecessor(foundElement).value; Delete(symPredecessor); if (isChildLeft) { fatherNode.left.value = symPredecessor; } else { fatherNode.right.value = symPredecessor; } } else { int symPredecessor = findSymmetricalPredecessor(foundElement).value; Delete(symPredecessor); root.value = symPredecessor; } }
static void Main(string[] args) { BinTreeNode<string> A = new BinTreeNode<string>("A"); BinTreeNode<string> B = new BinTreeNode<string>("B"); BinTreeNode<string> C = new BinTreeNode<string>("C"); BinTreeNode<string> D = new BinTreeNode<string>("D"); BinTree<string> BT = new BinTree<string>(A); BT.Insert(A, B, C); BT.Insert(B, D, null); Console.WriteLine("前序遍历:{0}", BT.PreOrderTraversal()); Console.WriteLine("中序遍历:{0}", BT.MidOrderTraversal()); Console.WriteLine("后序遍历:{0}", BT.PostOrderTraversal()); Console.WriteLine("层次遍历:{0}", BT.LevelTraversal()); BinTreeNode<string> Parent = BT.GetParent(D); BinTreeNode<string> LSibling = BT.GetLeftSibling(C); BinTreeNode<string> RSibling = BT.GetRightSibling(B); if (Parent != null) Console.WriteLine("结点{0}的Parent结点:{1}", D.Data, Parent.Data); else Console.WriteLine("结点{0}无Parent.", D.Data); if (LSibling != null) Console.WriteLine("结点{0}的左兄弟结点:{1}", C.Data, LSibling.Data); else Console.WriteLine("结点{0}无左兄弟结点.", C.Data); if (LSibling != null) Console.WriteLine("结点{0}的右兄弟结点:{1}", B.Data, RSibling.Data); else Console.WriteLine("结点{0}无右兄弟结点.", B.Data); BT.DeleteSubTree(D); Console.WriteLine("把结点{0}从二叉树中移除.", D.Data); Console.WriteLine("前序遍历:{0}", BT.PreOrderTraversal()); Console.WriteLine("中序遍历:{0}", BT.MidOrderTraversal()); Console.WriteLine("后序遍历:{0}", BT.PostOrderTraversal()); Console.WriteLine("层次遍历:{0}", BT.LevelTraversal()); BinTreeNode<string> E = BT.Search("A"); if (E != null && E.LeftChild != null) { Console.WriteLine("寻找结点{0}的左孩子为{1}", E.Data, E.LeftChild.Data); } else { Console.WriteLine("未找到{0}结点或者{0}结点左孩子为null", A.Data); } Console.WriteLine("叶子结点的个数:{0}", BT.GetLeafCont()); BT.Exchange(); Console.WriteLine("前序遍历:{0}", BT.PreOrderTraversal()); Console.WriteLine("中序遍历:{0}", BT.MidOrderTraversal()); Console.WriteLine("后序遍历:{0}", BT.PostOrderTraversal()); Console.WriteLine("层次遍历:{0}", BT.LevelTraversal()); }
public BinSearchTree(params int[] key) { rootNode = new BinTreeNode(key[0]); for (int i = 1; i < key.Length; i++) { Insert(key[i]); } }
public static int NumOfNodes(BinTreeNode <T> t) { if (t == null) { return(0); } return(NumOfNodes(t.GetLeft()) + NumOfNodes(t.GetRight()) + 1); }
//מקבלת עץ חיפוש //מחזירה את האיבר מקסימלי בעץ //רקורסיה public static BinTreeNode <int> Maximaly(BinTreeNode <int> t) { if (t.GetRight() != null) { return(Maximaly(t.GetRight())); } return(t); }
//מקבלת עץ חיפוש //מחזירה את האיבר המינימלי בעץ //רקורסיה public static BinTreeNode <int> Minimaly(BinTreeNode <int> t) { if (t.GetLeft() != null) { return(Minimaly(t.GetLeft())); } return(t); }
// טענת כניסה: הפעולה מקבלת עץ בינארי של מספרים שלמים ומספר שלם // טענת יציאה: הפעולה מחזירה "אמת" אם המספר שהתקבל נמצא בעץ, אחרת מחזירה "שקר" // סיבוכיות זמן ריצה: O(n) public static bool IsExistTree(BinTreeNode <int> bt, int n) { if (bt == null) { return(false); } return(bt.GetInfo() == n || IsExistTree(bt.GetLeft(), n) || IsExistTree(bt.GetRight(), n)); }
public static int Max(BinTreeNode tree) { if (tree == null) return -1; if (tree.Right == null) return tree.Key; return Max(tree.Right); }
// טענת כניסה: הפעולה מקבלת עץ בינארי של מספרים שלמים // טענת יציאה: הפעולה מחזירה את מספר הצמתים בעץ // סיבוכיות זמן ריצה: O(n) public static int CountNodes(BinTreeNode <int> bt) { if (bt == null) { return(0); } return(1 + CountNodes(bt.GetLeft()) + CountNodes(bt.GetRight())); }
// טענת כניסה: הפעולה מקבלת עץ בינארי של מספרים שלמים // טענת יציאה: הפעולה מחזירה את סכום ערכי כל הצמתים בעץ // סיבוכיות זמן ריצה: O(n) public static int SumTree(BinTreeNode <int> bt) { if (bt == null) { return(0); } return(bt.GetInfo() + SumTree(bt.GetLeft()) + SumTree(bt.GetRight())); }
// טענת כניסה: הפעולה מקבלת עץ בינארי של מספרים שלמים // טענת יציאה: הפעולה מדפיסה את ערכי הצמתים בעץ לפי סדר סופי - שמאל, ימין, שורש // סיבוכיות זמן ריצה: O(n) public static void PrintPostOrder(BinTreeNode <int> bt) { if (bt != null) { PrintPostOrder(bt.GetLeft()); PrintPostOrder(bt.GetRight()); Console.WriteLine(bt.GetInfo()); } }
//מקבלת עץ חיפוש //מדפיסה אותו בסדר יורד public static void DownOrderPrint(BinTreeNode <int> t) { if (t != null) { DownOrderPrint(t.GetRight()); Console.Write(t.GetInfo() + " "); DownOrderPrint(t.GetLeft()); } }
//$goals 17 //$benchmark public void removeTest(BinTree tree, BinTreeNode z) { BinTreeNode ret_val; if (tree != null && z != null && tree.repOK()) { ret_val = tree.remove(z); } }
public static bool Contains(BinTreeNode tree, int x) { if (tree == null) return false; if (tree.Key == x) return true; if (x < tree.Key) return Contains(tree.Left, x); return Contains(tree.Right, x); }
private int repOK_isOrderedMin(BinTreeNode n) { BinTreeNode curr = n; while (curr.left != null) { curr = curr.left; } return(curr.key); }
public void Add2(int x) { if (x < Key) { if (Left == null) Left = new BinTreeNode(x); else Left.Add2(x); } }
BinTreeNode findSymmetricalPredecessor(BinTreeNode e) { BinTreeNode temp = (BinTreeNode)e.left; while (temp.right != null) { temp = (BinTreeNode)temp.right; } return(temp); }
private int repOK_isOrderedMax(BinTreeNode n) { BinTreeNode curr = n; while (curr.right != null) { curr = curr.right; } return(curr.key); }
//מקבלת עץ חיפוש //מחזירה את האיבר מקסימלי בעץ //While public static BinTreeNode <int> Maximaly2(BinTreeNode <int> t) { BinTreeNode <int> tree = t; while (tree.GetRight() != null) { tree = tree.GetRight(); } return(tree); }
public static BinTreeNode Add(BinTreeNode tree, int x) { if (tree == null) return new BinTreeNode(x); if (x < tree.Key) tree.Left = Add(tree.Left, x); else if (x > tree.Key) tree.Right = Add(tree.Right, x); return tree; }
/// /// /// עץ בינארי /// // טענת כניסה: הפעולה מקבלת עץ בינארי של מספרים שלמים // טענת יציאה: הפעולה מחזירה עץ חדש, זהה בדיוק בצמתיו ובמיקומים שלהם לעץ שהתקבל // סיבוכיות זמן ריצה: O(n) public static BinTreeNode <int> CloneTree(BinTreeNode <int> bt) { if (bt == null) { return(null); } BinTreeNode <int> left = CloneTree(bt.GetLeft()); BinTreeNode <int> right = CloneTree(bt.GetRight()); return(new BinTreeNode <int>(left, bt.GetInfo(), right)); //להחליף ימין ושמאל לקבלת עץ מראה }
// טענת כניסה: הפעולה מקבלת עץ בינארי של מספרים שלמים // טענת יציאה: הפעולה מחזירה את הערך הגבוה ביותר בעץ // סיבוכיות זמן ריצה: O(n) public static int MaxValue(BinTreeNode <int> bt) { if (IsLeaf(bt)) //! { return(bt.GetInfo()); } int maxLeft = MaxValue(bt.GetLeft()); int maxRight = MaxValue(bt.GetRight()); return(Math.Max(bt.GetInfo(), Math.Max(maxLeft, maxRight))); }
//מקבלת עץ תווים //מחזירה אמת אם הוא עץ עלה public static bool IfLeaf(BinTreeNode <char> t) { if (t != null) { //if (t.GetLeft() == null && t.GetRight() == null) // return true; //return false; return(t.GetLeft() == null && t.GetRight() == null); } return(false); }
public void Setup() { _algo = new BinaryTreesAlgorithms(); _treeElements = new long[] { 5,2,1,3,8,6,11,9,15 }; _testTree = _algo.GetBSTFromPreorder(_treeElements); Assert.IsNotNull(_testTree); }
public void add(int x) { BinTreeNode current = root; if (root == null) { {/*$goal 0 reachable*/} root = new BinTreeNode(); initNode(root,x); return; } while (current.key != x) { {/*$goal 1 reachable*/} if (x < current.key) { {/*$goal 2 reachable*/} if (current.left == null) { {/*$goal 3 reachable*/} current.left = new BinTreeNode(); initNode(current.left,x); } else { {/*$goal 4 reachable*/} current = current.left; } } else { {/*$goal 5 reachable*/} if (current.right == null) { {/*$goal 6 reachable*/} current.right = new BinTreeNode(); initNode(current.right,x); } else { {/*$goal 7 reachable*/} current = current.right; } } } {/*$goal 8 reachable*/} }
private int repOK_isOrderedMax(BinTreeNode n) { BinTreeNode curr = n; while (curr.right!=null) { curr = curr.right; } return curr.key; }
private int repOK_isOrderedMin(BinTreeNode n) { BinTreeNode curr = n; while (curr.left!=null) { curr = curr.left; } return curr.key; }
private boolean repOK_isOrdered(BinTreeNode n, int min, int max) { if ((n.key <= (min)) || (n.key >= (max))) return false; if (n.left != null) if (!repOK_isOrdered(n.left, min, n.key)) return false; if (n.right != null) if (!repOK_isOrdered(n.right, n.key, max)) return false; return true; }
private boolean repOK_isOrdered(BinTreeNode n) { int min = repOK_isOrderedMin(n); int max = repOK_isOrderedMax(n); return repOK_isOrdered(n, min, max); }
private void initNode(BinTreeNode node, int x) { node.key = x; node.left = null; node.right = null; }
//$goals 17 //$benchmark public void removeTest(BinTree tree, BinTreeNode z) { BinTreeNode ret_val; if (tree!=null && z!=null && tree.repOK()) { ret_val = tree.remove(z); } }
public BinTreeNode remove(final BinTreeNode z) { BinTreeNode y = null; if (z.left == null || z.right == null) { {/*$goal 0 reachable*/} y = z; } else { {/*$goal 1 reachable*/} y = treeSuccessor(z); } BinTreeNode x = null; if (y.left != null) { {/*$goal 2 reachable*/} x = y.left; } else { {/*$goal 3 reachable*/} x = y.right; } if (x != null) { {/*$goal 4 reachable*/} x.parent = y.parent; } if (y.parent == null) { {/*$goal 5 reachable*/} this.root = x; } else { {/*$goal 6 reachable*/} if (y == y.parent.left){ {/*$goal 7 reachable*/} y.parent.left = x; }else{ {/*$goal 8 reachable*/} y.parent.right = x; } } if (y != z) { {/*$goal 9 reachable*/} z.key = y.key; } {/*$goal 10 reachable*/} return y; }