//Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height.
//
// 译文:
//
// 给定一个有序数组(递增),写程序构建一棵具有最小高度的二叉树。 #include <iostream>
#include <string>
#include <queue>
using namespace std; class tree
{
public: tree()
{
//root = create();
root = NULL;
index = 0;
} /***输入扩展层次遍历序列,#表示该节点为空***/
tree(char *s)
{
root = NULL;
index = 0;
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 createBanlance(char *s)
{
int size = strlen(s); createB(&root, s, 0, size-1);
cout<<"End"<<endl;
} void levelOrder()
{
queue<treenode *> q;
if (root != NULL)
{
q.push(root);
}
while(!q.empty())
{
treenode *t = q.front();
cout<<t->data<<endl;
q.pop();
if (t->left != NULL)
{
q.push(t->left);
}
if (t->right != NULL)
{
q.push(t->right);
}
}
} bool isBanlance(int size)
{
int *s = new int[size];
depth(root,s,size,0);
s[index+1] = '\0';
int i = 0;
int max = s[0];
int min = s[0];
while(s[i]!='\0')
{
if (s[i]>max)
{
max = s[i];
}
if (s[i]<min)
{
min = s[i];
}
i++;
}
return ((max - min)>1? 0:1);
} void preOrder(){ pOrder(root);}
void inOreder(){ zOrder(root);}
void postOreder(){ hOrder(root);} private:
struct treenode
{
char data;
treenode * left;
treenode * right;
}; treenode *root;
int index; 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);
}
}
}
/**取begin,end中间值做根节点,两值相等相等为叶节点,大于为空**/
void createB(treenode **p, char *s, int begin, int end)
{
if (begin>end)
{
*p = NULL;
return;
} else if (begin == end)
{
*p = new treenode;
(*p)->data = s[end];
(*p)->left = (*p)->right = NULL; }
else
{
*p = new treenode;
int mid = (begin + end)/2;
(*p)->data = s[mid];
cout<<s[mid]<<endl; createB(&((*p)->left), s, begin, mid -1); createB(&((*p)->right), s, mid +1, end); }
} void depth(treenode *s, int *str,int size,int k)
{
if (s==NULL)
{
return;
}
if (s->left == NULL && s->right == NULL)
{
str[index]= k;
index++;
}
else
{
depth(s->left,str, size, k+1);
depth(s->right,str, size, k+1);
}
} 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[8] = "3289654";
//s.insert(t);
// s.postOreder();
/**4.1*************************/
//cout<<"is Banlance? "<<s.isBanlance(8)<<endl; /**4.3*************************/
tree s;
char t[9] = "12345678";
s.createBanlance(t); s.preOrder();
cout<<"Over"<<endl; /**非递归层次遍历*************************/
/*tree s;
char t[8] = "3289654";
s.insert(t);
s.levelOrder();*/ return 0;
}

Cracking The Coding Interview4.3的更多相关文章

  1. Cracking The Coding Interview4.8

    //You are given a binary tree in which each node contains a value. Design an algorithm to print all ...

  2. Cracking The Coding Interview4.5

    //原文: // // Write an algorithm to find the 'next' node (i.e., in-order successor) of a given node in ...

  3. Cracking the coding interview

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

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

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

  5. Cracking the coding interview--问题与解答

    http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...

  6. 《cracking the coding intreview》——链表

    前言 最近准备暑假回家回家修整一下,所以时间大部分用来完成项目上的工作,同时为了9月份的校招,晚上的时间我还在学习<cracking the coding intreview>,第二章链表 ...

  7. Cracking the Coding Interview(Trees and Graphs)

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

  8. 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 ...

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

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

随机推荐

  1. (转)c#反射

    1. 什么是反射2. 命名空间与装配件的关系3. 运行期得到类型信息有什么用4. 如何使用反射获取类型5. 如何根据类型来动态创建对象6. 如何获取方法以及动态调用方法7. 动态创建委托 1.什么是反 ...

  2. Spring AOP 理论

    一.AOP AOP 产生的背景 “存在即合理”,任何一种理论或技术的产生,必然有它的原因.了解它产生的背景.为了解决的问题有助于我们更好地把握AOP的概念. 软件开发一直在寻求一种高效开发.护展.维护 ...

  3. HTML 第十二章总结

    HTML5 markup 前言 在这一章中,讲解了新的 HTML5 的一些 markup,并且对其中的<video>进行了很详细的讲解. New HTML5 elements 列表如下: ...

  4. sql---->sql-summary&mysql-summary

    数据库操作: 1.创建数据库,并修改默认字符编码 create database 数据库名 [charset=字符编码]; ee: create database dog charset=utf8; ...

  5. 20171104xlVBA各人各科进退

    Sub 各班个人各科进步幅度() Dim dRank As Object Set dRank = CreateObject("Scripting.Dictionary") Dim ...

  6. Spring Batch 基本的批处理指导原则

    下面是一些关键的指导原则,可以在构批量处理解决方案可以参考: 请记住,通常皮脸处理体系结构将会影响在线应用的体系结构,同时反过来也是一样的.在你为批量任务和在线应用进行设计架构和环境的时候请尽可能的使 ...

  7. Confluence 6 教程:空间高手

    这个教程将会带你如何在 Confluence 中创建和自定义空间,同时也包括如何删除空间,如果需要的话.通过这个教程,你将成为使用空间的高手. 你需要具有创建空间(Create space)和创建个人 ...

  8. VS Code行内样式提示插件

    打开vscode,在软件界面左下角找到“齿轮”标志并点击,在弹出的菜单中选择“设置”,把下面的代码添加到设置里. { "workbench.colorTheme": "C ...

  9. HDU - 4780费用流

    题意:M台机器要生产n个糖果,糖果i的生产区间在(si, ti),花费是k(pi-si),pi是实际开始生产的时间机器,j从初始化到生产糖果i所需的时间Cij,花费是Dij,任意机器从生产糖果i到生产 ...

  10. Matlab-5:牛顿迭代法工具箱

    function [f,L]=Newton(f,a) %this is newton teration whic is used for solving implicit One-dimensiona ...