[infostudents] Info 2 Aufgabe 1 und 2 ohne 1c

  • From: Guido Solbach <ich@xxxxxxxxxxxxxxxx>
  • To: infostudents@xxxxxxxxxxxxx
  • Date: Mon, 02 Jul 2007 10:25:15 +0200

Hallo,
mit der Höhe eines Baumes hatte ich in meiner ersten Version einen Fehler.
Wenn jemand was zur 1c hat wäre das sehr gut....
Gruss
Guido
public class TreeTester {
        public TreeTester(){
                ChildSiblingNode ancestor = new ChildSiblingNode(2);
                ChildSiblingNode child = ancestor.addChild(8);
                child.addChild(36);
                child = child.addChild(21);
                child.addChild(26);
                child = ancestor.addChild(13);
                child = child.addChild(15);
                child.addChild(29);
                child.addChild(16);
                child.addChild(31);
                ancestor = ancestor.addChild(45);
                System.out.println("Fertig");
        }

        /**
         * @param args
         */
        public static void main(String[] args) {
                new TreeTester();

        }

}

JPEG image

public class ChildSiblingNode {
        int key;
        ChildSiblingNode parent;
        ChildSiblingNode child;
        ChildSiblingNode left;
        ChildSiblingNode right;
        
        /**
         * Erzeugt einen neuen Knoten mit gegebenem Schlüssel.
         * 
         * @param key der Schlüsselwert
         */
        public ChildSiblingNode(int key) {
                this.key = key;
                parent = null;
                child = null;
                left = this;
                right = this;
        }
        
        /**
         * Fügt diesem Knoten einen neuen Knoten mit gegebenem Schlüssel 
         * als neuen Sohn hinzu. Der enue Knoten wird zurückgegeben.
         *  
         * @param key der Schlüssel des neuen Knotens
         */
        public ChildSiblingNode addChild(int key) {
                ChildSiblingNode node = new ChildSiblingNode(key);
                node.parent = this;
                if(this.child!=null){
                        node.left = this.child;
                        node.right = this.child.right;
                        this.child.right=node;
                }
                this.child=node;
                return node;
        }
        
        /**
         * Trennt diesen Knoten (samt all seinen Nachkommen) vom Vater 
         * und aus der Liste seiner Geschwister ab.
         */
        public void cut() {
                if(this.left == this){
                        if(this.parent!=null){
                                this.parent.child = null;
                        }
                } else {
                        if(this.parent!=null){
                                this.parent.child = this.left;
                                this.left.right = this.right;
                                this.right.left = this.left;
                        }
                }
        }
}

Other related posts: