TreeSet and TreeMap

Source: Internet
Author: User

TreeSet and TreeMap
Relationship between TreeSet and TreeMap:

   

1. TreeSet actually uses TreeMap to organize data, because an instance variable of the NavigableMap <e, Object> interface is saved in the TreeSet, and the Implementation class of this interface is TreeMap.

Public V put (K key, V value) {Entry <K, V> t = root; // determine whether a root node exists in a binary tree. if yes, create a root node. if (t = null) {root = new Entry <K, V> (key, value, null ); // create the root node size = 1; // set the number of elements in the set to 1 modCount ++; return null;} int cmp; Entry <K, V> parent; // declare the parent node Comparator <? Super K> cpr = comparator; // create the comparator if (cpr! = Null) {// The set has a custom comparator do {parent = t; // set the parent node to t (the first time t is the root node) cmp = cpr. compare (key, t. key); // compare the key of the root node with the key in the parameter if (cmp <0) // key <t. key t = t. left; else if (cmp> 0) // key> t. key t = t. right; else return t. setValue (value); // key = t. key} while (t! = Null);} else {// This set does not have a custom comparator if (key = null) throw new NullPointerException (); Comparable <? Super K> k = (Comparable <? Super K>) key; // The parameter uses the type of comparator do {parent = t; // sets the parent node to t (the first time t is the root node) cmp = k. compareTo (t. key); // compare the key of the root node with the key in the parameter if (cmp <0) // key <t. key t = t. left; else if (cmp> 0) // key> t. key t = t. right; else return t. setValue (value); // key = t. key} while (t! = Null);} Entry <K, V> e = new Entry <K, V> (key, value, parent ); // encapsulate the passed parameters as the set element if (cmp <0) // e <parent. left = e; else // e> parent. right = e; fixAfterInsertion (e); size ++; modCount ++; return null ;}

The following figure shows a more intuitive display.

First assignment:

Store the value as the root node

Final Entry <K, V> getEntry (Object key) {if (comparator! = Null) // The set has a custom comparator return getEntryUsingComparator (key); if (key = null) throw new NullPointerException (); Comparable <? Super K> k = (Comparable <? Super K>) key; Entry <K, V> p = root; // get the root node while (p! = Null) {// if the root node is not empty, the traversal node int cmp = k. compareTo (p. key); if (cmp <0) p = p. left; else if (cmp> 0) p = p. right; else return p; if k and the key of the node compare return value = 0 (return 0 indicates it is equal) then return this node} return null ;}GetEntryUsingComparator (key) source code:

// Use a custom Comparator to compare and traverse final Entry <K, V> getEntryUsingComparator (Object key) {K k = (K) key; Comparator <? Super K> cpr = comparator; if (cpr! = Null) {Entry <K, V> p = root; while (p! = Null) {int cmp = cpr. compare (k, p. key); if (cmp <0) p = p. left; else if (cmp> 0) p = p. right; else return p;} return null ;}

 

Sorting of Treeset and TreeMap

Based on the hierarchical relationship of its binary tree, the system looks for its parent node from the leaf-level node on the far left. It searches for the right child node of its parent node. If there is a child node under the right child node of its parent node traverse the right node until the leftmost leaf-level node under the right node is found. After the current parent node traversal is completed, traverse the parent node of the parent node in the same way and so on until the binary tree traversal is completed.

:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.