数据结构
浙江大学慕课
时间复杂度 大O表示法
1 < logn < n < nlogn < n^2 < 2^n
空间复杂度
既然时间复杂度不是用来计算程序具体耗时的,那么我也应该明白,空间复杂度也不是用来计算程序实际占用的空间的。
空间复杂度是对一个算法在运行过程中临时占用存储空间大小的一个量度,同样反映的是一个趋势,我们用 S(n) 来定义。
什么是数据结构
程序 = 数据结构+算法。
数据结构主要讲解数据的组织形式。就是我们怎么样把这些数据存储起来,所以有数组、链表、栈、队列、树、图,这是数据结构的重点。
算法,则注重的是思想。比如数组里的元素怎么排序、怎么找到最大的数和最小的数等等。说白了就是解决现实中问题的思想。所有才会有贪心、动态规范等这些算法。
常见的数据结构与算法
算法
- 算法基本思想
- 回溯算法
- 分治算法
- 贪心算法
- 动态规划
- 搜索算法
- 广度优先搜索
- 深度优先搜索
- 查找算法
- 二分查找
- 散列表查找
- 树结构查找
- 字符串匹配算法
- 暴力匹配
- KMP算法
- 10大排序算法
- O(n2)
- 冒泡排序
- 选择排序
- 插入排序
- 希尔排序
- O(nlogn)
- 快速排序
- 归并排序
- 堆排序
- O(n+k)
- 计数排序
- 基数排序
- 桶排序
- O(n2)
复杂度分析
- 时间复杂度
- 空间复杂度
- 最好
- 最坏
- 平均
数据结构
- 数组
- 数组操作
- 链表
- 单链表
- 双向链表
- 循环链表
- 双向循环链表
- 栈
- 队列
- 普通队列
- 循环队列
- 字符串
- 空串
- 空格串
- 子串
- 主串
- 哈希表
- 哈希函数
- 哈希冲突
- 开放定址法
- 再哈希函数法
- 链地址法
- 树
- 二叉树
- 满二叉树
- 完全二叉树
- 二叉搜索树
- 平衡二叉树
- 红黑树
- 多路查找树
- B树
- B+树
- 堆
- 小顶堆
- 大顶堆
- 优先队列
- 二叉树
- 图
- 最小生成树
- 关键路径
- 最短路径
- 拓扑排序
常用数据结构,算法及概念
1.链表:链式结构,把一组内存不连续的数据(当然也可以连续),通过指针串联起来,链表中存储的数据称为数据域,指针域也可以叫做链,是指储存下一个节点的内存地址。
单链表:单链表的next指向下一个节点的引用地址,尾结点的next为空
双链表:与单链表不同的是,node节点中会有next和previous两个引用存储
环形链表:特点是尾结点的下一个节点会指向头结点。
链表的增删改查:
增加头结点:将该节点的next设为头结点,然后将头结点设为该节点;
增加尾结点:将尾结点的next设为该节点即可
增加中间节点:确定插入位置,将该节点next设下一个节点,将该节点设为
删除头结点:将头结点的下一个节点设为头结点
删除尾结点:需遍历,找到后将尾结点上一个设为尾结点
删除中间节点:删除节点前一个节点的next指向 删除节点的next
修改:遍历找到修改数据域的数据
查找:从头结点一节一节往下遍历
public class SingleLinkedListNode<T>
{
public T Value { get; set; }
public SingleLinkedListNode<T> Next { get; set; }
public SingleLinkedListNode()
{
Value = default(T);
Next = null;
}
}
interface ISingleLinkedListFunction<T>
{
SingleLinkedListNode<T> First { get; }
SingleLinkedListNode<T> Last { get; }
int Count { get; }
bool IsEmpty { get; }
void Clear();
bool Contains(T value);
SingleLinkedListNode<T> AddFirst(T value);
SingleLinkedListNode<T> AddLast(T value);
SingleLinkedListNode<T> AddFirst(SingleLinkedListNode<T> node);
SingleLinkedListNode<T> AddLast(SingleLinkedListNode<T> node);
SingleLinkedListNode<T> Insert(SingleLinkedListNode<T> node);
bool Delete(T value);
bool DeleteAt(int index);
bool Delete(LinkedListNode<T> node);
bool DeleteFirst();
bool DeleteLast();
SingleLinkedListNode<T> Find(T value);
SingleLinkedListNode<T> FindPrevious(T value);
T this[int index] { get; }
int IndexOf(int value);
}
public class SingleLinkedList<T> : ISingleLinkedListFunction<T>
{
public T this[int index]
{
get
{
if (index >= Count || index < 0)
{
return default(T);
}
SingleLinkedListNode<T> tempNode = first;
for (int i = 0; i < index; i++)
{
tempNode = tempNode.Next;
}
return tempNode.Value;
}
}
private SingleLinkedListNode<T> first = null;
public SingleLinkedListNode<T> First
{
get
{
return first;
}
}
private SingleLinkedListNode<T> last = null;
public SingleLinkedListNode<T> Last
{
get
{
return last;
}
}
private int count;
public int Count
{
get
{
count = GetCount();
return count;
}
}
private int GetCount()
{
throw new NotImplementedException();
}
private bool isEmpty;
public bool IsEmpty
{
get
{
isEmpty = GetIsEmpty();
return isEmpty;
}
}
private bool GetIsEmpty()
{
throw new NotImplementedException();
}
public SingleLinkedListNode<T> AddFirst(T value)
{
SingleLinkedListNode<T> node = new SingleLinkedListNode<T>();
if (first == null)
{
first = node;
last = node;
}
else
{
node.Next = first;
node.Value = value;
first = node;
}
return node;
}
public SingleLinkedListNode<T> AddFirst(SingleLinkedListNode<T> node)
{
if (first == null)
{
first = node;
last = node;
}
else
{
node.Next = first;
first = node;
}
return node;
}
public SingleLinkedListNode<T> AddLast(T value)
{
throw new NotImplementedException();
}
public SingleLinkedListNode<T> AddLast(SingleLinkedListNode<T> node)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(T value)
{
if (first == null)
{
return false;
}
SingleLinkedListNode<T> node = first;
if (first.Value.Equals(value))
{
return true;
}
while (true)
{
if (node.Next != null)
{
if (node.Value.Equals(value))
{
return true;
}
node = node.Next;
}
else
{
break;
}
}
return false;
}
public bool Delete(T value)
{
throw new NotImplementedException();
}
public bool Delete(LinkedListNode<T> node)
{
throw new NotImplementedException();
}
public bool DeleteAt(int index)
{
throw new NotImplementedException();
}
public bool DeleteFirst()
{
throw new NotImplementedException();
}
public bool DeleteLast()
{
throw new NotImplementedException();
}
public SingleLinkedListNode<T> Find(T value)
{
throw new NotImplementedException();
}
public SingleLinkedListNode<T> FindPrevious(T value)
{
throw new NotImplementedException();
}
public int IndexOf(int value)
{
throw new NotImplementedException();
}
public SingleLinkedListNode<T> Insert(SingleLinkedListNode<T> node)
{
throw new NotImplementedException();
}
}
2.树
3.图