Data structure and detailed solution of binary tree substructure in algorithm

  • 2020-05-19 05:16:42
  • OfStack

Data structure and detailed solution of 2 - tree substructure in algorithm

demand

Enter two two-fork trees, A and B, and determine if B is a substructure of A. (ps: we agree that an empty tree is not a substructure of any tree.)

Tree description:


class TreeNode {
  int val = 0;
  TreeNode left = null;
  TreeNode right = null;

  public TreeNode(int val) {
    this.val = val;

  }
}

solution

Using the element into the stack, and constantly pop elements, pop-up one element, joining together into a string, using special symbols to distinguish, this method is mainly according to the first sequence traversal of the tree node joining together as a string data information, so, two tree node joining together is in the judgement of string contains.

However, there are some sources that say you can do this recursively, but I feel and practice later found that is wrong. The code will be presented later and the reader will try it out.


public static boolean HasSubtree2(TreeNode root1, TreeNode root2) {

    if (root2 == null)
      return false;
    String str = "";
    Stack<TreeNode> stack = new Stack<TreeNode>();
    stack.push(null);
    stack.push(root1);
    TreeNode node = null;
    while ((node = stack.pop()) != null) {
      str += '_' + node.val + '_';

      if (node.right != null) {
        stack.push(node.right);
      }
      if (node.left != null) {
        stack.push(node.left);
      }
    }

    String str2 = "";
    node = null;
    stack.push(null);
    stack.push(root2);
    while ((node = stack.pop()) != null) {
      str2 += '_' + node.val + '_';

      if (node.right != null) {
        stack.push(node.right);
      }
      if (node.left != null) {
        stack.push(node.left);
      }
    }

    if (str.contains(str2)) {
      return true;
    } else {
      return false;
    }
  }

The construction of a tree

For a two-fork tree, it can be stored by means of an array. The first node is placed at position 0 of the array, the left node is at position 1, and the right node is at position 2. Thus, the mapping relationship of index is:

index_parent.left = > 2* index_parent + 1;
index_parent.right= > 2* index_parent + 2;

The construction idea is that the left node and the right node are respectively constructed. The left node of the root node traces its children directly to 1, and the right node of the root node traces its children directly to 1. Thus, a recursive structure is formed.

The code is as follows:

Note: here the array is distinguished by -1 and can be expanded by the reader.


public static TreeNode getTree(int[] node, int index) {

    if (index >= node.length)
      return null;
    TreeNode n = null;
    if (node[index] != -1) {
      n = new TreeNode(node[index]);
      n.left = getTree(node, index * 2 + 1);
      n.right = getTree(node, index * 2 + 2);
    }
    return n;
  }

The complete code

Includes the code provided in the documentation, but tests show that it is incorrect in the following use case, but in theory tree2 should be a substructure of tree1.


import java.util.Stack;

public class HasSubtree {

  public static void main(String[] args) {

    TreeNode tree = getTree(new int[] { 8, 8, 7, 9, 2, -1, -1, -1, -1, 4, 7 }, 0);
    TreeNode tree2 = getTree(new int[] { 2, 4, 7 }, 0);
    boolean bool = HasSubtree(tree, tree2);
    System.out.println(bool);

    boolean bool2 = HasSubtree2(tree, tree2);
    System.out.println(bool2);
  }

  public static boolean HasSubtree2(TreeNode root1, TreeNode root2) {

    if (root2 == null)
      return false;
    String str = "";
    Stack<TreeNode> stack = new Stack<TreeNode>();
    stack.push(null);
    stack.push(root1);
    TreeNode node = null;
    while ((node = stack.pop()) != null) {
      str += '_' + node.val + '_';

      if (node.right != null) {
        stack.push(node.right);
      }
      if (node.left != null) {
        stack.push(node.left);
      }
    }

    String str2 = "";
    node = null;
    stack.push(null);
    stack.push(root2);
    while ((node = stack.pop()) != null) {
      str2 += '_' + node.val + '_';

      if (node.right != null) {
        stack.push(node.right);
      }
      if (node.left != null) {
        stack.push(node.left);
      }
    }

    if (str.contains(str2)) {
      return true;
    } else {
      return false;
    }
  }

  public static TreeNode getTree(int[] node, int index) {

    if (index >= node.length)
      return null;
    TreeNode n = null;
    if (node[index] != -1) {
      n = new TreeNode(node[index]);
      n.left = getTree(node, index * 2 + 1);
      n.right = getTree(node, index * 2 + 2);
    }
    return n;
  }

  public static boolean HasSubtree(TreeNode root1, TreeNode root2) {

    boolean result = false;
    if (root1 != null && root2 != null) {

      if (root1.val == root2.val) {
        result = isSubTree(root1, root2);
      }

      if (!result) {
        result = isSubTree(root1.left, root2);
      }

      if (!result) {
        result = isSubTree(root1.right, root2);
      }
    }

    return result;
  }

  private static boolean isSubTree(TreeNode root1, TreeNode root2) {

    if (root1 == null)
      return false;
    if (root2 == null)
      return true;
    if (root1.val != root2.val)
      return false;

    return isSubTree(root1.left, root2.left)
        && isSubTree(root1.right, root2.right);
  }

}

class TreeNode {
  int val = 0;
  TreeNode left = null;
  TreeNode right = null;

  public TreeNode(int val) {
    this.val = val;

  }

}

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: