Search in sources :

Example 1 with BinaryNode

use of com.github.pedrovgs.binarytree.BinaryNode in project Algorithms by pedrovgs.

the class LowestCommonAncestor method getRecursiveInner.

private BinaryNode getRecursiveInner(BinaryNode root, BinaryNode n1, BinaryNode n2) {
    if (root == null) {
        return null;
    } else {
        if (root == n1 || root == n2) {
            return root;
        }
        BinaryNode leftBranch = getRecursiveInner(root.getLeft(), n1, n2);
        BinaryNode rightBranch = getRecursiveInner(root.getRight(), n1, n2);
        if (leftBranch != null && rightBranch != null) {
            return root;
        }
        return leftBranch != null ? leftBranch : rightBranch;
    }
}
Also used : BinaryNode(com.github.pedrovgs.binarytree.BinaryNode)

Example 2 with BinaryNode

use of com.github.pedrovgs.binarytree.BinaryNode in project Algorithms by pedrovgs.

the class BinaryTreeByLevel method getUsingQueue.

/**
   * Add implementation based on an additional data structure, one queue which implementation is a
   * LinkedList. We we are going to do is add elements of the tree to the queue and one by one
   * evaluate it adding more binary nodes to the queue if exist. The complexity order in time terms
   * is O(N) where N is the number of elements in the tree. The complexity order in space terms is
   * O(N) where N is the number of elements in the tree because we are going to store every node in
   * a queue.
   */
public List<BinaryNode> getUsingQueue(BinaryNode root) {
    validateBinaryNode(root);
    List<BinaryNode> result = new LinkedList<BinaryNode>();
    Queue<BinaryNode> queue = new LinkedList<BinaryNode>();
    queue.add(root);
    while (!queue.isEmpty()) {
        BinaryNode binaryNode = queue.remove();
        result.add(binaryNode);
        if (binaryNode.getLeft() != null)
            queue.add(binaryNode.getLeft());
        if (binaryNode.getRight() != null)
            queue.add(binaryNode.getRight());
    }
    return result;
}
Also used : BinaryNode(com.github.pedrovgs.binarytree.BinaryNode) LinkedList(java.util.LinkedList)

Example 3 with BinaryNode

use of com.github.pedrovgs.binarytree.BinaryNode in project Algorithms by pedrovgs.

the class BinaryTreeInOrder method getIterative.

/**
   * Iterative implementation of this binary tree traversal. The complexity order in time terms of
   * this algorithm is O(N) where N is the number of nodes in the tree. In space terms the
   * complexity order of this algorithm is also O(N) where N is the number of nodes we have to
   * store in the auxiliary data structure, the stack.
   */
public List<BinaryNode<Integer>> getIterative(BinaryNode<Integer> root) {
    validateBinaryNode(root);
    List<BinaryNode<Integer>> result = new LinkedList<BinaryNode<Integer>>();
    Stack<BinaryNode> stack = new Stack<BinaryNode>();
    //Define a pointer to track nodes
    BinaryNode current = root;
    while (!stack.empty() || current != null) {
        if (current != null) {
            //If it is not null, push to stack and go down the tree to left
            stack.push(current);
            current = current.getLeft();
        } else {
            //If no left child pop stack, process the node then let current point to the right
            BinaryNode node = stack.pop();
            result.add(node);
            current = node.getRight();
        }
    }
    return result;
}
Also used : BinaryNode(com.github.pedrovgs.binarytree.BinaryNode) LinkedList(java.util.LinkedList) Stack(java.util.Stack)

Example 4 with BinaryNode

use of com.github.pedrovgs.binarytree.BinaryNode in project Algorithms by pedrovgs.

the class BinaryTreePostOrder method getIterative.

public List<BinaryNode> getIterative(BinaryNode root) {
    validateTree(root);
    List<BinaryNode> result = new LinkedList<BinaryNode>();
    Stack<BinaryNode> stack = new Stack<BinaryNode>();
    stack.push(root);
    BinaryNode prev = null;
    while (!stack.empty()) {
        BinaryNode current = stack.peek();
        //keep going down
        if (prev == null || prev.getLeft() == current || prev.getRight() == current) {
            //prev == null is the situation for the root node
            if (current.getLeft() != null) {
                stack.push(current.getLeft());
            } else if (current.getRight() != null) {
                stack.push(current.getRight());
            } else {
                stack.pop();
                result.add(current);
            }
        //Go up the tree from left node need to check if there is a right child
        //if yes, push it to stack otherwise, process parent and pop stack
        } else if (current.getLeft() == prev) {
            if (current.getRight() != null) {
                stack.push(current.getRight());
            } else {
                stack.pop();
                result.add(current);
            }
        //Go up the tree from right node after coming back from right node, process parent node
        //and pop stack.
        } else if (current.getRight() == prev) {
            stack.pop();
            result.add(current);
        }
        prev = current;
    }
    return result;
}
Also used : BinaryNode(com.github.pedrovgs.binarytree.BinaryNode) LinkedList(java.util.LinkedList) Stack(java.util.Stack)

Example 5 with BinaryNode

use of com.github.pedrovgs.binarytree.BinaryNode in project Algorithms by pedrovgs.

the class LowestCommonAncestor method getIterative.

/**
   * Iterative solution for this problem. The complexity order of this algorithm is the same in
   * memory terms, O(N), but is worst in space terms because we are using a list of BinaryNodes
   * that is going to suppose O(N) for space terms where N is the path to the element.
   *
   * This algorithm is based on find the path of two nodes given a root node and compare both paths
   * to find the first non common element and return the previous node, the lowest common ancestor.
   */
public BinaryNode getIterative(BinaryNode root, BinaryNode n1, BinaryNode n2) {
    validateInput(root, n1, n2);
    List<BinaryNode> pathToA = getPathTo(root, n1);
    List<BinaryNode> pathToB = getPathTo(root, n2);
    BinaryNode result = null;
    int size = Math.min(pathToA.size(), pathToB.size());
    for (int i = 0; i < size; i++) {
        if (pathToA.get(i) != pathToB.get(i)) {
            result = pathToA.get(i - 1);
            break;
        }
    }
    return result;
}
Also used : BinaryNode(com.github.pedrovgs.binarytree.BinaryNode)

Aggregations

BinaryNode (com.github.pedrovgs.binarytree.BinaryNode)11 LinkedList (java.util.LinkedList)5 Stack (java.util.Stack)4 Test (org.junit.Test)3 BinaryTreeInOrder (com.github.pedrovgs.problem15.BinaryTreeInOrder)1