Binary tree storage

Sequential storage

Using arrays for storage is
more suitable for complete binary trees
Insert picture description here
. The sequence number of the parent node of node i is i/2. The sequence number of
the left child node of the non-root node node i is 2i (2i<=n, otherwise there is no left child node. Point)
The sequence number of the right child node of node i is 2i+1 (2i+1<=n, otherwise there is no right child node)

For an incomplete binary tree, the position without nodes can be recorded as empty

Linked list storage

For general binary trees, the use of sequential storage will cause a waste of space

Chain storage is the use of linked lists for storage
Insert picture description here

typedef struct TreeNode *BinTree;
typedef BinTree Position;
struct TreeNode
{
    
    
	int data;
	BinTree left;
	BinTree right;
};

Guess you like

Origin blog.csdn.net/m0_54621932/article/details/114104255