#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. SpringCloud 之 Hystrix熔断器

    Hystrix  Hystrix请求熔断与服务降级 Hystrix线程隔离&请求缓存&请求合并

  2. 一、win+git安装

    最近公司版本控制准备弃用svn,采用git.所以在个人系统安装玩了下,留点爪印... 1.下载最新的 git 包(根据电脑系统) 官网地址:https://git-scm.com/download/w ...

  3. 雷林鹏分享: XML CDATA

    XML CDATA XML 文档中的所有文本均会被解析器解析. 只有 CDATA 区段中的文本会被解析器忽略. PCDATA - 被解析的字符数据 XML 解析器通常会解析 XML 文档中所有的文本. ...

  4. induced pluripotent stem cell (iPSC) 诱导性多能干细胞

    参考: 诱导性多能干细胞 Induced pluripotent stem cell Induced Pluripotent Stem Cells: Problems and Advantages w ...

  5. English Voice of << Count on me >>

    Count On Me 歌手:Bruno Mars 所属专辑:It´s Better If You Don´t Understand If you ever find yourself stuck i ...

  6. LeetCode--404--左叶子之和

    问题描述: 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 方法:recursive class ...

  7. android ------- 运行官方NDK 案例HelloJNI

    下载案例 HelloJNI ,导入工程到Eclipse,  可以直接下载我的案例, 源码下载:https://github.com/DickyQie/android-ndk 目录图 使用命令生成  . ...

  8. python3-----多进程、多线程、多协程

    目前计算机程序一般会遇到两类I/O:硬盘I/O和网络I/O.我就针对网络I/O的场景分析下python3下进程.线程.协程效率的对比.进程采用multiprocessing.Pool进程池,线程是自己 ...

  9. Mysql中Join用法及优化

    Join的几种类型 笛卡尔积(交叉连接) 如果A表有n条记录,B表有m条记录,笛卡尔积产生的结果就会产生n*m条记录.在MySQL中可以为CROSS JOIN或者省略CROSS即JOIN,或者直接用f ...

  10. 小程序模板中data传值有无...

    A:<template is="gemSelectColor" data="{{optionData}}" />B:<template is= ...