[Depth understanding of the Java Collections Framework] - Data structure

data structure

Common structure data are stored: stacks, queues, arrays, linked lists, and binary trees.

Stack

  • Stack : it is limited linear operation table, with the restriction that only permits insertion and deletion operations in the subject at one end, is not allowed to add any other location, search, and delete operations.

Briefly: using the set of structure elements have access to the following features

  • Last-out (i.e., into the memory element to the element after it has successively taken back, in order to remove the element). For example, the bullet pressed into the clip, the first bullet under pressure to go after pressing into the bullets above, when shot, the first bullet above pop, pop before following bullets.

  • Stack inlet, an outlet stack is the top position.

Here the two terms should be noted:

  • Push : that is stored elements. That is, the storage element to the top of stack location, already in the stack elements sequentially moves one place to the direction of bottom of the stack.
  • Popped : it is to take elements. That is, the position of the top stack element is taken out, the elements already in the stack sequentially moves one place to the direction of stack.

queue

  • Queue : It is the same as a stack, the linear form is also a restricted operation, the restriction that only permits insertion of the end of the table, and delete the table at the other end.

Briefly, using the set of structure elements have access to the following features:

  • FIFO (i.e., into the memory element to the front element after it has successively taken out, in order to remove the element). For example, the train went through the tunnel, the front go in, go in the rear; the front first came out, came out after the rear.
  • The ingress queue, half and an outlet side. For example, the left side of the figure is an inlet, an outlet for the right side.

Array

  • Array : Array , is an ordered sequence of elements, an array is a contiguous open space in memory, and storage elements in this space. The B & B is like a discharge, there are 100 rooms, has a fixed number from 001,100 to each room by number can quickly find people to rent.

Briefly, using the set of structure elements have access to the following features:

  • Finding elements quickly: by the index, you can quickly access the location of the element

  • Add or delete elements slow

  • 指定索引位置增加元素:需要创建一个新数组,将指定新元素存储在指定索引位置,再把原数组元素根据索引,复制到新数组对应索引的位置。如下图
    在这里插入图片描述

  • **指定索引位置删除元素:**需要创建一个新数组,把原数组元素根据索引,复制到新数组对应索引的位置,原数组中指定索引位置元素不复制到新数组中。如下图

在这里插入图片描述

链表

  • 链表:由一系列结点node(链表中每一个元素称为结点)组成,结点可以在运行时i动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。我们常说的链表结构有单向链表双向链表

  • 双向链表

  • 单向链表

简单的说,采用该结构的集合,对元素的存取有如下的特点 (使用单项链表说明)

  • 多个结点之间,通过地址进行连接。例如,多个人手拉手,每个人使用自己的右手拉住下个人的左手,依次类推,这样多个人就连在一起了。

  • 查找元素慢:想查找某个元素,需要通过连接的节点,依次向后查找指定元素

  • 增删元素快:

    • 增加元素:只需要修改连接下个元素的地址即可。

    • 删除元素:只需要修改连接下个元素的地址即可。

二叉树

  • 二叉树binary tree ,是每个结点不超过2的有序树(tree)

(1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;
(2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;
(3)左、右子树也分别为二叉排序树;
(4)没有键值相等的结点。

简单的理解,就是一种类似于我们生活中树的结构,只不过每个结点上都最多只能有两个子结点。

二叉树是每个节点最多有两个子树的树结构。顶上的叫根结点,两边被称作“左子树”和“右子树”。

如图:

在这里插入图片描述

  • 二叉树查找
  1. 若根结点的关键字值等于查找的关键字,查找成功。
  2. 否则,若小于根结点的关键字值,递归查左子树。
  3. 若大于根结点的关键字值,递归查右子树。
  4. 若子树为空,查找不成功。
发布了61 篇原创文章 · 获赞 55 · 访问量 3万+

Guess you like

Origin blog.csdn.net/qq_18604209/article/details/104429182