Exemplo n.º 1
0
Arquivo: tree.cs Projeto: Squarion/ds
        public static void insert(Node z)
        {
            Node y = nil;
            Node x = root;
            // path from root to correct node
            while (!x.Equals(nil)) { // x is niet nil
                y = x;
                if (z.getKey () < x.getKey ()) { // key van z is kleiner dan key van x -> ga naar links
                    x = x.getLeft ();

                } else { // key van z is groter dan key van x -> ga naar rechts
                    x = x.getRight();
                }
                y.incrementSize ();
            }
            z.setParent (y); // y (laatste node in juiste tak) is parent van z
            if (y.Equals(nil)) { // y (parent van z) is nil -> z is de root
                root = z;
            } else if (z.getKey () < y.getKey ()) { // key van z is kleiner dan key van y -> z is left node van y
                y.setLeft (z);
            } else {  // key van z is groter dan key van y -> z is right node van y
                y.setRight (z);
            }
            z.setLeft (nil); // z heeft geen kinderen
            z.setRight (nil); // z heeft geen kinderen
            z.setColor(false); // z is rood
            z.setSize(1);
            fixup (z);
        }