A computer science tree
Second, the definition of a binary tree
A tree, each node can have it at most two child nodes, this time is called a binary tree. (We usually see in the book questions the tree is a binary tree, but it does not mean that all trees are binary if more than two nodes, we also call multiple trees)
can be seen: full binary tree certain It is a complete binary tree; not necessarily a complete binary tree is a full binary tree.
If we give a binary tree add additional conditions, you can get a special method called binary tree binary search tree (binary search tree) is.
Binary search tree requirements: if its left subtree is not empty, then the value of the left sub-tree, all the nodes are less than the value of the root node; if its right subtree is not empty, then all the right subtree nodes which are greater than the value of the root; its left and right sub-trees are binary sort tree. [ Small left bottom, right side of the big top ]
Daily operations Third, a binary search tree
PS: binary tree lot, but for us, in practical application, the application binary sort tree more.
1. Insert a new node
Suppose we want to array a [] = {10, 5, 15, 6, 4, 16} build a binary sort tree, we insert elements sequentially one by one.
The insertion process is as follows:
If the tree is empty, create a new node, the new node as the root, so a binary search tree to the root node element 10 constructed for that.
- 5,5 insertion smaller than 10, the left child node 10 are compared, left child node 10 is empty, for insertion.
- Insert 15, 15 is greater than 10, were compared, 10 right child node is empty and the right child node 10, for insertion.
- 6,6 insertion than 10 hours, compared with the left child node 510; 6 is larger than 5, compared with the right child node 5, 5 right child is empty, for insertion.
- 4,4 insertion than 10 hours, compared with the left child node 510; less than 45, compared with the left child node 5, left child 5 is empty, insert.
- Insert 10 is greater than 16, 16, 10 compared to the right child of node 15; 16 larger than 15, compared with the right child nodes 15, 15 of the right child is empty insertion.
From this we can conclude that the process steps to insert a new element:
- Suitable elements to find the insertion position: the new element with the current node comparison, if the value is greater than the current node, then find the right subtree; otherwise subtrees left to find.
- After finding the insertion position, the value of the element in the construction of new node is inserted into binary sort tree.
2. balanced binary tree traversal
] [Baidu Encyclopedia balanced binary search tree, also known as AVL trees, and having the following properties: it is the absolute value of the difference between the height of the empty tree or a subtree left and right does not exceed 1, and the left and right subtrees It is a balanced binary tree.
Balanced binary tree traversal is that in some way one by one "visit" each node binary tree. for example:
Preorder traversal
- Data Access root node in
- Preorder traversal left subtree
- Preorder traversal right subtree
preorder traversal results:
1, 2, 4, 8, 9, 5, 10, 3, 6, 7
Preorder
- Preorder left subtree
- Data Access root node in
- Right subtree preorder
traversal sequence results:
8, 4, 9, 2, 10, 5, 1, 6, 3, 7
Postorder
- Postorder left subtree
- Postorder right subtree
- The root node to access data in
postorder traversal results:
8, 9, 4, 10, 5, 2, 6, 7, 3, 1
Traverse the level
- Data Access root node in
- Data of the second layer to access all the nodes
- All data in the third layer of the access node
- ......
levels through the results:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
3. Delete Node
Delete binary sort tree is a node in three ways:
- Nodes have been removed while the left subtree and the right subtree. The predecessor node value stored in the current node, then remove the precursor node.
- Node is deleted only the left subtree or only a right subtree. Direct replacement with truncated point subtree.
- No sub-tree nodes are removed. You can delete a node directly.
4. Find the value of the element
Minimum binary sort tree is located on its left-most node; maximum is at its rightmost node
my micro-channel public number: architecture Scriptures (id: gentoo666), shared Java dry, high concurrency programming, popular technical tutorials, micro distributed services and technology, architecture, design, block chain technology, artificial intelligence, big data, Java interview questions, and leading edge of the top news and so on. Updated daily Oh!
Fourth, references
https://blog.csdn.net/wannuoge4766/article/details/83998377
https://www.cnblogs.com/shixiangwan/p/7530015.html
https://www.cnblogs.com/ysocean/p/8032642.html
https://blog.csdn.net/u014634338/article/details/42465089
http://www.it610.com/article/3607922.htm