Binary Tree (Java implementation)

A common language

1, the logical structure: describes the correlation between the logical data. Into linear structure (e.g., string), and non-linear structures (e.g., trees, graphs).

2, the physical structure: describes the data storage structure, structure into the sequence (e.g., arrays), and chain structure.

3, of node: the number of a node in the subtree (i.e. branched).

4, leaf nodes: also referred to as terminal nodes, refers to a node of degree zero.

5, the tree depth (height): the maximum level of the tree nodes.

6, ordered tree: sub-tree has divided the order of (my understanding is that the left and right node order can not be reversed).

7, with the structure: The appropriate rename nodes can have two identical tree

: 8, the child node direct successor of a node.

9, the parent node: a node's immediate predecessor.

Second, the concept of a binary tree

1, a binary tree: the following two conditions are satisfied is referred to as a binary tree

Of not more than 2 nodes ①

② order of child nodes nodes can not be reversed

2, a full binary tree: each have nodes are full, i.e., 2 I-. 1

3, complete binary tree: node 1 ~ n correspond to the full binary tree node 1 ~ n

4, complete binary tree properties:

 (1) If the node number is i (i> 1), the number of its parent node i / 2. (Here is divisible)

 (2) If the number of node i (i> = 1), it is the left child node number 2i.

 (3) If the serial number of the node i (i> = 1), then it is the right child node number 2i + 1.

Third, the binary tree operation

 1, the storage node binary tree structure

public class TreeNode {
    String data;
    TreeNode LChild;
    TreeNode RChild;
    TreeNode(String data) {
        this.data = data;
    }
    public String toString() {
        return data;
    }
}

 2. Create a binary tree

Before using preorder traversal create a binary tree is more appropriate, in accordance with the logic, the total must first create a root node in the left and right subtrees created, it can not yet create the root put root.LChild pass out of it.

Private  static String [] Tree = { "." "." "A", "B",,, "C", "D",,,. "". "". "" };
 Private  static  int I = 0 ;
 // first binary sequence is more appropriate to create 
TreeNode inOrderCreateBTree () {
    TreeNode bt = null;
    String s = tree[i++];
    if(s == ".") {
        return bt;
    }else {
        bt = new TreeNode(s);
        bt.LChild = inOrderCreateBTree();
        bt.RChild = inOrderCreateBTree();
        return bt;
    }
}

 By way of contribution can be non-recursive, but the difficulty is still getting bigger.

3, preorder

Recursively:

// traversing Binary Tree before recursion 
void preOrderPrintBTree (the TreeNode BT) {
     IF (BT == null ) {
        System.out.print("." + " ");
    }else {
        System.out.print(bt + " ");
        preOrderPrintBTree(bt.LChild);
        preOrderPrintBTree(bt.RChild);
    }
}

 Based on non-recursively stack:

Along with ideas like writing: in fact this is a template code three traversal code in the structure are similar.

① pointer to the leftmost descendant node, edge shift becomes printing while the stack (the stack to save the parent node, to access the right subtree).

while(p != null) {
    System.out.print(p + " ");
    stack.push(p);
    p = p.LChild;
    if(p == null) {
        System.out.print("." + " ");
    }
}

 ② stack is not empty, pop, p pointer pointing to the right subtree.

if(!stack.isEmpty()) {
    p = stack.pop();
    p = p.RChild;
    if(p == null) {
        System.out.print("." + " ");
    }
}

 Complete code:

// stack-based non-recursive method preorder binary tree 
void preOrderPrintBTree1 (the TreeNode BT) {
     IF (BT == null ) {
        System.out.println("null tree");
    }
    Stack<TreeNode> stack = new Stack<TreeNode>();
    TreeNode p = bt;
    while(!stack.isEmpty() || p != null) {
        while(p != null) {
            System.out.print(p + " ");
            stack.push(p);
            p = p.LChild;
            if(p == null) {
                System.out.print("." + " ");
            }
        }
        if(!stack.isEmpty()) {
            p = stack.pop();
            p = p.RChild;
            if(p == null) {
                System.out.print("." + " ");
            }
        }
    }
}

 4, preorder

 Recursion:

void inOrderPrint(TreeNode bt) {
    if(bt == null) {
        System.out.print("." + " ");
    } else {
        inOrderPrint(bt.LChild);
        System.out.print(bt + " ");
        inOrderPrint(bt.RChild);
    }
}

 Non-recursive method:

Preamble and preorder traversal is more or after printing the first stack former, the latter is pushed onto the stack before printing.

①p the pointer to the extreme left, the edge shift becomes stack.

while(p != null) {
    stack.push(p);
    p = p.LChild;
}

 ② the stack side edge printing, p pointer pointing to the right subtree

if(!stack.isEmpty()) {
    p = stack.pop();
    System.out.print(p + " ");
}

 Complete code:

void inOrderPrint1(TreeNode bt) {
    if(bt == null) {
        System.out.println(".");
    }else {
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode p = bt;
        while(!stack.isEmpty() || p != null) {
            while(p != null) {
                stack.push(p);
                p = p.LChild;
            }
            if(!stack.isEmpty()) {
                p = stack.pop();
                System.out.print(p + " ");
            }
        }
    }
}

 5, subsequent traversal

Recursion:

void postOrderPrint(TreeNode bt) {
    if(bt == null) {
        System.out.print("." + " ");
    }else {
        postOrderPrint(bt.LChild);
        postOrderPrint(bt.RChild);
        System.out.print(bt + " ");
    }
}

Non-recursive: the difficulty lies in the follow-up to traverse the node to know before a visit is the left child or right child, so to set up a pre-pointer pre.

 ①pcur the pointer to the extreme left, the stack side edge shift

 There are children ②pcur is null or is accessed, the access pcur, otherwise pcur to continue to push, pcur point to its right child.

Complete code:

void postOrderPrint1(TreeNode bt) {
    if(bt == null) {
        System.out.print("." + " ");
    }else {
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode pcur = bt;
        TreeNode pre = null;
        while(pcur != null) {
            stack.push(pcur);
            pcur = pcur.LChild;
        }
        while(!stack.isEmpty()) {
            pcur = stack.pop();
            
            if(pcur.RChild == null || pcur.RChild == pre) {
                System.out.print(pcur + " ");
                to = pcur;
            }else {
                stack.push(pcur);
                pcur = pcur.RChild;
                while(pcur != null) {
                    stack.push(pcur);
                    pcur = pcur.LChild;
                }
            }
        }
    }
}

 6. Other operating Binary Tree

(1) Print leaf node

// preorder traversal of the leaf node before printing 
public  static  void preprintLeaves (the TreeNode BT) {
     IF (BT == null ) {
        
    }else {
        if(bt.LChild == null && bt.RChild == null) {
            System.out.print(bt + " ");
        }
        preprintLeaves(bt.LChild);
        preprintLeaves(bt.RChild);
    }
}

 (2) Find the depth of the tree

// preorder traversal of the binary tree depth 
public  static  int preTreeDepth (BT the TreeNode, int H) {
     IF (BT =! Null ) {
         IF (H> depth) depth = H;
        preTreeDepth(bt.LChild,h+1);
        preTreeDepth(bt.RChild,h+1);
    }
    return depth;
}
// subsequent binary tree traversal depth 
public  static  int postTreeDepth (the TreeNode BT) {
     int HL = 0 ;
     int HR = 0 ;
     int max = 0 ;
     IF (BT == null ) {
         return 0 ;
    }else {
        hl = postTreeDepth(bt.LChild);
        hr = postTreeDepth(bt.RChild);
        max = Math.max(hr, hl);
        return max + 1;
    }
}

 

Guess you like

Origin www.cnblogs.com/mgblogs/p/11629959.html