bool BTreesAreEquals(BTN a, BTN b) { if (BTreeAsString(a) == BTreeAsString(b)) { return(true); } else { return(false); } }
public void run() { BTN a = new BTN(1) { left = new BTN(2), right = new BTN(3) { left = new BTN(4), right = new BTN(5) } }; BTN b = new BTN(1) { left = new BTN(2) { left = new BTN(4), right = new BTN(5) }, right = new BTN(3) }; BTN c = new BTN(1) { left = new BTN(2), right = new BTN(3) { left = new BTN(4), right = new BTN(5) } }; BTN d = new BTN(1) { left = new BTN(2) { left = new BTN(4), right = new BTN(5) }, right = new BTN(3) }; Console.WriteLine("Is a equal to b? {0}", BTreesAreEquals(a, b)); Console.WriteLine("Is a equal to c? {0}", BTreesAreEquals(a, c)); Console.WriteLine("Is a equal to d? {0}", BTreesAreEquals(a, d)); Console.WriteLine("Is b equal to c? {0}", BTreesAreEquals(b, c)); Console.WriteLine("Is b equal to d? {0}", BTreesAreEquals(b, d)); }
string BTreeAsString(BTN a) { var result = a.val.ToString(); if (a.left != null) { result = result + "-L-" + BTreeAsString(a.left); } if (a.right != null) { result = result + "-R-" + BTreeAsString(a.right); } return(result); }
bool BTreesAreEquals(BTN a, BTN b) { // se AMBOS forem nulos, então são iguais if (a == null && b == null) { return(true); } // se SOMENTE UM for nulo, então são diferentes if (a == null || b == null) { return(false); } // AMBOS são NÃO-NULOS, então todas propriedades devem ser iguais return((a.val == b.val) && BTreesAreEquals(a.left, b.left) && BTreesAreEquals(a.right, b.right)); }
bool BTreesAreEquals(BTN a, BTN b) { if (a.val != b.val) { return(false); } if ( (a.left != NULL && b.left == NULL) || (a.left == NULL && b.left != NULL) || (a.right != NULL && b.right == NULL) || (a.right == NULL && b.right != NULL) ) { return(false); } if (a.left == NULL && b.left == NULL && a.right == NULL && b.right == NULL) { return(true); } return(BTreesAreEquals(a.left, b.left) && BTreesAreEquals(a.right, b.right)); }
public void run() { BTN a = new BTN { val = 1, left = new BTN { val = 2 }, right = new BTN { val = 3, left = new BTN { val = 4 }, right = new BTN { val = 5 } } }; BTN b = new BTN { val = 1, left = new BTN { val = 2, left = new BTN { val = 4 }, right = new BTN { val = 5 } }, right = new BTN { val = 3 } }; BTN c = new BTN { val = 1, left = new BTN { val = 2 }, right = new BTN { val = 3, left = new BTN { val = 4 }, right = new BTN { val = 5 } } }; BTN d = new BTN { val = 1, left = new BTN { val = 2, left = new BTN { val = 4 }, right = new BTN { val = 5 } }, right = new BTN { val = 3 } }; Console.WriteLine("Is a equal to b? {0}", a.Equals(b)); Console.WriteLine("Is a equal to c? {0}", a.Equals(c)); Console.WriteLine("Is a equal to d? {0}", a.Equals(d)); Console.WriteLine("Is b equal to c? {0}", b.Equals(c)); Console.WriteLine("Is b equal to d? {0}", b.Equals(d)); }
public void run() { BTN a = new BTN { val = 1, left = new BTN { val = 2 }, right = new BTN { val = 3, left = new BTN { val = 4 }, right = new BTN { val = 5 } } }; BTN b = new BTN { val = 1, left = new BTN { val = 2, left = new BTN { val = 4 }, right = new BTN { val = 5 } }, right = new BTN { val = 3 } }; BTN c = new BTN { val = 1, left = new BTN { val = 2 }, right = new BTN { val = 3, left = new BTN { val = 4 }, right = new BTN { val = 5 } } }; BTN d = new BTN { val = 1, left = new BTN { val = 2, left = new BTN { val = 4 }, right = new BTN { val = 5 } }, right = new BTN { val = 3 } }; Console.WriteLine("Is a equal to b? {0}", BTreesAreEquals(a, b)); Console.WriteLine("Is a equal to c? {0}", BTreesAreEquals(a, c)); Console.WriteLine("Is a equal to d? {0}", BTreesAreEquals(a, d)); Console.WriteLine("Is b equal to c? {0}", BTreesAreEquals(b, c)); Console.WriteLine("Is b equal to d? {0}", BTreesAreEquals(b, d)); }