二叉树中序遍历 (C语言实现)
在计算机科学中,树是一种重要的非线性数据结构,直观地看,它是数据元素(在树中称为结点)按分支关系组织起来的结构。二叉树是每个节点最多有两个子树的有序树。通常子树被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用于实现二叉查找树和二叉堆。
如下是实现创建二叉树和二叉树中序遍历的代码:
#include <stdio.h>
#include <stdlib.h>
#include <memory.h> typedef struct _tree_node{
char data;
struct _tree_node * left;
struct _tree_node * right;
struct _tree_node * father;
}tree_node; void createTree(tree_node * root);
void inorderTraverseTree(tree_node * pRoot); int main()
{
tree_node root;
memset(&root, , sizeof(tree_node));
printf("Please create the tree: \n");
createTree(&root);
printf("The inorder traverse result is: \n");
inorderTraverseTree(&root);
return ;
} //inorder traversal
void inorderTraverseTree(tree_node * pRoot)
{
tree_node * pCur = pRoot;
if(pCur != NULL)
{
if(pCur->left != NULL)
{
inorderTraverseTree(pCur->left);
}
else
{
printf("%c ", pCur->data);
return;
}
printf("%c ", pCur->data); if(pCur->right != NULL)
{
inorderTraverseTree(pCur->right);
}
}
} //Create the binary tree
void createTree(tree_node * pRoot)
{
char ch = ;
tree_node * pCur = pRoot;
while((ch = getchar())!= 'e')
{
//printf("%c" , ch);
tree_node * pNewNode = (tree_node *)malloc(sizeof(tree_node));
pNewNode->left = NULL;
pNewNode->right = NULL;
pNewNode->father = NULL;
if(ch == 'L')
{
//printf("Input L\n");
pNewNode->data = getchar();
pNewNode->father = pCur;
pCur->left = pNewNode;
pCur = pNewNode;
}
else if(ch == 'R')
{
//printf("Input R\n");
pNewNode->data = getchar();
pNewNode->father = pCur;
pCur->right = pNewNode;
pCur = pNewNode;
}
else if(ch == 'B')
{
//printf("Input B\n");
free(pNewNode);
if(pCur->father != NULL)
pCur = pCur->father;
else
printf("It's the top\n");
}
}
}
构造这样一颗二叉树:

程序运行结果为:

二叉树中序遍历 (C语言实现)的更多相关文章
- 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)
题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- 10.26最后的模拟DAY2 改造二叉树[中序遍历+严格递增的最长不下降子序列]
改造二叉树 [题目描述] 小Y在学树论时看到了有关二叉树的介绍:在计算机科学中,二叉树是每个结点最多有两个子结点的有序树.通常子结点被称作“左孩子”和“右孩子”.二叉树被用作二叉搜索树和二叉堆.随后他 ...
- lintcode.67 二叉树中序遍历
二叉树的中序遍历 描述 笔记 数据 评测 给出一棵二叉树,返回其中序遍历 您在真实的面试中是否遇到过这个题? Yes 样例 给出二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,3, ...
- LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium
题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: struct TreeNode { int val; TreeNode* l ...
- [Leetcode] Binary tree inorder traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- 二叉树中序遍历,先序遍历,后序遍历(递归栈,非递归栈,Morris Traversal)
例题 中序遍历94. Binary Tree Inorder Traversal 先序遍历144. Binary Tree Preorder Traversal 后序遍历145. Binary Tre ...
- 39.Binary Tree Inorder Traversal(二叉树中序遍历)
Level: Medium 题目描述: Given a binary tree, return the inorder traversal of its nodes' values. 思路分析: ...
随机推荐
- 把CSV文件导入到SQL Server表中
保存数据库数据直接查询select * from tableName 在数据表格的左上角右击——将结果另存为选择路劲保存好的就是.csv格式的数据 有时候我们可能会把CSV中的数据导入到某个数据库的表 ...
- 同一张表不同SESSION相互持有对方记录引发的死锁
锁产生的原因:如果有两个会话,每个会话都持有另一个会话想要的资源,此时就会发生死锁. 同一张表不同SESSION持有不同记录 SQL> create table t1(id int); Tabl ...
- 数学(矩阵乘法,随机化算法):POJ 3318 Matrix Multiplication
Matrix Multiplication Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17783 Accepted: ...
- Android 开机自启动
首先实现开机自启动: 第一步创建一个广播接收者,如MyBootBroadcastReceiver.java package com.example; import android.content.Br ...
- 开源库CImg 数据格式存储之二(RGB 顺序)
在上一篇博客中已经初步说明了GDI和CImg数据的存储格式感谢博友 Imageshop 评论说明 CImg的说明文档中已有详细说明(详见上篇博客说明) CImg的数据格式确实是RRRGGGBBB顺序存 ...
- Template 使用注意问题和范例
1. 基本定义 模板是 2. 分类 2.1 函数模板 (1) 作用: 函数模板可以用来创建一个通用功能的函数,以支持多种不同形参,进一步简化重载函数的函数体设计. (2)申明方法: template ...
- 手游Apk破解疯狂,爱加密apk加固保护开发人员
2013年手游行业的规模与收入均实现了大幅增长,发展势头强劲.权威数据显示, 我国移动游戏市场实际销售收入从2012年的32.4亿猛增到2013年的112.4亿元,同比增长了246.9%,手游用户从2 ...
- 数据库框架 Litepal
1.导包 dependencies { compile 'org.litepal.android:core:1.4.1' } 2.在asstes中建立litepal.xml文件 <?xml ...
- 山东省赛A题:Rescue The Princess
http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3230 Description Several days ago, a beast caught ...
- IE6和IE7下绝对定位position:absolute和margin的冲突问题解决
绝对定位的Position:absoulte的元素,会让相邻的兄弟元素的margin-top失效.而如果去掉了兄弟元素的高度又会正常. <div id="layer1" st ...