0

I have used this code from this website to practice with avl tree, but when i change the first bit from

        tree.Add(2);
        tree.Add(1);
        tree.Add(0);
        tree.Add(-1);
        tree.Add(-2);
        tree.Add(3);
        tree.Add(5);
        tree.Add(4);

        tree.DisplayTree();

to

        tree.Add(5);
        tree.Add(3);
        tree.Add(7);
        tree.Add(8);
        tree.Delete(3);
        tree.DisplayTree();

I get a System.NullReferenceException error . if I delete 7 instead of 3 I won't get that error . Does anyone know why this happens ?

Dylan Knowles
  • 2,726
  • 1
  • 26
  • 52
  • Please do enough debugging so that you can get to a point where your question asks more than just "why does `NullReferenceException` get thrown?" If, once you do, you still are having trouble, post a new question with a good [mcve], along with a precise description of what _specifically_ you are having trouble figuring out. – Peter Duniho May 02 '16 at 04:41
  • Change your balance_factor method into this. private int balance_factor(Node current) { if (current.left != null) { int l = getHeight(current.left); int r = getHeight(current.right); int b_factor = l - r; return b_factor; } return 0; } – Jobert Enamno May 02 '16 at 05:06
  • Hi. I am the author of the post and I have already fixed this issue as of now. The bug was in both the private and public Delete methods. The main issue was in the checking for a RR case and then reassigning the proper root node after the correction. Once again thanks for detecting this error and for your feedback. I have tested further cases & scenarios to double check my updated solution as well. – Karim O. May 02 '16 at 05:35

0 Answers0