Data Structure Handwriting Tree Traversal (Full Implementation)

Keywords: less Java MySQL Database

1. Summary of Previous Situation
This is a tree traversal implementation. To focus on how queues are implemented, follow these articles:
Data structure (3) Handwritten queues
2. Basic concepts of trees
Chain lists, stacks, and queues are one-to-one linear structures, one-to-many linear structures - trees."One-to-many" means that an element can only have one precursor but can have multiple successors.
The node definition of a tree is shown in the following figure

b Full Binary Tree
In a binary tree, if all branch nodes have left and right subtrees and all leaves are on the same level, such a binary tree is called a full binary tree.
Full binary trees have the following characteristics:
Leaves can only appear on the bottom layer
Non-leaf nodes must have a degree of 2
In the same depth of binary tree, the full binary tree has the most nodes and leaves.
c Complete Binary Tree
If the height of a binary tree is h, the maximum number of nodes in all layers (1-h-1) except the h-level will be reached. The h-level has leaf nodes, and the leaf nodes are arranged from left to right in order. This is a complete binary tree.
Features of a complete binary tree:
Leaf nodes can only appear on the bottom two layers
Lowest leaf on left and continuous
A binary tree with the same number of nodes has the smallest depth of a complete binary tree

d-balanced binary tree
Balanced binary tree, also known as AVL tree (different from AVL algorithm), is a binary sorting tree with the following properties:
It is an empty tree or its left and right subtrees have absolute height differences of no more than 1, and both subtrees are balanced binary trees
The non-leaf node value is greater than the left child node and less than the right child node.
There are no nodes with equal duplicate values;

e Red-Black Tree
Because the balanced binary tree query performance is inversely proportional to the tree hierarchy (h-height), the smaller the h-value, the faster the query. In order to ensure a general balance of data at the left and right ends of the tree structure and reduce the query difficulty of the binary tree, an algorithm mechanism is generally used to achieve the balancing of node data structure, and this algorithm has a red-black tree.Red-black tree is widely used, mainly to store ordered data. Its time complexity is O(lgn), and its efficiency is very high.For example, TreeSet and TreeMap in a Java collection.

h B+Tree
B+Tree makes full use of the space of nodes to make the query speed more stable, which is completely close to the dichotomy search speed.For example, MySQL's database index lookup.
The non-leaf nodes of the B+tree do not store pointers for keyword records, which greatly increases the number of keywords that each node of the B+tree can store.
B+ leaf node stores all keyword and keyword records pointer of parent node, keyword of each leaf node from small to large link;
The number of root node keywords of B+tree is equal to the number of its child nodes.
B+ non-leaf nodes only index data, do not store the actual keyword record pointer, all data addresses must be to the leaf node to get, so the number of data queries is the same;

3. Tree traversal using stacks and queues
This paper mainly uses non-recursive method to achieve the first, middle and last order traversal and hierarchical traversal of binary tree. The first, middle and last order traversal is mainly achieved by the structure of stack, and the hierarchical traversal is achieved by the queue structure. Writing this article is also a summary and record of learning, which can be used in work and interviews.

/**
 * Class: Tree traversal (prefix/middle/postorder traversal->traverse using stack hierarchy->utilize queue structure)
 *
 * @Author: wangzhong
 * @Date: 2020-02-11 00:28
 */
public class TreeNodeTraverse {

	/**
	 * Non-recursive prefix traversal: root node->left child node->right child node
	 */
	public static void preTraverse(TreeNode<Integer> head) {
		if (head != null) {
			Stack<TreeNode<Integer>> stack = new Stack<TreeNode<Integer>>();
			stack.push(head);
			while (!stack.isEmpty()) {
				TreeNode<Integer> treeNode = stack.pop();
				System.out.print(treeNode.getItem() + " ");
				//First put in the right subtree
				if (treeNode.right != null) {
					stack.push(treeNode.right);
				}
				//Place Left Subtree
				if (treeNode.left != null) {
					stack.push(treeNode.left);
				}
			}
		}
	}

	/**
	 * Non-recursive middle traversal: left sub-node->root node->right sub-node (idea: stack the left sub-tree in the stack until no later pop-up, then press into the right sub-tree)
	 *
	 * @param head
	 */
	public static void midTraverse(TreeNode<Integer> head) {
		if (head != null) {
			Stack<TreeNode<Integer>> stack = new Stack<TreeNode<Integer>>();
			while (!stack.isEmpty() || head != null) {
				if (head != null) {
					stack.push(head);
					head = head.left;
				} else {
					head = stack.pop();
					System.out.print(head.getItem() + " ");
					head = head.right;
				}

			}
		}
	}

	/**
	 * Non-recursive sequential traversal of left subnode - > right subnode - > root node
	 */
	public static void postTraverse(TreeNode<Integer> head) {
		if (head != null) {
			Stack<TreeNode<Integer>> stack1 = new Stack<TreeNode<Integer>>();
			Stack<TreeNode<Integer>> stack2 = new Stack<TreeNode<Integer>>();
			stack1.push(head);
			while (!stack1.isEmpty()) {
				TreeNode<Integer> pop = stack1.pop();
				stack2.push(pop);
				if (pop.left != null) {
					stack1.push(pop.left);
				}
				if (pop.right != null) {
					stack1.push(pop.right);
				}
			}

			while (!stack2.isEmpty()) {
				System.out.print(stack2.pop().getItem() + " ");
			}

		}
	}

	/**
	 * Hierarchical traversal traverses by height using queue structure
	 *
	 * @param head
	 */
	public static void leveLTraverse(TreeNode<Integer> head) {
		if (head != null) {
			Queue<TreeNode<Integer>> queue = new ArrayDeque<TreeNode<Integer>>();
			//Put in
			queue.offer(head);
			while (!queue.isEmpty()) {
				int levelNum = queue.size();
				for (int i = 0; i < levelNum; i++) {
					TreeNode<Integer> pop = queue.poll();
					System.out.print(pop.getItem() + " ");
					if (pop.left != null) {
						queue.offer(pop.left);
					}
					if (pop.right != null) {
						queue.offer(pop.right);
					}
				}

			}
		}
	}

	/**
	 *  test
	 *          5
	 *       1       4
	 *    3       8      7
	 * 18      9    20
	 *
	 * Pre-order traversal: 51 3 18 4 8 9 20 7
	 * Medium order traversal: 18 3 1 5 9 8 20 4 7
	 * Post-order traversal: 18 3 1 9 20 8 7 4 5
	 * @param args
	 */
	public static void main(String[] args) {
		TreeNode<Integer> a=new TreeNode<Integer>(5);
		TreeNode<Integer> b=new TreeNode<Integer>(1);
		TreeNode<Integer> c=new TreeNode<Integer>(4);
		TreeNode<Integer> d=new TreeNode<Integer>(3);
		TreeNode<Integer> e=new TreeNode<Integer>(8);
		TreeNode<Integer> f=new TreeNode<Integer>(7);
		TreeNode<Integer> j=new TreeNode<Integer>(18);
		TreeNode<Integer> k=new TreeNode<Integer>(9);
		TreeNode<Integer> l=new TreeNode<Integer>(20);
		a.left=b;
		a.right=c;
		b.left=d;
		d.left=j;
		c.left=e;
		e.left=k;
		e.right=l;
		c.right=f;
		preTraverse(a);
		System.out.println();
		midTraverse(a);
		System.out.println();
		postTraverse(a);
		System.out.println();
		leveLTraverse(a);
	}

}
Four original articles were published, 0 were praised, 41 were visited
Private letter follow

Posted by amrigo on Mon, 10 Feb 2020 21:47:25 -0800