Data type: tree
Binary tree: A finite set of nodes. The set can be empty. If it is not empty, it is composed of the root node and two disjoint binary trees. The left subtree is called TL and the right subtree is called TR
These five situations are called binary trees:
-
1. Empty 2. Only A 3. AB 4. AC 5. ABC
-
There are also special names in several cases
- Oblique binary tree
- Full binary tree
- Complete binary tree
node
- Tree: a finite set of n(n>=0) nodes
- N=0 is called empty tree
- Every non-empty tree has a root node, Root, represented by r.
- The remaining nodes can be divided into m (m>0) disjoint finite sets
- Each set is a tree again, becoming the original SubTree
- Trees and non-trees: subtrees are disjoint. Except for the root node, each child node has one and only one parent node. A tree with N nodes has N-1 edges
Some basic terms
Degree of the node (Degree): the number of subtrees of the node
Tree degree: the largest degree among all nodes of the tree
Leaf node (leaf): node with degree 0
Parent node (Parent)
Child
Sibiling: Each node with the same parent node is a sibling node
Ancestor node: all nodes along the path from the root of the tree to a node
Descendant nodes: all nodes in the subtree of a node
The depth of the tree: the maximum level of nodes in the tree is the depth of the tree
Operations involved in a binary tree:
1.boolean isEmpty(BT tree), judge whether BT is empty
2. Void traversal (Bt tree), traverse in a certain order
3.BT createBT (), create a binary tree
The way to traverse the binary tree
Preorder traversal
- 1. Visit the root node first
- 2. Traverse the left subtree first
- 3. Traverse the right subtree first
In-order traversal
- 1. Traverse the left subtree in middle order
- 2. Visit the root node
- 3. Traverse the right subtree in middle order
Post-order traversal
- 1. Post-order access to the left subtree
- 2. Traverse the right subtree in post-order
- Visit the root node