Search in sources :

Example 6 with BinaryNode

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

the class TreeToListByLevel method transform.

/**
   * Iterative algorithm to resolve this problem. Binary tree traversal by level implemented with a
   * complexity order in time and space terms equivalent to O(N).
   */
public List<BinaryNode> transform(BinaryNode tree) {
    if (tree == null) {
        return Collections.EMPTY_LIST;
    }
    List<BinaryNode> nodesByLevel = new LinkedList<BinaryNode>();
    Queue<BinaryNode> stack = new LinkedList<BinaryNode>();
    stack.add(tree);
    while (!stack.isEmpty()) {
        BinaryNode node = stack.remove();
        nodesByLevel.add(node);
        if (node.getLeft() != null) {
            stack.add(node.getLeft());
        }
        if (node.getRight() != null) {
            stack.add(node.getRight());
        }
    }
    return nodesByLevel;
}
Also used : BinaryNode(com.github.pedrovgs.binarytree.BinaryNode) LinkedList(java.util.LinkedList)

Example 7 with BinaryNode

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

the class LowestCommonAncestorTest method shouldFindLCAWhenIsAFatherOfBothNodesIterative.

@Test
public void shouldFindLCAWhenIsAFatherOfBothNodesIterative() {
    BinaryNode<Integer> root = new BinaryNode<Integer>(1);
    BinaryNode<Integer> n2 = new BinaryNode<Integer>(2);
    BinaryNode<Integer> n3 = new BinaryNode<Integer>(3);
    BinaryNode<Integer> n4 = new BinaryNode<Integer>(4);
    BinaryNode<Integer> n5 = new BinaryNode<Integer>(5);
    BinaryNode<Integer> n6 = new BinaryNode<Integer>(6);
    root.setLeft(n2);
    root.setRight(n3);
    n2.setLeft(n4);
    n2.setRight(n5);
    n4.setLeft(n6);
    BinaryNode result = lca.getIterative(root, n5, n6);
    assertEquals(n2, result);
}
Also used : BinaryNode(com.github.pedrovgs.binarytree.BinaryNode) Test(org.junit.Test)

Example 8 with BinaryNode

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

the class BinaryTreePreOrder 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> getIterative(BinaryNode root) {
    validateBinaryNode(root);
    List<BinaryNode> result = new LinkedList<BinaryNode>();
    Stack<BinaryNode> stack = new Stack<BinaryNode>();
    stack.push(root);
    while (!stack.isEmpty()) {
        BinaryNode node = stack.pop();
        result.add(node);
        if (node.hasRight()) {
            stack.add(node.getRight());
        }
        if (node.hasLeft()) {
            stack.add(node.getLeft());
        }
    }
    return result;
}
Also used : BinaryNode(com.github.pedrovgs.binarytree.BinaryNode) LinkedList(java.util.LinkedList) Stack(java.util.Stack)

Example 9 with BinaryNode

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

the class BinaryTreeEquals method areEqualsIterative.

/**
   * Iterative solution for this algorithm. Using a tree traversal based on two parallel data
   * structures implemented using one Stack. The complexity order of this algorithm is O(N) in time
   * terms, as the previous one. But in space terms, the complexity order of this algorithm is O(N)
   * where N is the number of elements in the smallest tree because we are going to store this
   * elements in the stack.
   */
public boolean areEqualsIterative(BinaryNode<Integer> tree1, BinaryNode<Integer> tree2) {
    validateInput(tree1, tree2);
    boolean equals = true;
    Stack<BinaryNode> stack1 = new Stack<BinaryNode>();
    Stack<BinaryNode> stack2 = new Stack<BinaryNode>();
    stack1.push(tree1);
    stack2.push(tree2);
    while (!stack1.isEmpty()) {
        BinaryNode node1 = stack1.pop();
        BinaryNode node2 = stack2.pop();
        if (!node1.equals(node2)) {
            equals = false;
            break;
        }
        addNodeToStack(stack1, node1.getLeft());
        addNodeToStack(stack1, node1.getRight());
        addNodeToStack(stack2, node2.getLeft());
        addNodeToStack(stack2, node2.getRight());
    }
    return equals;
}
Also used : BinaryNode(com.github.pedrovgs.binarytree.BinaryNode) Stack(java.util.Stack)

Example 10 with BinaryNode

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

the class LowestCommonAncestorTest method shouldFindLCAWhenIsAFatherOfBothNodesRecursive.

@Test
public void shouldFindLCAWhenIsAFatherOfBothNodesRecursive() {
    BinaryNode<Integer> root = new BinaryNode<Integer>(1);
    BinaryNode<Integer> n2 = new BinaryNode<Integer>(2);
    BinaryNode<Integer> n3 = new BinaryNode<Integer>(3);
    BinaryNode<Integer> n4 = new BinaryNode<Integer>(4);
    BinaryNode<Integer> n5 = new BinaryNode<Integer>(5);
    BinaryNode<Integer> n6 = new BinaryNode<Integer>(6);
    root.setLeft(n2);
    root.setRight(n3);
    n2.setLeft(n4);
    n2.setRight(n5);
    n4.setLeft(n6);
    BinaryNode result = lca.getRecursive(root, n5, n6);
    assertEquals(n2, result);
}
Also used : BinaryNode(com.github.pedrovgs.binarytree.BinaryNode) Test(org.junit.Test)

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