Binary Tree of DS
A binary tree is another tree-type structure. It features that each node has at most two Subtrees (that is, a node with a degree greater than 2 in a binary tree), and, the subtree of a binary tree can be left or right, and its order cannot be reversed.
Binary trees can be divided into five basic forms:
(1) Empty binary tree.
(2) binary tree with only root nodes.
(3) The left subtree is a null binary tree.
(4) The right subtree is an empty binary tree.
(5) Left and Right Subtrees are non-empty Binary Trees.
The figure below shows:
The next step is to draw the basic form of a binary tree with three nodes:
Full Binary Tree
A binary tree with a depth of k and A (2 ^ K)-1 node is called a full binary tree.
Full Binary Tree
A binary tree with a depth of k and n nodes. if and only when each node is one-to-one with a number ranging from 1 to n in a full binary tree with a depth of k, it is called a Complete Binary Tree.
The diagram of these two special Binary Trees:
The depth of full binary tree (a) is 4, and the number of knots on each layer is the maximum number of knots. Number this full binary tree in a row, starting from the root node, from top to bottom, from left to right.
For the depth of the full binary tree (B) is 4, the leaf node may only appear on the maximum two layers of the hierarchy. For any node, if the maximum layer of the child under the right branch is l, the maximum layer of the left branch must be l or l + 1.
Binary Tree nature:
(1) at most 2 ^ (I-1) (I> = 1) nodes on the I layer of the binary tree.
(2) A binary tree with a depth of k has at most (2 ^ k)-1 (k> = 1) nodes.
(3) For any binary tree T, if the number of terminal nodes is n0 and the number of nodes with the degree of 2 is n2, n0 = n2 + 1.
(4) the depth of the Complete Binary Tree with n nodes is [log2n] + 1, where [log2n] indicates taking the integer part of log2n.
(5) If you have A Complete Binary Tree with n nodes (its depth is [log2n] + 1) node number (from layer 1st to layer [log2n] + 1, each layer from left to right), then to any node I (1 <= I <= n ), include:
1 If I = 1, then node I is the root of the Complete Binary Tree, without PARENT; if I> 1, its PARENT (I) is the node [I/2].
2 If 2i> n, node I has no left child (node I is a leaf node); otherwise, the left child LCHILD (I) is node 2i.
3 if 2i + 1> n, node I has no right child; otherwise, its right child RCHILD (I) is node 2i + 1.
Binary Tree Storage Structure
Sequential Storage Structure
According to the definition of the sequential storage structure, in this Convention, the node elements of a complete binary tree are stored from left to right using a set of continuous storage units at a time from top to bottom, the node elements that are about to be completely Binary Tree numbered I are stored in the components labeled as I-1 in the one-dimensional array defined above.
Sequential Storage Structure Representation:
<span style="font-size:18px;">#define MAX_TREE_SIZE 100typedef char TElemtype;TElemtype SqBiTree[MAX_TREE_SIZE];</span>
The sequential storage structure of the above full binary tree and full binary tree is as follows:
For the ordered storage structure of a common binary tree, "null" nodes are required.
For the preceding binary tree, the sequential storage number 0 or character ^ represents a null node. In the worst case, a single decision tree with a depth of k and only k nodes (a node with a degree of 2 in the tree does not exist) needs to have a length of (2 ^ k) -1.
Chained Storage Structure
According to the definition of a binary tree, the node of the binary tree is composed of a Data Element and two branches pointing to the left and right sub-trees respectively, therefore, the nodes in the chain store of Binary Trees contain at least three fields: Data Field, left and right pointer fields. The most common one is the binary linked list storage structure of Binary Trees.
Node Structure of the binary linked list
Binary linked list
To facilitate access to the parent nodes of a node, you can add a parent field parent to the linked list node to point to the parent node. Each node is composed of four fields. The node structure is as follows:
This storage structure is convenient for finding child nodes and parent nodes. However, compared with the binary linked list storage structure, it increases the space overhead. The chain storage structure of a binary tree represented by such a node structure is called a three-way linked list.
Let's look at the binary linked list storage representation of the most commonly used binary tree:
<Span style = "font-size: 18px;"> typedef struct BiTNode // redefine the structure of the Binary Tree binary linked list {TElemType data; struct BiTNode * lchild, * rchild; // left and right child pointer} BiTNode, * BiTree; </span>
Basic operations for Binary linked list:
<Span style = "font-size: 18px;"> Status createBiTree (BiTree & T) enters the value (one character) of the binary tree's forehead node in the FIFO order ), # The character represents an empty character to construct a binary tree TStatus PreOrderTraverse (BiTree T, Status (* Visit) (TElemType e) represented by a binary linked list using a binary linked list storage structure, visit is an application function for node operations that first traverses binary tree T and calls Visit once for each node. Once only one Visit () operation fails, the operation fails. Status InOrderTraverse (BiTree T, status (* Visit) (TElemType e) adopts a binary linked list storage structure. Visit is an application function for node operations that traverses binary tree T sequentially, once a Visit () operation fails, the operation fails. Status PostOrderTraverse (BiTree T, Status (* Visit) (TElemType e )) using the binary linked list storage structure, Visit is the application function for node operation that traverses binary tree T sequentially. Visit is called once for each node and once only once the Visit () operation fails, the operation fails. Status LeveleOrderTraverse (BiTree T, Status (* Visit) (TElemType e) adopts the binary linked list storage structure. Visit is the application function sequence traversal binary tree T for node operations, call Visit once for each node and once only once the Visit () operation fails, the operation fails </span>