hdu3999The order of a Tree (二叉平衡树(AVL))
1. insert a key k to a empty tree, then the tree become a tree with
only one node;
2. insert a key k to a nonempty tree, if k is less than the root ,insert
it to the left sub-tree;else insert k to the right sub-tree.
We call the order of keys we insert “the order of a tree”,your task is,given a oder of a tree, find the order of a tree with the least lexicographic order that generate the same tree.Two trees are the same if and only if they have the same shape.
1 3 4 2
#include<stdio.h>
#include<malloc.h>
typedef struct tree
{
struct tree *lchilde,*rchilde;
int w;
}tree,*Tree;
void creaRoot(Tree &root)//创建根节点
{
root=(Tree)malloc(sizeof(tree));
root->lchilde=root->rchilde=NULL;
}
void printTree(Tree root,int k)//遍历树并输出该节点的值
{
if(root==NULL) return ;//为空时反回
if(k==0)
printf("%d",root->w);
else
printf(" %d",root->w);
printTree(root->lchilde,k+1);//先搜左
printTree(root->rchilde,k+1);//后搜右
}
void InTree(Tree &node,int x)//建立二叉树,比当前节点小的放左边,否则放右边
{
Tree T;
if(node==NULL)//为空时,就把该值放到该节点
{
T=(Tree)malloc(sizeof (tree));
T->w=x; T->lchilde=T->rchilde=NULL;//把左右孩子置为空
node=T;
}
else//不为空,继续搜
{
if(x<=node->w)
InTree(node->lchilde,x);
else
InTree(node->rchilde,x);
}
}
int main()
{
Tree root;
int n,i,x;
while(scanf("%d",&n)>0)
{
if(n==0)continue;
creaRoot(root);
scanf("%d",&x);
root->w=x;
for(i=2;i<=n;i++)
{
scanf("%d",&x);
InTree(root,x);
}
printTree(root,0);
printf("\n");
}
}
hdu3999The order of a Tree (二叉平衡树(AVL))的更多相关文章
- Algorithms: 二叉平衡树(AVL)
二叉平衡树(AVL): 这个数据结构我在三月份学数据结构结构的时候遇到过.但当时没调通.也就没写下来.前几天要用的时候给调好了!详细AVL是什么,我就不介绍了,维基百科都有. 后面两月又要忙了. ...
- 二叉平衡树AVL的插入与删除(java实现)
二叉平衡树 全图基础解释参考链接:http://btechsmartclass.com/data_structures/avl-trees.html 二叉平衡树:https://www.cnblogs ...
- hdu 3999 The order of a Tree (二叉搜索树)
/****************************************************************** 题目: The order of a Tree(hdu 3999 ...
- HDU 3999 The order of a Tree 二叉搜索树 BST
建一个二叉搜索树,然后前序输出. 用链表建的,发现很久没做都快忘了... #include <cstdio> #include <cstdlib> struct Node{ i ...
- (4) 二叉平衡树, AVL树
1.为什么要有平衡二叉树? 上一节我们讲了一般的二叉查找树, 其期望深度为O(log2n), 其各操作的时间复杂度O(log2n)同时也是由此决定的.但是在某些情况下(如在插入的序列是有序的时候), ...
- 树-二叉平衡树AVL
基本概念 AVL树:树中任何节点的两个子树的高度最大差别为1. AVL树的查找.插入和删除在平均和最坏情况下都是O(logn). AVL实现 AVL树的节点包括的几个组成对象: (01) key -- ...
- java项目---用java实现二叉平衡树(AVL树)并打印结果(详)(3星)
package Demo; public class AVLtree { private Node root; //首先定义根节点 private static class Node{ //定义Nod ...
- 各种查找算法的选用分析(顺序查找、二分查找、二叉平衡树、B树、红黑树、B+树)
目录 顺序查找 二分查找 二叉平衡树 B树 红黑树 B+树 参考文档 顺序查找 给你一组数,最自然的效率最低的查找算法是顺序查找--从头到尾挨个挨个遍历查找,它的时间复杂度为O(n). 二分查找 而另 ...
- 判断一颗二叉树是否为二叉平衡树 python 代码
输入一颗二叉树,判断这棵树是否为二叉平衡树.首先来看一下二叉平衡树的概念:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树.因此判断一颗二叉平衡树的关键在于 ...
随机推荐
- Linux下gsoap实现webservice功能
蓝字为关键字,等号=后面为关键字值. 一.介绍 我们用的webservice是根据gsoap编译工具来实现,gSOAP的编译器能够自动的将用户定义的本地化的C或C++数据类型转变为符合XML语法的数据 ...
- PROPAGATION_REQUIRED
PROPAGATION_REQUIRED (2009-05-13 13:26:52) 转载▼ 事务传播行为种类 Spring在TransactionDefinition接口中规定了7种类型的事务传 ...
- iOS/Xcode异常:reason = “The model used to open the store is incompatible with the one used to create the store”
reason=The model used to open the store is incompatible with the one used to create the store 出现上述异常 ...
- 免费利用网页版谷歌翻译实现任意语言转换php版
本文源发布地址: http://ourgarden.cn/2013/07/20/%E5%85%8D%E8%B4%B9%E5%88%A9%E7%94%A8%E7%BD%91%E9%A1%B5%E7%89 ...
- org.tigris.subversion.javahl.ClientException: Attempted to lock an already-locked dir svn: Working c
Eclipse插入svn提交出现:org.tigris.subversion.javahl.ClientException: Attempted to lock an already-locked d ...
- STL之Queue(Q)
STL的Queue(数据结构中的队列): 特点:FIFO 先进先出: 自适应容器(即容器适配器) 栈适配器STL queue STL中实现的Queue: 用list来实现queue: queue ...
- 虚拟rethat联网问题
近日在vmware虚拟了一台rethat的linux,但是使用桥连也上不了网! 宿主机是win7,由于在公司,是采用固定ip上网的,通过上网查资料,终于可以连接网络了: 步骤如下: 第一:修改vi / ...
- python3.4.3将汉字转换为大写拼音首字母
from pypinyin import pinyin a=pinyin(u'杨强',type=FIRST_LETTER) --->此时返回一个列表并赋给a(元素也是列表) b=[] - ...
- ASP.NET MVC 5 学习教程:创建连接字符串
原文 ASP.NET MVC 5 学习教程:创建连接字符串 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 ...
- HDU 4738 双连通模版题
九野的博客,转载请注明出处:http://blog.csdn.net/acmmmm/article/details/11711577 题意:给定n个点,m条无向边 下面m行表示u , v ,边权值 求 ...