您好,欢迎访问代理记账网站
移动应用 微信公众号 联系我们

咨询热线 -

电话 15988168888

联系客服
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

[Dive in Data Structure] TreeMap and TreeSet

What is TreeMap?

TreeMap extends AbstractMap and implement NavigableMap
在这里插入图片描述
treeMap is a k-v pair set with order based on red-black tree’s navigableMap to implement. we can custom of sort principle or just use the default with based on the key.

Red-Black Tree Review

The Defination of Red-black tree:(keep in mind, black nodes are way more than red)

  1. node is either red or black
  2. root is black
  3. every leaf is black
  4. every red node’s child node must be all black(means for every path, no two adjacent red node)
  5. every path has the exactly same number of black nodes.

In summary, red black tree is a balanced binary search tree with less balance than AVL tree(because in order to maintain a perfect balance tree like AVL, we have to cost lot when add and remove)

Difference between TreeMap and HashMap:

  1. the key of TreeMap can’t be null whereas hashmap can.
  2. treemap is not sync-ed
  3. treemap is slower than hashmap in search(), insert(), delete()

Constructor of TreeMap:

  1. TreeMap()
  2. TreeMap(Comparator<? super K> comparator)
  3. TreeMap(Map<? extends K,? extends V> m)
  4. TreeMap(SortedMap<K,? extends V> m)

Functions of TreeMap

the function in Hashmap is the same as Treemap, feel free to use them.

Why didn’t wee talk about TreeSet?

The relationship between Treeset and hashset is exactly the same with TreeMap and hashmap.


分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进