#include <iostream>
#include <string>
using namespace std; class tree
{
public: tree()
{
//root = create();
root = NULL;
} /***输入扩展层次遍历序列,#表示该节点为空***/
tree(char *s)
{
root = NULL; if (s == NULL)
{
return;
} int size = strlen(s);
create(&root,s,0,size);
} ~tree()
{
/***清空二叉树***/
} /***二叉排序树:插入。***/
void insert(char *s)
{
if (s == NULL)
{
return;
} int size = strlen(s);
for (int i = 0; i<size; i++)
{
insert(&root, s[i]);
}
} void preOrder(){ pOrder(root);}
void inOreder(){ zOrder(root);}
void postOreder(){ hOrder(root);} private:
struct treenode
{
char data;
treenode * left;
treenode * right;
}; treenode *root; void insert(treenode **p, char s)
{
if (((*p) == NULL) && s != '\0')
{
*p = new treenode;
(*p)->data = s;
(*p)->left = NULL;
(*p)->right = NULL;
}
else
{
if ((*p)->data > s)
{
insert(&((*p)->left) , s);
}
else
{
insert(&((*p)->right) , s);
}
}
} treenode* create()
{
treenode *p;
char t;
cout<<"请输入:"<<endl;
t = getchar();
if (t=='#')
{
p = NULL;
}
else
{
p = new treenode;
p->data = t;
cout<<"create tree node: "<<t<<endl;
p->left = create();
p->right = create(); }
return p;
} void create(treenode **p, char *str, int i, int size)
{ if (i>size-1 || str[i] == '\0')
{
*p = NULL;
return;
} if (str[i] == '#')
{
*p=NULL;
}
else
{
*p = new treenode;
(*p)->data = str[i];
create(&((*p)->left),str,2*i+1,size);
create(&((*p)->right),str,2*i+2,size);
}
} void pOrder(treenode *p)
{
if (p==NULL)
{
return;
} cout<<p->data<<" "<<endl;
pOrder(p->left);
pOrder(p->right);
} void zOrder(treenode *p)
{
if (p==NULL)
{
return;
}
zOrder(p->left);
cout<<p->data<<" "<<endl;
zOrder(p->right);
} void hOrder(treenode *p)
{
if (p==NULL)
{
return;
}
hOrder(p->left);
cout<<p->data<<" "<<endl;
hOrder(p->right);
}
}; int main()
{
/***扩展层次序列简立树***/
//char t[9] = "ABCDE#FG";
//tree s(t);
//s.preOrder(); /***建立二叉排序树***/
tree s;
char t[9] = "57821639";
s.insert(t);
s.postOreder();
cout<<"Over"<<endl; return 0;
}

Cracking The Coding Interview 4.0_二叉树的更多相关文章

  1. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

  2. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  5. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  6. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  7. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  8. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  9. cracking the coding interview系列C#实现

    原版内容转自:CTCI面试系列——谷歌面试官经典作品 | 快课网 此系列为C#实现版本 谷歌面试官经典作品(CTCI)目录   1.1 判断一个字符串中的字符是否唯一 1.2 字符串翻转 1.3 去除 ...

随机推荐

  1. 在shell终端操作oracle数据库的常用命令

    这里面是在一个项目中用到的操作oracle数据库的常用linux命令,因为当时无法用plsql远程连接,大部分操作都需要在命令行窗口进行,总结一下 第一种方式 (1)先切换至sqlplus [orac ...

  2. sass制作雪碧图

    1.配置文件config.rb http_path = "../../../" css_dir = "Content/css" sass_dir = " ...

  3. 基于Lua语言的触动精灵脚本开发

    工具下载 官网地址 连接模拟器 studio连接 首先要先下载ADB模拟器连接IDE,注意,这里一定要用官网提供的ADB,安卓开发的adb不行!!! 下载好之后,打开studio,输入Access K ...

  4. Maximum Questions CodeForces - 900E (字符串,dp)

    大意:给定长$n$的字符串$s$, 只含'a','b','?', '?'可以替换为任意字符, 在给定长$t$的字符串, "ababab...", 求替换尽量少的'?', 使得$s$ ...

  5. 关于js原型链

    关于原型链,我们先贴上一张图(来自某知乎大佬专栏),然后听我娓娓道来. 先来说说什么是原型? JavaScript 中的对象有一个特殊的 [[Prototype]] 内置属性,其实就是对于其他对象的引 ...

  6. Oracle11g温习-第十六章:用户管理

    2013年4月27日 星期六 10:50 1.概念 (1)schema : user.object    就是用户创建的对象 (2)用户认证方式:                            ...

  7. webmagic 日志使用及maven项目中排除日志依赖

    我用的Spring Boot maven构建的工程,默认引入了 <dependency> <groupId>org.springframework.boot</group ...

  8. C/C++ 全局变量的访问

    #include <iostream> using namespace std; ; int main(int argc, char **argv) { ; std::cout <& ...

  9. linux下/proc/diskstats文件详解

    每一列的含义分别为: 第一列为 设备号 (number of issued reads. This is the total number of reads completed successfull ...

  10. zabbix_server.conf、zabbix_agentd.conf配置文件详解

    zabbix_server.conf配置文件详解 AlertScriptsPath 默认值:/usr/local/share/zabbix/alertscripts 说明:告警脚本目录 AllowRo ...