1. Concept
1. Node
- Parent node
- Child node
- Sibling nodes: nodes of the same parent node
- Root node: a node without a parent node
- Leaf nodes: nodes without byte points
2 depth level
- Height: count from bottom to top
- Depth: Counting from top to bottom
- Floor:
3 Full Binary Tree Complete Binary Tree
Full binary tree:
- The leaf nodes are all at the bottom;
- Except for the leaf nodes, the rest of the nodes all contain two left and right nodes.
Complete binary tree: - Except for the last layer, the number of nodes in other layers must reach the maximum
- The last layer of leaf nodes are arranged to the left
4 Traversal of Binary Tree
Named according to the order of the root nodes
Pre-order traversal: root left and right
Middle-order traversal: left root right
Post-order traversal: left and right root
Second, the code implementation
Array-based sequential storage method
Application: complete binary tree,
method:
suppose that the root node is subscript i in the array, and i=1
the left child node of the root node: i * 2
the right child node of the root node: i * 2+1
Core code:
Prerequisite: The array data stores the data of a complete binary tree
///<summary>
///
///<param name="index"></param>
///</summary>
private void preOrder(int index)
{
if(index>=data.Count||data[index].IsNullOrEmpty()){
return ;
}
int num=index;
Console.write(data[index]+" ");
var leftNum=num*2;
var rightNum=num*2+1;
preOrder(leftNum);
preOrder(rightNum);
}