The establishment and basic operation of binary tree | C++

1. Topic description

Write a program to realize the following operations of a binary tree:
1) Create a binary linked list
2) Traverse the pre-order, in-order, and post-order traversal of the binary tree
3) Find the number of leaf nodes
in the binary tree 4) Combine the left and right children of all nodes in the binary tree tree swap

Input:
  Extended binary tree preorder sequence: ab#d##ce###. Where # represents a null pointer.

Output:
  Concave representation of
  binary tree Preorder sequence, inorder sequence, and postorder sequence of
  binary tree Number of leaf nodes in binary tree
  Concave representation of binary tree after left
  and right subtrees are exchanged preorder, inorder, and postorder sequences.

Explanation:
  When outputting a concave binary tree, first output the root node, and then output the left and right subtrees in sequence, with 3 spaces between the upper and lower nodes.

test input expected output time limit memory limit extra process
Test case 1 display as text
  1. ab#d##what###↵
display as text
  1. BiTree↵
  2. a↵
  3.     b↵
  4.         d↵
  5.     c↵
  6.         e↵
  7. pre_sequence  : abdce↵
  8. in_sequence   : bdaec↵
  9. post_sequence : dbeca↵
  10. Number of leaf: 2↵
  11. BiTree swapped↵
  12. a↵
  13.     c↵
  14.         e↵
  15.     b↵
  16.         d↵
  17. pre_sequence  : acebd↵
  18. in_sequence   : ceadb↵
  19. post_sequence : ecdba↵
1 second 64M 0
Test case 2 display as text
  1. abd##e##cf##g##↵
display as text
  1. BiTree↵
  2. a↵
  3.     b↵
  4.         d↵
  5.         e↵
  6.     c↵
  7.         f↵
  8.         g↵
  9. pre_sequence  : abdecfg↵
  10. in_sequence   : dbeafcg↵
  11. post_sequence : debfgca↵
  12. Number of leaf: 4↵
  13. BiTree swapped↵
  14. a↵
  15.     c↵
  16.         g↵
  17.         f↵
  18.     b↵
  19.         e↵
  20.         d↵
  21. pre_sequence  : acgfbed↵
  22. in_sequence   : gcfaebd↵
  23. post_sequence : gfcedba↵
1 second 64M 0

2. Complete code

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;

//二叉树的存储表示
typedef struct BiTNode
{
	char data;
	struct BiTNode *lchild;
	struct BiTNode *rchild;
}BiTNode, *BiTree;

queue<BiTree>q;	//辅助队列
int counts = 0;	//统计叶子结点数目

void createBiTree();	//建立二叉树
void visit(BiTree R);	//访问结点
void print(BiTree R, int n);	//输出二叉树
void pre_sequence(BiTree R);	//先序遍历
void in_sequence(BiTree R);		//中序遍历
void post_sequence(BiTree R);	//后序遍历
/* 左右子树交换后的各种表示 */
void swap_print(BiTree R, int n);
void swap_pre_sequence(BiTree R);
void swap_in_sequence(BiTree R);
void swap_post_sequence(BiTree R);

int main()
{
	BiTree bit;
	bit = (BiTree)malloc(sizeof(BiTNode));
	q.push(bit);

	//建树 
	createBiTree();
	
	//打印二叉树
	printf("BiTree\n");
	print(bit, 0);

	//先序 
	printf("pre_sequence  : ");
	pre_sequence(bit);
	printf("\n");

	//中序 
	printf("in_sequence   : ");
	in_sequence(bit);
	printf("\n");

	//后序 
	printf("post_sequence : ");
	post_sequence(bit);
	printf("\n");

	//叶子结点数目
	printf("Number of leaf: %d\n", counts);
	
	/* 翻转 */ 
	printf("BiTree swapped\n");
	swap_print(bit, 0);

	printf("pre_sequence  : ");
	swap_pre_sequence(bit);
	printf("\n");
 
	printf("in_sequence   : ");
	swap_in_sequence(bit);
	printf("\n");

	printf("post_sequence : ");
	swap_post_sequence(bit);
	printf("\n");

	return 0;
}

void createBiTree()
{
	char c;
	BiTree T, N;
	while (!q.empty())
	{
		scanf("%c", &c);
		if (c == '\n')	//换行符结束读取
			break;

		T = q.front();
		q.pop();
		T->data = c;	//结点赋值
		
		if (c!='#') {
			//建立左子树
			N = (BiTree)malloc(sizeof(BiTNode));
			T->lchild = N;
			q.push(N);
			createBiTree();
			//建立右子树 
			N = (BiTree)malloc(sizeof(BiTNode));
			T->rchild = N;
			q.push(N);
			createBiTree();
		}
	}
}
void visit(BiTree R)
{
	//结点不为空则输出
	if (R->data != '#'&&R->data != '\0')
		cout << R->data;
}
void print(BiTree R, int n)
{
	if (R->data != '#'&&R->data != '\0')
	{
		int i = 1;
		while (i <= n)	//第几层就输几个tab
		{
			cout << "    ";
			i++;
		}
		visit(R);
		cout << endl;

		//一层一层往下找
		n++;
		print(R->lchild, n);
		print(R->rchild, n);
	}
}
void pre_sequence(BiTree R)
{
	if (R->data != '#'&&R->data != '\0')
	{
		visit(R);
		pre_sequence(R->lchild);
		pre_sequence(R->rchild);
	}
}
void in_sequence(BiTree R)
{
	if (R->data != '#'&&R->data != '\0')
	{
		in_sequence(R->lchild);

		/* 统计叶子结点和中序遍历结合 */
		if ((R->lchild->data == '#' || R->lchild->data == '\0') && (R->rchild->data == '#' || R->rchild->data == '\0'))
			counts++;

		visit(R);
		in_sequence(R->rchild);
	}
}
void post_sequence(BiTree R)
{
	if (R->data != '#'&&R->data != '\0')
	{
		post_sequence(R->lchild);
		post_sequence(R->rchild);
		visit(R);
	}
}
void swap_print(BiTree R, int n)
{
	//翻转后的写法与原来一模一样,只是换了顺序而已
	if (R->data != '#'&&R->data != '\0')
	{
		int i = 1;
		while (i <= n)
		{
			cout << "    ";
			i++;
		}
		visit(R);
		cout << endl;

		n++;
		swap_print(R->rchild, n);
		swap_print(R->lchild, n);
	}
}
void swap_pre_sequence(BiTree R)
{
	if (R->data != '#'&&R->data != '\0')
	{
		visit(R);
		swap_pre_sequence(R->rchild);
		swap_pre_sequence(R->lchild);
	}
}
void swap_in_sequence(BiTree R)
{
	if (R->data != '#'&&R->data != '\0')
	{
		swap_in_sequence(R->rchild);
		visit(R);
		swap_in_sequence(R->lchild);
	}
}
void swap_post_sequence(BiTree R)
{
	if (R->data != '#'&&R->data != '\0')
	{
		swap_post_sequence(R->rchild);
		swap_post_sequence(R->lchild);
		visit(R);
	}
}

3. Description

The original text comes from http://t.csdn.cn/j3292

It's a senior/senior sister with the same major as me

This blog post by ta was written in 20 years, and the topic used in 22 years is no longer applicable. I slightly changed some codes of the function createBiTree(), and now the updated code can be fully AC

Guess you like

Origin blog.csdn.net/m0_70241024/article/details/127332875