根据二叉树的先序遍历建立二叉树
来源:互联网 发布:儿童学唱歌软件 编辑:程序博客网 时间:2024/05/18 12:31
#include<iostream>
#include<queue>
//#include<stdio.h>
using namespace std;
#define Elemtype char
typedef struct node
{
Elemtype data;
node* left_child;
node* right_child;
}node,*BST;
void build_pre(node*& one_node,queue<Elemtype>& q)//入口这里的指针意思是有意愿在某处建立结点
{
if (q.empty() != true)
{
if (q.front() == '#')
{
one_node = NULL;//这个结点指向空,空不可访问,所以不建立这个节点(子树)
q.pop();
}
else
{
one_node = (node*)malloc(sizeof(node));//造结点建立结点的过程
one_node->data = q.front();
one_node->left_child = NULL;
one_node->right_child = NULL;
q.pop();
build_pre(one_node->left_child, q);//建立左孩子(左子树)
build_pre(one_node->right_child, q);//建立右孩子(右子树)
}
}
}
int main()
{
BST tree = NULL;//指向树的根节点,此时是空树(什么都没有,不存在树)
queue<Elemtype> q;
int n = 0;
cout << "input n" << endl;
cin >> n;
cout << "input nums" << endl;
for (int i = 0; i < n; i++)
{
Elemtype temp;
cin >> temp;
q.push(temp);
}
build_pre(tree, q);
return 0;
}
#include<queue>
//#include<stdio.h>
using namespace std;
#define Elemtype char
typedef struct node
{
Elemtype data;
node* left_child;
node* right_child;
}node,*BST;
void build_pre(node*& one_node,queue<Elemtype>& q)//入口这里的指针意思是有意愿在某处建立结点
{
if (q.empty() != true)
{
if (q.front() == '#')
{
one_node = NULL;//这个结点指向空,空不可访问,所以不建立这个节点(子树)
q.pop();
}
else
{
one_node = (node*)malloc(sizeof(node));//造结点建立结点的过程
one_node->data = q.front();
one_node->left_child = NULL;
one_node->right_child = NULL;
q.pop();
build_pre(one_node->left_child, q);//建立左孩子(左子树)
build_pre(one_node->right_child, q);//建立右孩子(右子树)
}
}
}
int main()
{
BST tree = NULL;//指向树的根节点,此时是空树(什么都没有,不存在树)
queue<Elemtype> q;
int n = 0;
cout << "input n" << endl;
cin >> n;
cout << "input nums" << endl;
for (int i = 0; i < n; i++)
{
Elemtype temp;
cin >> temp;
q.push(temp);
}
build_pre(tree, q);
return 0;
}
阅读全文
0 0
- 根据二叉树的先序遍历建立二叉树
- 根据二叉树的先序遍历和中序遍历建立二叉树
- 根据先序遍历建立一个二叉树
- 二叉树的先序建立及先序遍历:
- 数据结构--java实现二叉树的先序、中序、后序、层次遍历及根据先序中序建立二叉树
- 根据先序遍历和中序遍历建立二叉树
- //二叉树的(二叉排序树)建立和先序遍历
- 根据二叉树的先序遍历和中序遍历重构二叉树
- 数组实现根据二叉树的先序遍历和中序遍历构造二叉树
- 根据二叉树的先序遍历和中序遍历重建二叉树
- HDU 5444 二叉树的遍历,先根据先序遍历建树
- 根据先序和中序或后序和中序建立二叉树及树的遍历
- 根据二叉树的先序、中序遍历结果重建二叉树
- 根据二叉树的先序和中序遍历还原二叉树
- 根据二叉树的先序和中序遍历重建二叉树
- 根据二叉树的先序、中序遍历结果重建二叉树
- 根据二叉树的先序、中序遍历结果重建二叉树
- 建立二叉树,实现二叉树的先序,中序遍历的递归算法
- leetcode解题方案--030--Substring with Concatenation of All Words
- 田忌赛马
- ESP8266解析JSON数据
- Pace 8 (C语言实现扫雷小游戏)
- JAVA中的Socket一对一聊天程序
- 根据二叉树的先序遍历建立二叉树
- PHP 构造方法 __construct()
- 171111 Learning Python Chapter 25 OOP-The Big Picture
- SpringAop 9 (面向切面编程,常用于拦截器)
- Android学习之隐藏activity标题栏
- ThinkPHP框架整合phpqrcode生成二维码DEMO
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- Linux ./configure && make && make install 编译安装和卸载