Data structure-N-ary tree-child brother notation|Understand and realize the child brother notation of N-ary tree|Tree into binary tree

Child brother notation

Tree converted to binary tree

Parental array notation , child parental notation

The child brother notation is also called the binary tree notation, which uses a binary linked list as the storage structure of the tree. The child sibling notation makes each node include three parts: the value of the node, the pointer to the first child node of the node, and the pointer to the next sibling node of the node (you can find the value of the node along this field. All sibling nodes)

The steps to convert a tree into a binary tree are:
(1) Add lines. It is to add a line between all the brother nodes;
(2) Wipe the line. That is, for each node in the tree, only the connection between it and the first child node is kept, and the connection between it and the other child nodes is deleted;
(3) Rotation. The root node of the tree is used as the axis, and the whole tree is rotated clockwise to a certain angle to make the structure distinct.

Insert picture description here

Binary tree

When constructing a binary tree before, the pointers were the left child and the right child.

typedef struct BTree
{
    
    
	elementype data;
	struct BTree *lef_child, *rig_child;
}*T_NODE,Bnode;


When a tree has multiple subtrees, it is defined as:

typedef struct NTree
{
    
    
	elementype data;
	struct NTree *fis_child, *next_sib;
}*Node;

So when inputting tree elements, the left side is the first child, and the right side is the brother.

#include<iostream>
using namespace std;

typedef int Status;
typedef char elementype;

typedef struct NTree
{
    
    
	elementype data;
	struct NTree *fis_child, *next_sib;
}*Node;

//初始化
Status InitTree(Node &tree)
{
    
    
	tree = new NTree;
	tree->data = NULL;
	tree->fis_child = NULL;
	tree->next_sib = NULL;
	return 0;
}
//添加内容
Status CreatTree(Node &tree)
{
    
    
	
	if (tree != NULL)
	{
    
    
		tree = new NTree;
		cout <<"please input a data" << endl;
		elementype x;
		cin >> x;

		tree->data = x;
		if (tree->data == '#')
		{
    
    
			cout << "The node branch is empty" << endl;
			tree = NULL;
			//return NULL;
		}
		else
		{
    
    
			
			cout << "please input " << tree->data << " first child" <<endl;
			CreatTree(tree->fis_child);
			cout << "please input " << tree->data << " child of sibling" << endl;
			CreatTree(tree->next_sib);
		}
	}
	return 0;
}

//——————————————输出————————————————
//前序输出
Status OutputTree(Node &tree)
{
    
    
	if (tree == NULL)
	{
    
    
		cout << "The Tree is empty" << endl;
		return NULL;
	}
	cout << tree->data << endl;
	if (tree->fis_child != NULL)
	{
    
    
		OutputTree(tree->fis_child);
	}
	if (tree->next_sib != NULL)
	{
    
    
		OutputTree(tree->next_sib);
	}
	return 0;
}
//中序输出
Status OutTree_mid(Node &tree)
{
    
    
	if (tree == NULL)
	{
    
    
		return NULL;
	}
	if (tree->fis_child != NULL)
	{
    
    
		OutTree_mid(tree->fis_child);
	}
	cout << tree->data << endl;
	if (tree->next_sib != NULL)
	{
    
    
		OutTree_mid(tree->next_sib);

	}
	return 0;
}
//后序输出
Status OutTreeafter(Node &tree)
{
    
    
	if (tree == NULL)
		return NULL;
	if (tree->fis_child != NULL)
	{
    
    
		OutTreeafter(tree->fis_child);
	}
	if (tree->next_sib != NULL)
	{
    
    
		OutTreeafter(tree->next_sib);
	}
	cout << tree->data << endl;
	return 0;
}
//——————————————输出————————————————

//copy
Status CopyTree(Node &tree,Node &TTT)
{
    
    
	if (tree == NULL)
	{
    
    
		TTT = NULL;
		return 0;
	}
	else
	{
    
    
		TTT = new NTree;
		TTT->data = tree->data;
		CopyTree(tree->fis_child,TTT->fis_child);
		CopyTree(tree->next_sib,TTT->next_sib);

		//return 0;

	}
	return 0;

}
int main()
{
    
    
	Node T,Tcopy;
	cout << "——————InitTree——————" << endl;
	InitTree(T);
	cout << "——————CreatTRee——————" << endl;
	CreatTree(T);
	cout << "——————Preamble output——————" << endl;
	OutputTree(T);
	cout << "——————Middle order output——————" << endl;
	OutTree_mid(T);
	cout << "——————Sequential output——————" << endl;
	OutTreeafter(T);

	cout << "——————Copy a binary tree——————" << endl;
	CopyTree(T,Tcopy);
	//OutTreeafter(Tcopy);
	return 0;
}

For the specific operations of the binary tree, you can click
the chain representation of the binary tree | pre-order output | subsequent output | middle-order output | destruction and other operations
. For the understanding and realization of the clue binary tree, the
singly linked list | the double linked list | the binary tree | the clue binary tree | the conversion explanation and application

Guess you like

Origin blog.csdn.net/weixin_46096297/article/details/112545956