数据结构的堆和栈 非完全二叉树

堆和栈的理解和区别,C语言堆和栈完全攻略 http://c.biancheng.net/c/stack/ 

数据结构的堆和栈

在数据结构中,栈是一种可以实现“先进后出”(或者称为“后进先出”)的存储结构。假设给定栈 S=(a0,a1,…,an-1),则称 a0 为栈底,an-1 为栈顶。进栈则按照 a0,a1,…,an-1 的顺序进行进栈;而出栈的顺序则需要反过来,按照“后存放的先取,先存放的后取”的原则进行,则 an-1 先退出栈,然后 an-2 才能够退出,最后再退出 a0

在实际编程中,可以通过两种方式来实现:使用数组的形式来实现栈,这种栈也称为静态栈;使用链表的形式来实现栈,这种栈也称为动态栈。

相对于栈的“先进后出”特性,堆则是一种经过排序的树形数据结构,常用来实现优先队列等。假设有一个集合 K={k0,k1,…,kn-1},把它的所有元素按完全二叉树的顺序存放在一个数组中,并且满足:

Python - DS Introduction - Tutorialspoint https://www.tutorialspoint.com/python_data_structure/python_data_structure_introduction.htm

Liner Data Structures

These are the data structures which store the data elements in a sequential manner.

  • Array: It is a sequential arrangement of data elements paired with the index of the data element.
  • Linked List: Each data element contains a link to another element along with the data present in it.
  • Stack: It is a data structure which follows only to specific order of operation. LIFO(last in First Out) or FILO(First in Last Out).
  • Queue: It is similar to Stack but the order of operation is only FIFO(First In First Out).
  • Matrix: It is two dimensional data structure in which the data element is referred by a pair of indices.

Non-Liner Data Structures

These are the data structures in which there is no sequential linking of data elements. Any pair or group of data elements can be linked to each other and can be accessed without a strict sequence.

  • Binary Tree: It is a data structure where each data element can be connected to maximum two other data elements and it starts with a root node.
  • Heap: It is a special case of Tree data structure where the data in the parent node is either strictly greater than/ equal to the child nodes or strictly less than it’s child nodes.
  • Hash Table: It is a data structure which is made of arrays associated with each other using a hash function. It retrieves values using keys rather than index from a data element.
  • Graph: .It is an arrangement of vertices and nodes where some of the nodes are connected to each other through links.

栈 stack是线性的数据结构

堆 heap是非线性的数据结构

完全二叉树

https://baike.baidu.com/item/完全二叉树/7773232

完全二叉树是效率很高的数据结构,完全二叉树是由 满二叉树而引出来的。对于深度为K的,有n个结点的二叉树,当且仅当其每一个结点都与深度为K的满二叉树中编号从1至n的结点一一对应时称之为完全二叉树。
 
 
中文名
完全二叉树
外文名
Complete Binary Tree
实    质
效率很高的数据结构
特    点
叶子结点只可能在最大的两层出现
性    质
度为1的点只有1个或0个
应用学科
计算机科学

算法思路

判断一棵树是否是完全二叉树的思路
1>如果树为空,则直接返回错
  2>如果树不为空:层序遍历二叉树
  2.1>如果一个结点左右孩子都不为空,则pop该节点,将其左右孩子入队列;
  2.1>如果遇到一个结点,左孩子为空,右孩子不为空,则该树一定不是完全二叉树;
  2.2>如果遇到一个结点,左孩子不为空,右孩子为空;或者左右孩子都为空;则该节点之后的队列中的结点都为叶子节点;该树才是完全二叉树,否则就不是完全二叉树;
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/yuanjiangw/p/12441335.html