[Data structure] Implementation of binary tree chain structure (3)

Table of contents

1. Chain structure of binary tree

2. Interface implementation of binary chain

        1. Creation of binary chain

        2. Interface function

        3. Dynamically create new nodes

        4. Create a binary tree

        5. Preorder traversal

        6. In-order traversal

        7. Post-order traversal

Third, the number and height of nodes, etc.

        1. Interface function

        2. Number of nodes

        3. Number of leaf nodes

        4. Binary tree height

        5. Number of nodes at the kth level of the binary tree

        6. Binary tree searches for the node with value x


1. Chain structure of binary tree

The linked storage structure of a binary tree refers to using a linked list to represent a binary tree, that is, using a chain to indicate the logical relationship of elements;

The usual method is that each node in the linked list consists of three fields, the data field and the left and right pointer fields. The left and right pointers are used to give the storage addresses of the link points where the left child and right child of the node are located respectively.

Chain structures are divided into binary chains and trifurcated chains. Here we study binary chains;

 A binary tree is:

1. Empty tree

2. Non-empty: the root node, the left subtree of the root node, and the right subtree of the root node.

As can be seen from the diagram, the definition of a binary tree is recursive , also called a recursive tree , so the basic post-order operations are basically implemented according to this concept;

 Diagram of the structure of a binary chain;

2. Interface implementation of binary chain

        1. Creation of binary chain

typedef int BTDataType;
//二叉链
typedef struct BinaryTreeNode
{
	BTDataType data; // 当前结点值域	
	struct BinaryTreeNode* left; // 指向当前结点左孩子
	struct BinaryTreeNode* right; // 指向当前结点右孩子
}BTNode;

First create a structure to represent the binary chain , data is the value range of the current node, and BTDataType is the data type of the stored value;

left refers to the left child of the current node , and right refers to the right child of the current node ;

The BTDataType here is a renaming of int , which can also be said to be a renaming of the data type, so that it can be unified to facilitate subsequent changes;

        2. Interface function

//动态创立新结点
BTNode* BuyNode(BTDataType x);
//创建二叉树
BTNode* GreatBTree();
//前序遍历
void PrevOrder(BTNode* root);
//中序遍历
void InOrder(BTNode* root);
//后序遍历
void PostOrder(BTNode* root);

This is the interface function to be implemented above;

        3. Dynamically create new nodes

//动态创立新结点
BTNode* BuyNode(BTDataType x)
{
	BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
	assert(newnode);
	newnode->data = x;
	newnode->left = NULL;
	newnode->right = NULL;
	return newnode;
}

When you create a new node later, call this function directly, and be sure to apply for space in the heap area, so that the space will be retained and will not be recycled after the function ends;

Assign new values ​​to data , both left and right point to null, and then return the node pointer ;

        4. Create a binary tree

//创建二叉树
BTNode* GreatBTree()
{
	BTNode* node1 = BuyNode(1);
	BTNode* node2 = BuyNode(2);
	BTNode* node3 = BuyNode(3);
	BTNode* node4 = BuyNode(4);
	BTNode* node5 = BuyNode(5);
	BTNode* node6 = BuyNode(6);

	node1->left = node2;
	node1->right = node4;
	node2->left = node3;
	node4->left = node5;
	node4->right = node6;

	return node1;
}

Then we apply for nodes to construct a binary tree and link the new nodes through links;

The created binary tree structure is shown below:

        5. Preorder traversal

//前序遍历
void PrevOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("N ");
		return;
	}

	printf("%d ", root->data);
	PrevOrder(root->left);
	PrevOrder(root->right);
}

The preorder, inorder, and subsequent traversal of a binary tree all use the same idea:

1. Preorder Traversal (also known as preorder traversal) - root node---->left subtree--->right subtree

2. Inorder Traversal - left subtree ---> root node ---> right subtree

3. Postorder Traversal—left subtree--->right subtree--->root node

Recursive thinking is used here: NULL is represented by N here . It is recommended to draw a picture to understand it and traverse it layer by layer;

Preorder traversal:

First access the root node (1) and then access its left subtree (2) ; print 1

At this time, the root node is (2) and then accesses its left subtree (3) ; print 1 2

At this time, the root node is (3) and then access its left subtree (NULL) ; print 1 2 3

At this time, the root node is (NULL) return NULL to (3) , and then access the right subtree (NULL) of (3 ) ; print 1 2 3 N

At this time, the root node is (NULL) return NULL to (3) . At this time, the access to (3) , that is, the left subtree of (2) ends, and then the right subtree (NULL) of (2) is accessed ; Print 1 2 3 NN

At this time, the root node is (NULL) return NULL to (2) . At this time, the access to (2) , that is, the left subtree of (1) ends, and then the right subtree (4) of (1) is accessed ; print 1 2 3 NNN

At this time, the root node is (4) and then access its left subtree (5) ; print 1 2 3 NNN 4

At this time, the root node is (5) and then access its left subtree (NULL) ; print 1 2 3 NNN 4 5

At this time, the root node is (NULL). Return NULL to (5) and then access the right subtree (NULL ) of (5 ) ; print 1 2 3 NNN 4 5 N

At this time, the root node is (NULL) return NULL to (5). At this time, the access to (5), that is, the left subtree of (4) is over, and then the right subtree (6) of (4) is accessed ; print 1 2 3 NNN 4 5 NN

At this time, the root node is (6) and then access its left subtree (NULL) ; print 1 2 3 NNN 4 5 NN 6

At this time, the root node is (NULL). Return NULL to (6) and then access the right subtree (NULL ) of (6) ; print 1 2 3 NNN 4 5 NN 6 N

At this time, the root node is (NULL). Return NULL to (6) . At this time, the access to (6), that is, the right subtree of (4) is over. At this time, the access to (4) , that is, the right subtree of (1) The access to the right subtree is over, and the access to (1) is also over at this time, and the preorder traversal is also over; print 1 2 3 NNN 4 5 NN 6 NN

Examples of graphic ideas:

    

BTNode* root = GreatBTree();
//前序遍历
PrevOrder(root);

This is preorder traversal;

        6. In-order traversal

//中序遍历
void InOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("N ");
		return;
	}

	InOrder(root->left);
	printf("%d ", root->data);
	InOrder(root->right);
}

In-order traversal: left subtree ---> root node ---> right subtree

The idea is the same as that of pre-order traversal , just change the order of access and follow the idea of ​​pre-order traversal and it's done;

//中序遍历
InOrder(root);
printf("\n");

        7. Post-order traversal

//后序遍历
void PostOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("N ");
		return;
	}

	PostOrder(root->left);
	PostOrder(root->right);
	printf("%d ", root->data);
}

Post-order traversal: left subtree ---> right subtree ---> root node

The ideas are still the same, but the access order has been changed. The ideas of front, middle and post-order traversal are all the same. As long as you understand one of them, you can figure them all out;

//后续遍历
PostOrder(root);
printf("\n");

 The basic traversal of the binary chain is fully implemented here. Some people say that there is also a layer-order traversal. This traversal requires the use of queues. The current C language stage implementation is too cumbersome, and the post-order blogger will make up for it;

Third, the number and height of nodes, etc.

Problems like this are also recursive problems, and we pay more attention to our understanding of function stack frames;

        1. Interface function

//结点个数
int	SumNode(BTNode* root);
//叶子结点个数
int LeafNode(BTNode* root);
//二叉树高度
int HeightTree(BTNode* root);
//二叉树第k层结点个数
int BTreeLeveSize(BTNode* root, int k);
//二叉树查找值为x的结点
BTNode* BTreeFine(BTNode* root, int x);

The above are the functions to be implemented;

        2. Number of nodes

//结点个数
int SumNode(BTNode* root)
{
	return root == NULL ? 0 : SumNode(root->left) + SumNode(root->right) + 1;
}

In fact, recursion is difficult to say, and it is not difficult to say that it is not difficult. There are skills in it;

1. Make a big deal small : the sum of the nodes of the binary tree with the root node (1) ==> the sum of the nodes of the left subtree (2) plus the sum of the nodes of the right subtree (4) plus its own node The number of points is 1 , and then the sum of the nodes with the root node (2) ==> the sum of the left subtree (3) plus NULL plus 1 , this is the rule; [ (1) = (2) + (4) +1】

2. End condition , returns 0 when the node is NULL ;

//结点个数
printf("%d\n", SumNode(root));

        3. Number of leaf nodes

//叶子结点个数
int LeafNode(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	if (root->left==NULL && root->right==NULL)
	{
		return 1;
	}
	else
	{
		return LeafNode(root->left) + LeafNode(root->right);
	}
}

 

Make a big deal small : find the number of leaf nodes of the binary tree with the root node (1) ==> the number of leaf nodes of its left subtree (2) plus its right subtree (4) ; [ (1) =(2)+(4)

End condition : Returns 0 when the node is NULL , returns 1 when the left and right subtrees of the node are both NULL ;

        4. Binary tree height

//二叉树高度
int HeightTree(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	int left = HeightTree(root->left);
	int right = HeightTree(root->right);
	return left > right ? left + 1 : right + 1;
}

Make a big deal small : find the height of the binary tree with the root node (1) ==> the height of the taller one of its left subtree (2) and right subtree (4) plus its own height 1; [(1) =(2)>(4)? (2)+1: (4)+1]

End condition : Return 0 when the node is NULL ;

//二叉树高度
printf("%d\n", HeightTree(root));

        5. Number of nodes at the kth level of the binary tree

//二叉树第k层结点个数
int BTreeLeveSize(BTNode* root, int k)
{
	if (root == NULL)
	{
		return 0;
	}
	if (k == 1)
	{
		return 1;
	}
	return BTreeLeveSize(root->left, k - 1)  + BTreeLeveSize(root->right, k - 1);
}

Make a big deal small : find the number of nodes at the Kth level of the binary tree with the root node (1) ==> the number of nodes at the K-1th level in its left subtree (2) plus the right subtree (4) Number; [ (1) = (2) + (4) ]

End condition : Returns 0 when the node is NULL , returns 1 when K is equal to 1 ;

//二叉树第k层结点个数
printf("%d\n", BTreeLeveSize(root,3));

        6. Binary tree searches for the node with value x

//二叉树查找值为x的结点
BTNode* BTreeFine(BTNode* root, int x)
{
	if (root == NULL)
	{
		return NULL;
	}
	if (root->data == x)
	{
		return root;
	}
	if (BTreeFine(root->left, x) == NULL)
	{
		return BTreeFine(root->right, x);
	}
	else
	{
		return BTreeFine(root->left, x);
	}
}

Make a big deal small : Find the node with the value x in the binary tree with the root node (1) ==> Find the node with the value x in its left subtree (2) and right subtree (4) ;

End condition : Return NULL when the node is NULL , return the node when the value of the node is x ;

Idea: So when one of the subtrees is not NULL , it is the requested node. If the left subtree is not empty, the node of the left subtree is returned. Otherwise, the node of the right subtree is returned. If both the left and right subtrees are empty, then Also returns the node of the right subtree;

//二叉树查找值为x的结点
BTNode* ret = BTreeFine(root, 6);
printf("%d\n", ret->data);

ret = BTreeFine(root, 3);
printf("%d\n", ret->data);

It’s over here. Through these questions, we have fully understood the binary tree (recursive tree) . This is the recursive algorithm . You still need to draw more pictures to understand. The basic knowledge of recursion is the creation and destruction of function stack frames ;

The third stage is here. At this stage, we will take you to understand the recursive idea of ​​binary trees (recursive trees);

The blogger will update it one after another later;

If there are any deficiencies, please feel free to supplement and communicate!

end. .


Guess you like

Origin blog.csdn.net/m0_71676870/article/details/132839828