`
linest
  • 浏览: 149984 次
  • 性别: Icon_minigender_1
  • 来自: 内蒙古
社区版块
存档分类
最新评论

pat-1020* Tree Traversals

    博客分类:
  • pat
 
阅读更多
给后序和中序遍历
求层序遍历

Sample Input:7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

Sample Output:4 1 6 3 5 7 2

注意如果是按char型处理,还要考虑多字符情况  如12
在输入上要加以处理,用int比较简单

#include "stdio.h"
#include "stdlib.h"


typedef struct bt_node    // 定义二叉树结点。
{
   char data ;
   struct bt_node * left ;
   struct bt_node * right ;
} bt_node ;

//搜索根在中序数组中的下标,若两个序列存在逻辑错误结束程序。
int search(char in[] , int inl , int inh , char ch)
{
   int i ;
   for(i=inl;i<=inh;i++)
   {
       if(in[i]==ch) return i ;
   } 
   printf("两个序列相互矛盾,无法构造二叉树!\n") ;
   exit(1) ;
}

//中根序列、后跟序列构造二叉树。
bt_node * create_bt1(char in[],int inl,int inh,char post[],int postl,int posth)
{
   int mid ;
   bt_node * r ;
   if(inl>inh||postl>posth) return NULL; //递归结束条件。
   mid    = search(in , inl , inh , post[posth]) ; //搜索根在中序数组中的下标。
   //printf("%d\n" , mid) ;可以通过这个语句查看有没有搜索正确。
   r = (bt_node *)malloc(sizeof(bt_node)) ;
   r->data = post[posth] ; //构造根结点。
   r->left = create_bt1(in,inl,mid-1, post,postl,postl+mid-inl-1) ; //构造左子树。
   r->right = create_bt1(in,mid+1,inh, post,postl+mid-inl,posth-1) ; //构造右子树。
   return r ; //返回根指针。
}

bt_node * create_bt2(char in[],int inl,int inh,char pre[],int prel,int preh)
{
   int mid ;
   bt_node * r ;
   if(inl>inh||prel>preh) return(NULL) ;
   mid = search(in , inl , inh , pre[prel]) ;
   //printf("%d\n" , mid) ;可以通过这个语句查看有没有搜索正确。
   r = (bt_node *)malloc(sizeof(bt_node)) ;
   r->data = pre[prel] ;
   r->left = create_bt2(in,inl,mid-1, pre,prel+1,prel+mid-inl) ;
   r->right = create_bt2(in,mid+1,preh, pre,prel+mid-inl+1,preh) ;
   return(r) ;
}

void pre_order(bt_node * r)
{
   if(r==NULL) return ;
   putchar(r->data) ;
   pre_order(r->left) ;
   pre_order(r->right) ;
}

void in_order(bt_node * r)
{
   if(r==NULL) return ;
   in_order(r->left) ;
   putchar(r->data) ;
   in_order(r->right) ;
}

void post_order(bt_node * r)
{
   if(r==NULL) return ;
   post_order(r->left) ;
   post_order(r->right) ;
   putchar(r->data) ;
}

//定义链队列结点。
typedef struct queue_node
{
   struct bt_node * bt_node_pointer ;
   struct queue_node * next ;
} queue_node ;

//二叉树的层次遍历。
void wfs_order(bt_node * r)
{
   queue_node * front , * rear , * mid ;
   front = (queue_node *)malloc(sizeof(queue_node)) ;
   rear = front ;
   mid = front ; //定义一个队列,并给予初始化。

   rear->bt_node_pointer = r ;
   rear = (queue_node *)malloc(sizeof(queue_node)) ;
   mid->next = rear ;
   mid = rear ; //r进队。

   while(front!=rear) //front!=rear指队列非空。
   {
       r = front->bt_node_pointer ;
	   printf("%d",r->data) ; //r出队,并打印r->data。
       front = front->next ;
       

       if(r->left != NULL) //r->left进队。
       {
           rear->bt_node_pointer = r->left ;
           rear = (queue_node *)malloc(sizeof(queue_node)) ;
           mid->next = rear ;
           mid = rear ;
       }

       if(r->right != NULL) //r->right进队。
       {
           rear->bt_node_pointer = r->right ;
           rear = (queue_node *)malloc(sizeof(queue_node)) ;
           mid->next = rear ;
           mid = rear ;
       }    

	   if(front != rear)
		   putchar(' ');
   }
}



int main()
{
	int N;
	int num;
	scanf("%d",&N);
	getchar();
	char* post = new char[N];
	char* in = new char[N];

	for(int i=0;i<N;i++)
	{
		scanf("%d",&num);
		post[i] = num;
	}

	for(int i=0;i<N;i++)
	{
		scanf("%d",&num);
		in[i] = num;
	}

	 bt_node * root = create_bt1(in,0,N-1, post,0,N-1);
	 wfs_order(root) ;
     putchar('\n') ;

	 return 0;
}




分享到:
评论

相关推荐

    03-树2. Tree Traversals Again.zip

    给你inorder的栈操作步骤,让你写出postorder后的序列。 http://blog.csdn.net/xkzju2010/article/details/46325457

    7-1 Tree Traversals Again_DS_pta_C++_AgainAgain_

    PTA 7-1 Tree Traversals Again

    陈越、何钦铭-数据结构作业11:Tree Traversals Again二叉树非递归遍历/栈遍历

    An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the ...

    Heyinsen#LeetcodeOrPat#1020 Tree Traversals (25 分) 后序和中序构建树1

    思路后序和中序构造二叉树唯一一个注意的地方,在注释中。// 注意这里是pos-right_num-1而不是想当然的pos_inr-1,这两个的位置并不一定对应。

    算法:经典算法和最知名算法的实现

    This repository contains various classic algorithms about ordenation, graph traversals, dynamic programing and some crypto related stuff as well. 在这个仓库中: - Peak finding (1D/2D) - Binary ...

    BinaryTree-traversals:React Application呈现二叉树遍历

    cd BinaryTree遍历 npm安装 npm开始 浏览到htpp:// localhost:3000 登陆页面 包含一棵静态树。 用户可以选择任何遍历(顺序,前顺序或后顺序),遍历的结果将显示在树下。 您也可以同时签出算法。 预购遍历 后...

    c(数据结构).

    c (数据结构) AVLTree Graph Heap Tree Traversals Again 计算器_栈 二叉排序树 二分法查找 线性表 等 值得学习

    Binary_tree_order_traversals

    Binary_tree_order_traversals 在该项目中,我们可以看到树的顺序,前顺序和后顺序。 该程序将打开一个菜单,并允许用户通过一个接一个地导入数据(节点)来创建他/她的树: '''0。退出 插入 展示 搜索 为了 预购...

    Data Structures & Algorithms in Swift 4th Edition

    * Traversals: Traverse graphs and trees efficiently with breadth-first, depth-first, Dijkstra’s and Prim’s algorithms to solve problems such as finding the shortest path or lowest cost in a network...

    leetcode焦虑-Interview-Notes:采访笔记

    Tree traversals, search Stack Queue Graph traversals(BFS, DFS) 最短路径数学题 从之前的采访中吸取的错误和教训: 调度: 更加注意时间安排,在公司现场面试前保持2-3天的开放时间。 除非非常关键,否则不要立即...

    Programming Interview Problems and Algorithms in Ruby

    Tree Serialization, Finding the Top k Elements of Data Streams, MapReduce, Partial Sorting, the Skyline Problem, DFS, BFS and Topological Sorting of Dags, the Alternative Alphabet and the Phone Words...

    开源项目-mdempsky-unbed.zip

    开源项目-mdempsky-unbed.zip,unbed: refactoring tool to remove implicit field traversals

    Neo4j High Performance(PACKT,2015)

    Study the in-built graph algorithms for better traversals and discover Spring-Data-Neo4j Look under the hood of Neo4j, covering concepts from the core classes in the source to the internal storage ...

    iBFS - Concurrent Breadth-First Search on GPUs - 2016 (ibfs_tcm18-284417)-计算机科学

    iBFS: Concurrent Breadth-First Search on GPUsHang Liu H....- where multiple breadth-first traversals are performed si- multaneously on the same graph. We have designed and developed a new

    FileOutputBuffer.rar_Cause

    In debug build this will cause full traversals of the tree when the validate is called on insert and remove. Useful for debugging but very slow.

    PURE-FTPD for WINDOWS 源码

    Before all : Pure-FTPd was designed on Unix and for Unix. The Windows port has been done because some people are forced to work on Win32 by their ...traversals, device opening, etc) .

    ProjectOZ

    The interrupt execution environment is recorded in a PCB and subsequent portal traversals chain PCBs as a stack. &lt;br&gt;Resume() restores the most recent PCB (analogous to a return-from-interrupt ...

    generic-lens:一般地得出遍历,透镜和棱镜

    通用镜头 通常得出等值线,遍历,透镜和棱镜。 该库使用GHC.Generics以类型定向的方式为代数数据类型派生高效的光学系统(遍历,透镜和棱镜),并在可能时侧重于良好的类型推断和错误消息。 该库在论文中进行了描述...

    Android代码-janusgraph

    can support thousands of concurrent users, complex traversals, and analytic graph queries. Learn More The project homepage contains more information on JanusGraph and provides links to ...

    Android代码-probe

    Dissect layout traversals on Android. Features Intercept View methods. onMeasure(int, int) onLayout(boolean, int, int, int, int) draw(Canvas) and onDraw(Canvas) requestLayout() Override any of ...

Global site tag (gtag.js) - Google Analytics