[] Tree and binary tree data structure Introduction

[] Tree and binary tree data structure


This blog to record the basic concepts of trees and binary trees.

This article reprinted from: Binary Tree - you may need to know these


1. What is the tree

Is a nonlinear tree data structure is a finite set of n (n> = 0) nodes thereof.
If n == 0, the tree is empty tree.
If n> 0,
the tree has a particular node, the root node. Only the root immediate successor, has no direct predecessor.
Other nodes other than the root node is divided into m (m> = 0) a finite set of disjoint, T0, T1, T2, ... , Tm-1, each binding a tree, called the root subtree node point.

2. Binary Tree

For such a tree data structure, the most frequently used data structure such that a binary tree.

2.1 What is a binary tree

Each node of the tree only a maximum of 2 child nodes is called a binary tree.

2.2 some of the features of a binary tree

A, at the i-th layer of the binary tree up to 2 ^ (i-1) th node (i> = 1).
B, k is the height of the binary tree, up to 2 ^ k-1 nodes (k> = 0).
C, any binary tree, if it has n leaf nodes, a degree of non-leaf nodes have m 2, then n = m + 1.
Complete binary tree of a height D, with n + junction. 1 is logN
E, For complete binary tree of n nodes, the nodes of the hierarchical numbered (top to bottom, left to right), for any number to node i:

2.3 storage to achieve binary tree


// 节点
public class BinaryNode {
    // 存放的信息
    Object data;
    // 左儿子
    BinaryNode left;
    // 右儿子
    BinaryNode right;
}

2.4 binary tree traversal

Traversal is a basic operation on trees, so-called binary tree traversal is that all nodes traveled binary tree according to certain rules and order, so that each node have been visited once, and only accessed once. Since a binary tree is a non-linear structure, therefore, traversing the tree is essentially converts the binary tree each node is represented as a linear sequence.

For traversing the tree, in the order of different root access, there are three main traversal algorithm:

  • Preorder
  • Postorder
  • Preorder

In addition to the above three basic way to navigate, as well as binary tree depth-first traversal and breadth-first traversal.

We first give a hypothesis:
L: left subtree
D: root
R: right subtree

2.4.1 preorder (DLR)

Preorder: root -> left sub-tree -> right subtree

public static void DLR(BinaryNode node) {
        // 访问根节点
        System.out.print(node.data + " ");
        // 遍历左子树
        if (node.left != null) {
            DLR(node.left);
        }
        // 遍历右子树
        if (node.right != null) {
            DLR(node.right);
        }
    }

2.4.2 preorder (LRD)

Postorder: left sub-tree -> right subtree -> root


public static void LRD(BinaryNode node) {
        // 遍历左子树
        if (node.left != null) {
            LRD(node.left);
        }
        // 遍历右子树
        if (node.right != null) {
            LRD(node.right);
        }
        // 访问根节点    
        System.out.print(node.data + " ");
    }

2.4.3 inorder traversal (LDR)

Preorder: left subtree -> root -> right subtree

          // 遍历左子树
        if (node.left != null) {
            LDR(node.left);
        }
        // 访问根节点
        System.out.print(node.data + "");

        // 遍历右子树
        if (node.right != null) {
            LDR(node.right);
        }

2.4.4 depth-first traversal

That is the abbreviation for the DFS Depth First Search. The procedure is brief for each possible path deep into the branch can not go any further so far, and each node can only be accessed once.

Depth-first traversal requires the use of this data structure on the stack, the stack having the advanced features.

As shown above, we have to analyze the process under the depth-first traversal.

  • A root node is first stack, stack (A).
  • The node A pop-up, because there are two child nodes A BC, definition and characteristics of the stack, the first C (the right son) pushed onto the stack, then the B (left son) pushed onto the stack, stack (CB)
  • Pop pop top element Node B, D and E to node pushed onto the stack, stack (CED).
  • Pop the top element D, node D since there is only one child node H, H therefore directly stack, stack (CEH).
  • Pop the top element H, the element H child element is not present, stack (CE).
  • Pop the top element E, the element E is not present sub-elements, stack (C).
  • Pop stack elements C, subsection node GF respectively stack, stack (GF).
  • F the stack, stack (G).
  • G a stack, stack ().
  • Traverse end.
    Depth-first traversal of results: ABDHECF G.

Through the above analysis, is not that depth-first traversal difficulty, in fact not so, let's take a hands-implement the code (a clear understanding of the process, the code is simple to implement).


private void depthFirst(AVLTreeNode<T> node) {
        if (node == null) {
            return;
        }

        Stack<AVLTreeNode> stack = new Stack<>();
        // 根节点入栈,然后进行后续操作
        stack.push(node);

        while (!stack.isEmpty()) {
            AVLTreeNode root = stack.pop();
            // 弹出栈顶元素,进行访问。
            System.out.println(root.key + " ");
            // 首先将右节点入栈
            if (root.right != null) {
                stack.push(node.right);
            }
            // 然后左节点入栈
            if (root.left != null) {
                stack.push(node.left);
            }
        }

    }

2.4.5 breadth-first traversal

English abbreviation for the BFS namely Breadth FirstSearch. The procedure is followed by access to the test each layer node, complete access layer to the next level, and each node can only be accessed once. For the above example, the breadth-first traversal of the result is: A, B, C, D, E, F, G, H (assuming each access node from left to right).

First traversal need to queue such data structure, having a FIFO queue characteristics.

As shown above, we analyze the breadth-first traversal process.

A first node into the queue, Queue (A);
the popup node A, while the sub Node B A, C is inserted into the queue, the first queue in case B, C in the tail of the queue, Queue (B, C);
node B will pop up, while the B sub-nodes D, E inserted in the queue, the first queue in case C, E at the tail of the queue, queue (C, D, E);
the popup node C, while node C the child F, G inserted into the queue, the first queue in case D, G in the tail of the queue, queue (D, E, F, G);
the popup node D, and node D is a child node H is inserted into the queue, then E first in the queue, H at the tail of the queue, queue (E, F., G, H);
EFGH are ejected (the four nodes child nodes do not exist).
Breadth-first traversal results: ABCDEFGH

Hands to achieve the breadth-first traversal, code is as follows:


public void breadthFirst() {
        breadthFirst(root);
    }

    private void breadthFirst(AVLTreeNode<T> node) {
        if (node == null) {
            return;
        }

        Queue<AVLTreeNode> queue = new ArrayDeque<>();
        // 根节点入栈
        queue.add(node);

        while (!queue.isEmpty()) {
            AVLTreeNode root = queue.poll();
            System.out.print(node.key + " ");

            if (root.left != null) {
                queue.add(node.left);
            }

            if (root.right != null) {
                queue.add(node.right);
            }

        }


    }

2.5 full Nimata 树

Definition: Assuming that binary tree depth is h, h except the first layer, the other layers (1 ~ h-1) has reached the maximum number of nodes, the h layer all nodes are continuously concentrated in the left, which is complete binary tree.

Complete binary tree has the following characteristics:

  • Allowing only the last layer of vacancies and vacancies node on the right, that is, leaf nodes can only occur at the maximum level two.
  • For any node, if the depth of its right subtree is j, the depth of which will be left subtree is j or j + 1. I.e., only one point of 1 or 0.

2.6 Full Binary Tree

Except the last one without any child node, all the nodes on each layer has two child node of the binary tree.
Domestic Tutorial definition: a binary tree, if the number of nodes in each layer have reached the maximum, then the binary tree is a full binary tree. That is, if a binary number of layers is K, and the total number of nodes is (2 ^ k) -1, it is a full binary tree.

Full binary tree is a special kind of complete binary tree.

Guess you like

Origin www.cnblogs.com/54chensongxia/p/11567515.html