public static int SumRightNodes(BinTreeNode <int> r) { if (r.GetRight() == null) { return(0); } return(r.GetRight().GetValue() + SumRightNodes(r.GetRight()) + SumRightNodes(r.GetLeft())); }
public static int CountLeaves(BinTreeNode <int> r) { if (r == null) { return(0); } if (r.GetRight() == null && r.GetLeft() == null) { return(1); } return(CountLeaves(r.GetRight()) + CountLeaves(r.GetLeft())); }
public static int CountEvenVertices(BinTreeNode <int> r) { if (r == null) { return(0); } if (r.GetValue() % 2 == 0) { return(1 + CountEvenVertices(r.GetLeft()) + CountEvenVertices(r.GetRight())); } return(CountEvenVertices(r.GetLeft()) + CountEvenVertices(r.GetRight())); }
public static int CountPartialVertices(BinTreeNode <int> r) { if (r == null) { return(0); } if ((r.GetRight() != null && r.GetLeft() == null) || r.GetLeft() != null && r.GetRight() == null) { return(1 + CountPartialVertices(r.GetRight()) + CountPartialVertices(r.GetLeft())); } return(CountPartialVertices(r.GetRight()) + CountPartialVertices(r.GetLeft())); }
public static BinTreeNode <int> GetParent(BinTreeNode <int> r, int v) { if (r.GetLeft() == null && r.GetRight() == null) { return(null); } if (r.GetLeft().GetValue() == v || r.GetRight().GetValue() == v) { return(r); } GetParent(r.GetLeft(), v); GetParent(r.GetRight(), v); return(null); }
public static int CountVertices(BinTreeNode <int> r) { if (r == null) { return(0); } return(1 + CountVertices(r.GetLeft()) + CountVertices(r.GetRight())); }
public static bool IsInTree(BinTreeNode <int> r, int v) { if (r == null) { return(false); } if (r.GetValue() == v) { return(true); } return(IsInTree(r.GetLeft(), v) || IsInTree(r.GetRight(), v)); }
public static bool IsAllEven(BinTreeNode <int> r) { if (r == null) { return(true); } if (r.GetValue() % 2 == 1) { return(false); } return(IsAllEven(r.GetRight()) && IsAllEven(r.GetLeft())); }
public static void AddZeroToPartial(BinTreeNode <int> r) { if (r == null) { return; } if (r.GetLeft() != null && r.GetRight() == null) { r.SetRight(new BinTreeNode <int>(0)); AddZeroToPartial(r.GetLeft()); } else if (r.GetLeft() == null && r.GetRight() != null) { r.SetLeft(new BinTreeNode <int>(0)); AddZeroToPartial(r.GetRight()); } else { AddZeroToPartial(r.GetLeft()); AddZeroToPartial(r.GetRight()); } }
public static void DivideEvens(BinTreeNode <int> r) { if (r == null) { return; } if (r.GetValue() % 2 == 0) { r.SetValue(r.GetValue() / 2); } DivideEvens(r.GetLeft()); DivideEvens(r.GetRight()); }
public static Node <int> TurnToSortedList(Node <int> h, BinTreeNode <int> r) { if (r == null) { return(h); } h = TurnToSortedList(h, r.GetLeft()); Node <int> t = new Node <int>(r.GetValue()); Node <int> temp = h; Node <int> p = null; if (temp == null) { h = t; } else { FindNode(r, ref temp, ref p); h = AttachNode(h, t, temp, p); } h = TurnToSortedList(h, r.GetRight()); return(h); }