Let's play with the traversal algorithm of binary tree [NO.1]

What is a tree structure?

Tree is a kind of non-linear data structure. Compared with linear data structure (linked list, array), the average running time of tree is shorter (the complexity of sorting time related to tree is not high)

So what is linear structure?

  • As the most commonly used data structure, linear structure is characterized by one-to-one linear relationship between data elements.
  • Linear structure has two different storage structures: sequential storage structure and chain storage structure. The linear table of sequential storage is called the sequential table, the storage elements in the sequential table are continuous (similar to array), the linear table of chain storage is called the linked table, the storage elements in the linked list are not necessarily continuous, and the address information of data elements and adjacent elements are stored in the element node.
  • There are two usage scenarios with limited operations in linear structure, namely queue and stack. The stack operation can only be performed at one end of the linear table, that is to say, FILO. The queue insertion operation is performed at one end of the linear table, while other operations are performed at the other end of the linear table, FIFO. There are two storage structures in the linear structure
    There are two implementations for this queue and stack.

Conclusion:
There are four types of sequence structure: sequence table (array), linked list, queue, stack (special linear structure, stack is a linear table whose operation is limited at the end of the table. The end of the table is called the top of the stack, and the other end is fixed, which is called the bottom of the stack)

A tree is a nonlinear structure. What is a tree like?


As a widely used one to many nonlinear data structure, the traditional tree has not only the direction relationship between data, but also the hierarchical relationship (parent-child relationship, leaf relationship is the biggest feature). Because the structure of the traditional tree is complex, in order to simplify the operation and storage, we generally transform the tree into a binary tree for processing

Structure and characteristics of binary tree


Conclusion:

  • A binary tree will have at least one node (root node)
  • Each node cannot have more than two child nodes. If the root node is removed, the remaining nodes will be divided into two disjoint subtrees, called left subtree and right subtree respectively

    Full two fork tree
    Full binary tree. As the name implies, all nodes except the leaf node have two children, and the leaf node is on the same layer. See figure a for an example.
    Complete binary tree
    A complete binary tree is a full binary tree after removing the last layer of nodes, and the nodes of the last layer are continuously concentrated on the leftmost side. See Figure b for an example.

To play binary tree, create a binary tree manually

A binary tree is composed of several nodes. A Node maintains a metadata and two pointers to other nodes, which are connected to form a binary tree.

Define a TreeNode class, representing the tree structure

**
 * Representative binary tree structure
 * 
 * @author Zhang Jiang Feng
 *
 */
public class TreeNode {

	// Left node of binary tree
	private TreeNode leftNode;

	// Right node of binary tree
	private TreeNode rightNode;

	// Current node data
	private int value;

	// Construction method assignment
	public TreeNode(int value) {
		super();
		this.value = value;
	}

}

The following figure is an example of binary tree creation

Create and connect nodes from the legend

public static void main(String[] args) {
		int[] Tarr = { 10, 8, 20, 15, 30 };

		TreeNode treeNode = new TreeNode(10);
		TreeNode treeNode2 = new TreeNode(8);
		TreeNode treeNode3 = new TreeNode(20);
		TreeNode treeNode4 = new TreeNode(15);
		TreeNode treeNode5 = new TreeNode(30);

		// Set root node, left son node
		treeNode.setLeftNode(treeNode2);
		// Set root node, right son node
		treeNode.setRightNode(treeNode3);
		// Set the left son node of 20 nodes
		treeNode3.setLeftNode(treeNode4);
		// Set the right son node of the 20 node
		treeNode3.setRightNode(treeNode5);

	}

Three ways of traversing binary tree

  • Preorder traversal

           Access the root node first, then the left node, and finally the right node (root - > left - > right)
    
  • Sequential traversal

            Access the left node first, then the root node, and finally the right node (left - > root - > right)
    
  • Post order traversal

            Access the left node first, then the right node, and finally the root node (left - > right - > root)
    

Recursion algorithm realizes traversal respectively

// Preorder traversal
	public static void befoTree(TreeNode tree) {

		if (tree != null) {

			if (tree.getValue() >= 0) {
				System.out.println(tree.getValue());
			}

			befoTree(tree.getLeftNode());
			befoTree(tree.getRightNode());

		}

	}

The output should be 10-8-20-15-30 according to the order of traversal (root - > left - > right)

// Sequential traversal
	public static void betTree(TreeNode tree) {

		if (tree != null) {

			betTree(tree.getLeftNode());

			System.out.println(tree.getValue());

			betTree(tree.getRightNode());

		}

	}

The output should be 8-10-15-20-30 according to the middle order traversal order (left - > root - > right)

// Post order traversal
	public static void afterTree(TreeNode tree) {

		if (tree != null) {
			afterTree(tree.getLeftNode());

			afterTree(tree.getRightNode());

			System.out.println(tree.getValue());

		}

	}

The output should be 8-15-30-20-10 according to the sequence of traversal (left - > right - > root)

The simple introduction of binary tree is completed, and other knowledge points of binary tree will be updated later. I hope that blog records can help me and everyone.

37 original articles published, 58 praised, 3956 visited
Private letter follow

Posted on Sun, 15 Mar 2020 05:16:25 -0400 by the_reverend