Note that there are some explanatory texts on larger screens.

plurals
  1. PORed-Black Tree Insertion Fixup Error
    primarykey
    data
    text
    <p>I have a homework problem about inserting into red-black trees in c#. I wrote the code below and the program works without any problems for adding first 3 numbers. When I try to add the 4th number, I get a NullReferenceException. I'm trying to solve the error for 2 days but I can't figure it out.</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Algoritmalar3b { class rbtNode { public int? key; public char? color; public rbtNode left, right, p; public rbtNode(int? key, char? color, rbtNode left, rbtNode right, rbtNode p) { this.key = key; this.color = color; this.left = left; this.right = right; this.p = p; } } class Program { static rbtNode Tnil = new rbtNode(null, 'B', null, null, null); static void Main(string[] args) { rbtNode root = new rbtNode(null, 'B', Tnil, Tnil, null); RB_Insert(root, 7); RB_Insert(root, 3); RB_Insert(root, 89); RB_Insert(root, 4); RB_Insert(root, 9); RB_Insert(root, 15); RB_Insert(root, 35); RB_Insert(root, 8); RB_Insert(root, 24); preOrderWalk(root); } static void RB_Insert(rbtNode T, int? deger) { rbtNode z = new rbtNode(deger, null, Tnil, Tnil, null); if (T.key == null) T.key = deger; else { rbtNode y = new rbtNode(null, null, Tnil, Tnil, null); y = Tnil; rbtNode x = new rbtNode(null, null, Tnil, Tnil, null); x = T; while (x != Tnil) { y = x; if (z.key &lt; x.key) x = x.left; else x = x.right; } z.p = y; if (y == Tnil) T = z; else if (z.key &lt; y.key) y.left = z; else y.right = z; z.left = Tnil; z.right = Tnil; z.color = 'R'; RB_Insert_Fixup(T, z); } } static void RB_Insert_Fixup(rbtNode T, rbtNode z) { rbtNode y = new rbtNode(null, null, Tnil, Tnil, null); while (z.p.color == 'R') { if (z.p == z.p.p.left) { y = z.p.p.right; if (y.color == 'R') { z.p.color = 'B'; y.color = 'B'; z.p.p.color = 'R'; z = z.p.p; } else if (z == z.p.right) { z = z.p; Left_Rotate(T, z); z.p.color = 'B'; z.p.p.color = 'R'; Right_Rotate(T, z.p.p); } } else { y = z.p.p.left; if (y.color == 'R') { z.p.color = 'B'; y.color = 'B'; z.p.p.color = 'R'; z = z.p.p; } else if (z == z.p.left) { z = z.p; Left_Rotate(T, z); z.p.color = 'B'; z.p.p.color = 'R'; Right_Rotate(T, z.p.p); } } } T.color = 'B'; } static void Left_Rotate(rbtNode T, rbtNode x) { rbtNode y = new rbtNode(null, null, Tnil, Tnil, null); y = x.right; x.right = y.left; if (y.left != Tnil) y.left.p = x; y.p = x.p; if (x.p == Tnil) T = y; else if (x == x.p.left) x.p.left = y; else x.p.right = y; y.left = x; x.p = y; } static void Right_Rotate(rbtNode T, rbtNode x) { rbtNode y = new rbtNode(null, null, Tnil, Tnil, null); y = x.left; x.left = y.right; if (y.right != null) y.right.p = x; y.p = x.p; if (x.p == null) T = y; else if (x == x.p.right) x.p.right = y; else x.p.left = y; y.right = x; x.p = y; } static void preOrderWalk(rbtNode T) { if (T.color == 'B') Console.WriteLine("-{0}", T.key); else Console.WriteLine("{0}", T.key); if (T.left != null) preOrderWalk(T.left); if (T.right != null) preOrderWalk(T.right); } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload