• 二叉树先序遍历、中序遍历、后序遍历


    输入二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列。(非建二叉树版本)

    #include<iostream>
    #include<string>
    using namespace std;
    string preord, inord;
    void rebuild (int preleft, int preright, int inleft, int inright)
    {
    	int root, leftsize, rightsize;
    	if(preleft <= preright && inleft <= inright)
    	{
    		for(root = inleft; root <= inright; root++)
    		{
    			if(preord[preleft] == inord[root])
    			{
    				break;
    			}
    		}
    		leftsize = root - inleft;
    		rightsize = inright - root;
    		if(leftsize > 0)
    		{
    			rebuild(preleft + 1, preleft + leftsize, inleft, inleft + leftsize - 1);
    		}
    		if(rightsize > 0)
    		{
    			rebuild(preleft + leftsize + 1, preright, inleft + leftsize + 1, inright);
    		}
    	cout << inord[root];
    	}
    }
    int main()
    {
    	int length;
    	while(cin >> preord >> inord)
    	{
    		length = preord.length();
    		rebuild(0, length-1, 0, length-1);
    		cout << endl;
    		preord.clear();
    		inord.clear();
    	}
    	return 0;
    }
    
    

    已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历

    #include<iostream>
    #include<string>
    #include<algorithm>
    using namespace std;
    string midord, inord;
    void rebuild(int midleft, int midright, int inleft, int inright)
    {
    	int root, leftsize, rightsize;
    	if(midleft <= midright && inleft <= inright)
    	{
    		 for(root = midleft; root <= midright; root++)
            {
                if(inord[inright] == midord[root])
    			{
    				break;
    			}
            }
    		leftsize = root - midleft;
    		rightsize = midright - root;
    		cout << midord[root];
    		if(leftsize > 0)
    		{
    			rebuild(midleft, midleft + leftsize - 1, inleft, inleft + leftsize - 1);
    		}
    		if(rightsize > 0)
    		{
    			rebuild(midleft + leftsize + 1, midright, inleft + leftsize, inright - 1);
    		}
    	}
    }
    int main()
    {
    	while(cin >> midord >> inord)
    	{
    	int length = midord.length() -1;
    	rebuild(0, length, 0, length);
    	cout << endl;
    	midord.clear();
    	inord.clear();
    	}
    	return 0;
    }
    

    至于已知前序遍历序列和后序遍历序列,求中序遍历序列,存在多种情况。


    建立二叉树版本


    #include<iostream>
    #include<queue>
    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    #pragma warning(disable : 4996)
    #define MAX 100
    typedef struct BiNode
    {
    	char data;
    	struct BiNode *left;
    	struct BiNode *right;
    }BiNode, *BiTree;
    int sum;
    
    void CreateBinaryTreeToPre(BiTree &tree, char *inorder, char *postorder, int length)
    {
    	if(length == 0)
    	{
    		return;
    	}
    	tree = new BiNode;
    	tree->data = *(postorder + length - 1);
    	tree->left = NULL;
    	tree->right = NULL;
    	//cout << tree->data;
    	int rootIndex;
    	for(rootIndex = 0; rootIndex < length; rootIndex++)
    	{
    		if(inorder[rootIndex] == *(postorder + length - 1))
    		{
    			break;
    		}
    	}
    	CreateBinaryTreeToPre(tree->left, inorder, postorder, rootIndex);
    	CreateBinaryTreeToPre(tree->right, inorder + rootIndex + 1, postorder + rootIndex, length - rootIndex - 1);
    }
    
    
    void CreateBinaryTreeToPost(BiTree &tree, char *preorder, char *inorder, int length)
    {
    	if(length == 0)
    	{
    		return;
    	}
    	tree = new BiNode;
    	tree->data = *preorder;
    	tree->left = NULL;
    	tree->right = NULL;
    	int rootIndex;
    	for(rootIndex = 0; rootIndex < length; rootIndex++)
    	{
    		if (inorder[rootIndex] == *preorder)
    		{
    			break;
    		}
    	}
    	CreateBinaryTreeToPost(tree->left, preorder + 1, inorder, rootIndex);
    	CreateBinaryTreeToPost(tree->right, preorder + rootIndex + 1, preorder + rootIndex + 1, length - rootIndex - 1);
    	//cout << tree->data;
    }
    
    void PreOrder(BiTree T)//前序遍历
    {
    	if(T != NULL)
    	{
    		cout << T->data;
    		PreOrder(T->left);
    		PreOrder(T->right);
    	}
    }
    void InOrder(BiTree T)//中序遍历
    {
    	if(T != NULL)
    	{
    		InOrder(T->left);
    		cout << T->data;
    		InOrder(T->right);
    	}
    }
    void PostOrder(BiTree T)//后序遍历
    {
    	if(T != NULL)
    	{
    		PostOrder(T->left);
    		PostOrder(T->right);
    		cout << T->data;
    	}
    }
    void LevOrder(BiTree T)//层次遍历
    {
    	if(T != NULL)
    	{
    		BiTree p = T;
    		queue<BiTree>que;
    		que.push(p);
    		while(!que.empty())
    		{
    			p = que.front();
    			cout << p->data;
    			que.pop();
    			if(p->left != NULL)
    			{
    				que.push(p->left);
    			}
    			if(p->right != NULL)
    			{
    				que.push(p->right);
    			}
    		}
    	}
    }
    int Size(BiTree T)//计算二叉树节点数
    {
    	if(T)
    	{
    		if(T->left == NULL && T->right == NULL)
    		{
    			sum++;
    		}
    		Size(T->left);
    		Size(T->right);
    	}
    	return sum;
    }
    int Deep(BiTree T)//计算二叉树深度
    {
    	int m, n;
    	if(T == NULL) return 0;
    	m = Deep(T->left);
    	n = Deep(T->right);
    	if(m > n) return m + 1;
    	else return n + 1;
    }
    
    int Scan()
    {
    	int n;
    	cout << "-----------------++++++++++++++++++------------------- " << endl;  
    	cout << "                请选择所要进行的操作                     " << endl;  
    	cout << "   1、已知前序、中序建立二叉树                           " << endl;  
    	cout << "   2、已知中序、后序建立二叉树                           " << endl;  
    	cout << "   3、输出前序遍历序列                                   " << endl;  
    	cout << "   4、输出中序遍历序列      5、输出后序遍历结果           " << endl;  
    	cout << "   6、输出层次遍历序列  7、输出叶节点数和深度             " << endl;  
    	cout << "-----------------++++++++++++++++++------------------- " << endl;  
    	cin >> n;
    	return n;
    }
    
    int main(void)
    {
    	int quit = 0;
    	BiTree tree = NULL;
    	char inorder[MAX] = {0};
    	char preorder[MAX] = {0};
    	char postorder[MAX] = {0};
    
    	while (!quit)
    	{
    		switch(Scan())
    		{
    		case 1 : cin >> preorder >> inorder; CreateBinaryTreeToPost(tree, preorder, inorder, strlen(preorder)); break;
    		case 2 : cin >> inorder >> postorder; CreateBinaryTreeToPre(tree, inorder, postorder, strlen(inorder));break;
    		case 3 : cout << "前序遍历结果为:" << endl; PreOrder(tree); cout << endl << endl; break;
    		case 4 : cout << "中序遍历结果为:" << endl; InOrder(tree); cout << endl << endl; break;
    		case 5 : cout << "后序遍历结果为:" << endl; PostOrder(tree); cout << endl << endl; break;
    		case 6 : cout << "层次遍历结果为:" << endl; LevOrder(tree); cout << endl << endl; break;
    		case 7 : cout << "二叉树叶节点个数为:" << Size(tree)<<endl; cout << "二叉树深度数为:" << Deep(tree) << endl; break;
    		default: quit = 0; break;
    		}
    	}
    	return 0;
    }



  • 相关阅读:
    @hdu
    @51nod
    @51nod
    @51nod
    JS-正则表达式常规运用
    CSS-复选框默认样式修改
    Vue-路由传参query与params
    Vue-阻止页面回退
    Vue-表单提交
    JS-原生的ajax
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835098.html
Copyright © 2020-2023  润新知