package treeTraversal;_x000D_
_x000D_
// Klasse voor een knoop voor een binaire boom_x000D_
// Met boomwandelingen pre-order en level-order_x000D_
import java.util.*;_x000D_
_x000D_
public class BinNode<E> {_x000D_
_x000D_
    private BinNode<E> parent, leftChild, rightChild;_x000D_
    private E userObject;_x000D_
    private static StringBuffer buffer;_x000D_
    private Queue<BinNode<E>> q; // queue voor level-order wandeling_x000D_
_x000D_
    public static final int LEFT = 0;  // public constanten voor het _x000D_
    public static final int RIGHT = 1; // toevoegen van linker- of rechterkind_x000D_
_x000D_
    // Constructors_x000D_
    public BinNode() {_x000D_
        this(null);_x000D_
    }_x000D_
_x000D_
    public BinNode(E userObject) {_x000D_
        parent = null;_x000D_
        leftChild = null;_x000D_
        rightChild = null;_x000D_
        this.userObject = userObject;_x000D_
    }_x000D_
_x000D_
    public String preOrderToString() {_x000D_
        buffer = new StringBuffer();_x000D_
        preOrder();            // roep recursieve methode aan_x000D_
        return buffer.toString();_x000D_
    }_x000D_
_x000D_
    private void preOrder() {_x000D_
        buffer.append(userObject.toString());  //bezoek van de node_x000D_
        if (leftChild != null) {_x000D_
            leftChild.preOrder();_x000D_
        }_x000D_
        if (rightChild != null) {_x000D_
            rightChild.preOrder();_x000D_
        }_x000D_
    }_x000D_
_x000D_
    public String postOrderToString() {_x000D_
        buffer = new StringBuffer();_x000D_
        postOrder();            // roep recursieve methode aan_x000D_
        return buffer.toString();_x000D_
    }_x000D_
_x000D_
    private void postOrder() {_x000D_
        if (leftChild != null) {_x000D_
            leftChild.postOrder();_x000D_
        }_x000D_
        if (rightChild != null) {_x000D_
            rightChild.postOrder();_x000D_
        }_x000D_
        buffer.append(userObject.toString());_x000D_
    }_x000D_
_x000D_
    public String inOrderToString() {_x000D_
        buffer = new StringBuffer();_x000D_
        inOrder();            // roep recursieve methode aan_x000D_
        return buffer.toString();_x000D_
    }_x000D_
_x000D_
    private void inOrder() {_x000D_
        if (leftChild != null) {_x000D_
            leftChild.inOrder();_x000D_
        }_x000D_
        buffer.append(userObject.toString());_x000D_
        if (rightChild != null) {_x000D_
            rightChild.inOrder();_x000D_
        }_x000D_
    }_x000D_
_x000D_
    public String levelOrderToString() {_x000D_
        buffer = new StringBuffer();_x000D_
        q = new LinkedList<BinNode<E>>();_x000D_
        q.add(this);_x000D_
        levelOrder();_x000D_
        return buffer.toString();_x000D_
    }_x000D_
_x000D_
    private void levelOrder() {_x000D_
        while (!q.isEmpty()) {_x000D_
            BinNode<E> knoop = q.remove();_x000D_
            buffer.append(knoop.userObject.toString());_x000D_
            if (knoop.leftChild != null) {_x000D_
                q.add(knoop.leftChild);_x000D_
            }_x000D_
            if (knoop.rightChild != null) {_x000D_
                q.add(knoop.rightChild);_x000D_
            }_x000D_
        }_x000D_
    }_x000D_
_x000D_
    public void add(BinNode<E> newChild) {_x000D_
        if (leftChild == null) {_x000D_
            insert(newChild, LEFT);_x000D_
        } else if (rightChild == null) {_x000D_
            insert(newChild, RIGHT);_x000D_
        } else {_x000D_
            throw new IllegalArgumentException(_x000D_
                    "Meer dan 2 kinderen");_x000D_
        }_x000D_
    }_x000D_
_x000D_
    public E get() {_x000D_
        return userObject;_x000D_
    }_x000D_
_x000D_
    public BinNode<E> getLeftChild() {_x000D_
        return leftChild;_x000D_
    }_x000D_
_x000D_
    public BinNode<E> getRightChild() {_x000D_
        return rightChild;_x000D_
    }_x000D_
_x000D_
    public BinNode<E> getParent() {_x000D_
        return parent;_x000D_
    }_x000D_
_x000D_
    public void insert(BinNode<E> newChild, int childIndex) {_x000D_
        if (isAncestor(newChild)) {_x000D_
            throw new IllegalArgumentException(_x000D_
                    "Nieuw kind is voorouder");_x000D_
        }_x000D_
        if (childIndex != LEFT_x000D_
                && childIndex != RIGHT) {_x000D_
            throw new IllegalArgumentException(_x000D_
                    "Index moet 0 of 1 zijn");_x000D_
        }_x000D_
        if (newChild != null) {_x000D_
            BinNode<E> oldParent = newChild.getParent();_x000D_
            if (oldParent != null) {_x000D_
                oldParent.remove(newChild);_x000D_
            }_x000D_
        }_x000D_
        newChild.parent = this;_x000D_
        if (childIndex == LEFT) {_x000D_
            leftChild = newChild;_x000D_
        } else {_x000D_
            rightChild = newChild;_x000D_
        }_x000D_
    }_x000D_
_x000D_
    public boolean isChild(BinNode<E> aNode) {_x000D_
        return aNode == null_x000D_
                ? false_x000D_
                : aNode.getParent() == this;_x000D_
    }_x000D_
_x000D_
    public boolean isAncestor(BinNode<E> aNode) {_x000D_
        BinNode<E> ancestor = this;_x000D_
        while (ancestor != null && ancestor != aNode) {_x000D_
            ancestor = ancestor.getParent();_x000D_
        }_x000D_
        return ancestor != null;_x000D_
    }_x000D_
_x000D_
    public void remove(BinNode<E> aChild) {_x000D_
        if (aChild == null) {_x000D_
            throw new IllegalArgumentException(_x000D_
                    "Argument is null");_x000D_
        }_x000D_
_x000D_
        if (!isChild(aChild)) {_x000D_
            throw new IllegalArgumentException(_x000D_
                    "Argument is geen kind");_x000D_
        }_x000D_
_x000D_
        if (aChild == leftChild) {_x000D_
            leftChild.parent = null;_x000D_
            leftChild = null;_x000D_
        } else {_x000D_
            rightChild.parent = null;_x000D_
            rightChild = null;_x000D_
        }_x000D_
    }_x000D_
_x000D_
    public String toString() {_x000D_
        return userObject.toString();_x000D_
    }_x000D_
}_x000D_

// public constanten voor het _x000D_
package treeTraversal;_x000D_
_x000D_
// Klasse voor een knoop voor een binaire boom_x000D_
// Met boomwandelingen pre-order en level-order_x000D_
import java.util.*;_x000D_
_x000D_
public class BinNode<E> {_x000D_
_x000D_
    private BinNode<E> parent, leftChild, rightChild;_x000D_
    private E userObject;_x000D_
    private static StringBuffer buffer;_x000D_
    private Queue<BinNode<E>> q; // queue voor level-order wandeling_x000D_
_x000D_
    public static final int LEFT = 0;  // public constanten<SUF>
    public static final int RIGHT = 1; // toevoegen van linker- of rechterkind_x000D_
_x000D_
    // Constructors_x000D_
    public BinNode() {_x000D_
        this(null);_x000D_
    }_x000D_
_x000D_
    public BinNode(E userObject) {_x000D_
        parent = null;_x000D_
        leftChild = null;_x000D_
        rightChild = null;_x000D_
        this.userObject = userObject;_x000D_
    }_x000D_
_x000D_
    public String preOrderToString() {_x000D_
        buffer = new StringBuffer();_x000D_
        preOrder();            // roep recursieve methode aan_x000D_
        return buffer.toString();_x000D_
    }_x000D_
_x000D_
    private void preOrder() {_x000D_
        buffer.append(userObject.toString());  //bezoek van de node_x000D_
        if (leftChild != null) {_x000D_
            leftChild.preOrder();_x000D_
        }_x000D_
        if (rightChild != null) {_x000D_
            rightChild.preOrder();_x000D_
        }_x000D_
    }_x000D_
_x000D_
    public String postOrderToString() {_x000D_
        buffer = new StringBuffer();_x000D_
        postOrder();            // roep recursieve methode aan_x000D_
        return buffer.toString();_x000D_
    }_x000D_
_x000D_
    private void postOrder() {_x000D_
        if (leftChild != null) {_x000D_
            leftChild.postOrder();_x000D_
        }_x000D_
        if (rightChild != null) {_x000D_
            rightChild.postOrder();_x000D_
        }_x000D_
        buffer.append(userObject.toString());_x000D_
    }_x000D_
_x000D_
    public String inOrderToString() {_x000D_
        buffer = new StringBuffer();_x000D_
        inOrder();            // roep recursieve methode aan_x000D_
        return buffer.toString();_x000D_
    }_x000D_
_x000D_
    private void inOrder() {_x000D_
        if (leftChild != null) {_x000D_
            leftChild.inOrder();_x000D_
        }_x000D_
        buffer.append(userObject.toString());_x000D_
        if (rightChild != null) {_x000D_
            rightChild.inOrder();_x000D_
        }_x000D_
    }_x000D_
_x000D_
    public String levelOrderToString() {_x000D_
        buffer = new StringBuffer();_x000D_
        q = new LinkedList<BinNode<E>>();_x000D_
        q.add(this);_x000D_
        levelOrder();_x000D_
        return buffer.toString();_x000D_
    }_x000D_
_x000D_
    private void levelOrder() {_x000D_
        while (!q.isEmpty()) {_x000D_
            BinNode<E> knoop = q.remove();_x000D_
            buffer.append(knoop.userObject.toString());_x000D_
            if (knoop.leftChild != null) {_x000D_
                q.add(knoop.leftChild);_x000D_
            }_x000D_
            if (knoop.rightChild != null) {_x000D_
                q.add(knoop.rightChild);_x000D_
            }_x000D_
        }_x000D_
    }_x000D_
_x000D_
    public void add(BinNode<E> newChild) {_x000D_
        if (leftChild == null) {_x000D_
            insert(newChild, LEFT);_x000D_
        } else if (rightChild == null) {_x000D_
            insert(newChild, RIGHT);_x000D_
        } else {_x000D_
            throw new IllegalArgumentException(_x000D_
                    "Meer dan 2 kinderen");_x000D_
        }_x000D_
    }_x000D_
_x000D_
    public E get() {_x000D_
        return userObject;_x000D_
    }_x000D_
_x000D_
    public BinNode<E> getLeftChild() {_x000D_
        return leftChild;_x000D_
    }_x000D_
_x000D_
    public BinNode<E> getRightChild() {_x000D_
        return rightChild;_x000D_
    }_x000D_
_x000D_
    public BinNode<E> getParent() {_x000D_
        return parent;_x000D_
    }_x000D_
_x000D_
    public void insert(BinNode<E> newChild, int childIndex) {_x000D_
        if (isAncestor(newChild)) {_x000D_
            throw new IllegalArgumentException(_x000D_
                    "Nieuw kind is voorouder");_x000D_
        }_x000D_
        if (childIndex != LEFT_x000D_
                && childIndex != RIGHT) {_x000D_
            throw new IllegalArgumentException(_x000D_
                    "Index moet 0 of 1 zijn");_x000D_
        }_x000D_
        if (newChild != null) {_x000D_
            BinNode<E> oldParent = newChild.getParent();_x000D_
            if (oldParent != null) {_x000D_
                oldParent.remove(newChild);_x000D_
            }_x000D_
        }_x000D_
        newChild.parent = this;_x000D_
        if (childIndex == LEFT) {_x000D_
            leftChild = newChild;_x000D_
        } else {_x000D_
            rightChild = newChild;_x000D_
        }_x000D_
    }_x000D_
_x000D_
    public boolean isChild(BinNode<E> aNode) {_x000D_
        return aNode == null_x000D_
                ? false_x000D_
                : aNode.getParent() == this;_x000D_
    }_x000D_
_x000D_
    public boolean isAncestor(BinNode<E> aNode) {_x000D_
        BinNode<E> ancestor = this;_x000D_
        while (ancestor != null && ancestor != aNode) {_x000D_
            ancestor = ancestor.getParent();_x000D_
        }_x000D_
        return ancestor != null;_x000D_
    }_x000D_
_x000D_
    public void remove(BinNode<E> aChild) {_x000D_
        if (aChild == null) {_x000D_
            throw new IllegalArgumentException(_x000D_
                    "Argument is null");_x000D_
        }_x000D_
_x000D_
        if (!isChild(aChild)) {_x000D_
            throw new IllegalArgumentException(_x000D_
                    "Argument is geen kind");_x000D_
        }_x000D_
_x000D_
        if (aChild == leftChild) {_x000D_
            leftChild.parent = null;_x000D_
            leftChild = null;_x000D_
        } else {_x000D_
            rightChild.parent = null;_x000D_
            rightChild = null;_x000D_
        }_x000D_
    }_x000D_
_x000D_
    public String toString() {_x000D_
        return userObject.toString();_x000D_
    }_x000D_
}_x000D_
