构建一棵二叉树C++

二叉树的定义我就不bb了,这里用来重新复习下数据结构。

#include<iostream>
using namespace std;
/*
@Author: liqiang
@Date: 2020-01-14 20:44
*/
typedef struct Node
{
	int data;
	Node *left;
	Node *right;
	Node(int data,Node *left=NULL,Node *right=NULL)
	{
		this->data = data;
		this->left = left;
		this->right = right;
	}
}Node;



void createBinaryTree(Node *&root)
{
	int data;
	cout<<"input the root if you donot want continue the nod input enough '2'"<<endl;
	cin >> data;
	if(data == 2)
	   return ;
	
	else
	{
		//按前序遍历输入构建二叉树
		root = new Node(data);
		createBinaryTree(root->left);
		createBinaryTree(root->right);
	}
}


void PrePut(Node *root)
{
	if(root == NULL) return ;
	else
	{
		cout << root->data << " ";
		PrePut(root->left);
		PrePut(root->right);
	}
}


void backPut(Node *root)
{
	if(root == NULL) return ;
	else
	{
		backPut(root->left);
		backPut(root->right);
		cout << root->data << " ";
	}
}



int main()
{
	
	Node *root = NULL;
	int n = 0;
	cout<<"input the tree from root in the way of preorder"<<endl;
	createBinaryTree(root);
	cout<<"choose the way to output"<<endl;
	cout<<"1 Preorder"<<endl<<"2 BackOrder"<<endl;
	cin>>n;
	switch(n){
	case 1:
		cout<<"the Preorder is :\t";
	    PrePut(root);break;
	case 2:
		cout<<"The BackOrder is :\t";
	    backPut(root);break;
	default:
		cout<<"choose 1 or 2"<<endl;
	}
	
	return 0;
} 

层次遍历

想必有了这个,大家就可以很容易写出二叉树前序、中序、后序遍历。但如果是层次遍历呢?也就是每一层从左往右遍历。这很类似于一个队列,根节点必然只有一个,将其加入队列,然后出队(也就是输出,第一层输出完毕)。紧接着第二层,用队首元素,也就是刚刚出队元素,把它的两个子节点加入队列。后续操作和这个重复。类似于BFS思路。

void bfs(Node *root)
{
	queue<Node*> q;
	q.push(root);
	while(!q.empty())
	{
		Node * now = q.front();
		q.pop();
		cout << now->data << " ";
		if(now->left != NULL)
		{
			q.push(now->left);
		}
		
		if(now->right != NULL)
		    q.push(now->right);
	}
} 

测试样例:1 3 4 2 2 2 5 6 2 2 7 2 2

先序输出:1 3 5 4 6 7

 根据中序+前序(后序)构建二叉树

测试样例:

先序遍历结果:ABDHKECFIGJ

中序遍历结果:HKDBEAIFCGJ

后序遍历结果:KHDEBIFJGCA

#include<iostream>
#include<queue> 
#include<cstring>
using namespace std;

typedef struct Node
{
	char data;
	Node *left;
	Node *right;
	Node(char data,Node *left=NULL,Node *right=NULL)
	{
		this->data = data;
		this->left = left;
		this->right = right;
	}
	Node()
	{
		this->left = NULL;
		this->right = NULL;
	}
}Node,*Tree;


char str1[100];//前序遍历
char str2[100];//中序遍历
Node *createNode()
{
	Tree node = new Node();
	return node; 
}


/*
测试样例
ABDHKECFIGJ
HKDBEAIFCGJ
*/
Node *build(int s1,int e1,int s2,int e2)
{
	Node* root = createNode();
	root->data = str1[s1];
	int rootIdx;
	//求前序节点在中序中的下标
	for(int i = s2;i <= e2;++i)
	{
		if(str2[i] == str1[s1])
		{
			rootIdx = i;
			break;
		}
	} 
	
	//左子树不为空 
	if(rootIdx != s2)
	{
		root->left = build(s1+1,s1+(rootIdx-s2),s2,rootIdx-1);
	}
	
	//右子树不为空 
	if(rootIdx != e2)
	{
		root->right = build(s1+(rootIdx-s2)+1,e1,rootIdx+1,e2); 
	} 
	
	return root;
}


//后序+中序 
/*
测试样例
KHDEBIFJGCA
HKDBEAIFCGJ
*/
Node *build1(int s1,int e1,int s2,int e2)
{
	Node* root = createNode();
	root->data = str1[e1];
	int rootIdx;
	//求后续序节点在中序中的下标
	for(int i = s2;i <= e2;++i)
	{
		if(str2[i] == str1[e1])
		{
			rootIdx = i;
			break;
		}
	} 
	
	//左子树不为空 
	if(rootIdx != s2)
	{
		root->left = build1(s1,s1+(rootIdx-s2-1),s2,rootIdx-1);
	}
	//右子树不为空 
	if(rootIdx != e2)
	{
		root->right = build1(s1+(rootIdx-s2),e1-1,rootIdx+1,e2); 
	} 
	
	
	
	return root;
}

void postOrder(Node* root)
{
	if(root == NULL) return ;
	else
	{
		postOrder(root->left);
		postOrder(root->right);
		cout << root->data << " ";
	}
}
int main()
{
	while(scanf("%s",str1) != EOF)
	{
		scanf("%s",str2);
		int l1 = strlen(str1);
		int l2 = strlen(str2);
		Node* root = build1(0,l1-1,0,l2-1);
		postOrder(root);
	}
	return 0;
}
发布了178 篇原创文章 · 获赞 14 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_40511966/article/details/103979592
今日推荐