![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
树
文章平均质量分 73
二叉树、BST、AVL...
zadarmo_
这个作者很懒,什么都没留下…
展开
-
【高级数据结构】Trie树
高效地存储和查询字符串的数据结构。所以其重点在于:存储、查询两个操作。原创 2024-03-02 20:08:22 · 632 阅读 · 0 评论 -
单链表、树、图的存储(数组存储)
一、单链表1. 存储方式(不总结结构体存储)单链表的存储,主要是以下几个方面:存储每个点的值e[i]存储每个结点的下一个结点ne[i]存储头结点head用这3个变量去模拟链表。其中每个结点用数组下标去索引,即每个点对应一个下标。相应的,ne指针就指向下一个结点的下标,而非地址。/*** head : 指向 头结点的下标* e[i] : 下标为i的点的值* ne[i] : 下标为i的点 的 下一个结点的下标* idx : 结点个数。[0, idx)的每个数对应一个结点*/int原创 2020-09-28 14:01:02 · 1049 阅读 · 0 评论 -
树的直径
一、 概念树的直径:树中两个叶子结点距离的最大值。二、代码实现1. 任取一个点,找到距离该点距离最远的点,记为u2. 找到距离u最远的点,该距离即为树的直径struct edge { int id, w; // 点的下标和边的权重 edge(int a, int b) { id = a; w = b; }};vector<edge> h; // 用邻接表存图...原创 2020-03-18 23:36:16 · 141 阅读 · 0 评论 -
1004 Counting Leaves (30 分)——甲级(dfs)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.Input Specification:Each input file contains one test case. Each case starts w...原创 2019-08-06 00:00:10 · 89 阅读 · 0 评论 -
最小生成树算法——Prim算法和Kruskal算法
Prim算法该算法是一颗小树逐渐变大的过程。每次找到距树距离最小的点,最后加起来的权值就会最小(贪心)。设V是图中所有点构成的集合,U是生成树的点构成的集合,lowcost[j]表示结点j到生成树的最小距离(即与生成树中所有距离的最小值)。其步骤如下:确定数据结构。用邻接矩阵e[i][j]表示有权图,如果i,j之间存在边,则e[i][j]为 边权值;否则为无穷大INF。用bool数组...原创 2019-07-26 09:59:58 · 1276 阅读 · 0 评论 -
1064 Complete Binary Search Tree (30 分)——甲级(完全二叉搜索树)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of...原创 2019-08-15 22:27:31 · 122 阅读 · 0 评论 -
1043 Is It a Binary Search Tree (25 分)——甲级(二叉搜索树)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:If we swap the left and right subtrees of every node, then the resulting tree is called the Mirr...原创 2019-08-15 21:50:16 · 116 阅读 · 0 评论 -
1099 Build A Binary Search Tree (30 分)——甲级:建树,输出层序(二叉搜索树)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:Given the structure of a binary tree and a sequence of distinct integer keys, there is only one ...原创 2019-08-13 18:38:34 · 940 阅读 · 0 评论 -
二叉搜索树(BST)
定义二叉搜索树:一棵二叉树,可以为空;如果不为空,满足下一下性质:非空左子树的所有键值小于其根节点的键值。非空右子树的所有键值大于其根结点的键值。左、右子树都时二叉搜索树。(可以理解为用树的数据结构实现了二分查找)操作查找元素X找最大值max,最小值min插入元素X删除元素X/* 二叉搜索树结点定义 */typedef struct node* bst;s...原创 2019-08-13 18:26:10 · 198 阅读 · 0 评论 -
1123 Is It a Complete AVL Tree (30 分)——甲级(完全平衡二叉树)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is...原创 2019-08-16 18:03:55 · 546 阅读 · 1 评论 -
平衡二叉树(AVL树)
基本概念ASL:一颗搜索树查找元素的平均查找次数。平衡因子(BF):BF(T) = hL–hR。其中hL,hR分别为左右子树高度。定义平衡二叉树也是一颗搜索树,它的ASL相对来说会小一些。满足以下特性:是一颗空树,或者任一结点左、右子树高度差的绝对值不超过1,即|BF(T)|≤1。/* AVL树结点定义 */typedef struct node* AVLTree;st...原创 2019-08-16 16:43:38 · 404 阅读 · 0 评论 -
1127 ZigZagging on a Tree (30 分)——甲级(二叉树蛇形输出)
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple stand...原创 2019-08-16 19:22:21 · 201 阅读 · 0 评论 -
1020 Tree Traversals (25 分)——甲级:已知后序、中序,输出层序(二叉树、对于建树递归算法的理解)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the cor...原创 2019-08-13 16:43:20 · 128 阅读 · 0 评论 -
1102 Invert a Binary Tree (25 分)——甲级:建树输出中序、层序(二叉树镜像输出)
The following is from Max Howell @twitter:Now it’s your turn to prove that YOU CAN invert a binary tree!Input Specification:Each input file contains one test case. For each case, the first line gi...原创 2019-08-13 15:02:02 · 281 阅读 · 3 评论 -
给定二叉树的前序和后序,判断二叉树是否唯一?
对于一棵二叉树,如果给定中序和前序后序任意一个,是可以确定这个树的形状的。例如给定前序和中序:前序第一个结点肯定是根节点,后面是:1.左子树、右子树2.只有左子树3.只有右子树对于前序中的根节点,在中序找找到位置后,他的左边有元素就有左子树,右边有元素就有右子树。所以结合以上两点是可以唯一确定一棵二叉树的。而如果把中序换成后序: 因为后序是左-右-根,无法从后序中判断前序中根节点...原创 2019-08-13 16:47:26 · 11368 阅读 · 4 评论 -
二叉树遍历:已知前、中求后序;已知后、中求前序。
首先给出一颗树和树的遍历方式:前序:1、2、3、4、5、6、7中序:3、2、4、1、6、7、5后序:3、4、2、6、7、5、1层序:1、2、5、3、4、6、71.已知前序、中序,求后序分析:对于前序的每一个子部分来说,先是根,然后后面连续的若干个是左子树的所有节点,然后是右子树的所有节点。如:1(根节点),2、3、4(左子树节点),5、6、7(右子树节点)。对于中序的每一个子部分...原创 2019-06-22 16:40:28 · 3312 阅读 · 1 评论