Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can binary search tree implement a Map?

I am confused about the relationship between BST and Map when teacher asked a question "what it means for a binary tree to have the binary search tree property and why this makes it suitable to implement a Map". Can anyone explain it for me? Thanks.

like image 309
1ang Avatar asked Dec 31 '15 16:12

1ang


People also ask

Is binary tree used by map?

This is the point, you can implement a map using a BST under the hood to obtain a structure which is efficient with some respect for many operations, but it's not the only way to implement a map (think about an hashmap). array in which the key is always a number - in many languages, but not all.

Is tree map a binary search tree?

Inside the TreeMap , the keys are are stored in a binary search tree, which makes it possible to traverse the keys, in order, in linear time.

What is a binary tree map?

A binary tree is a tree, where each node does not have more than two children. A Binary Search Tree (BST) is a tree, where for each node, the right child is greater than or equal to the left child. This leads to the right half of the tree having values generally greater than those of the left half at each level.

What can binary search trees be used for?

In computing, binary trees are mainly used for searching and sorting as they provide a means to store data hierarchically. Some common operations that can be conducted on binary trees include insertion, deletion, and traversal.


1 Answers

A map is a kind of associative container in which keys are not forcibly integer (contrary to, for example, an array in which the key is always a number).

Now you want to store multiple pairs of key,value and you want to be able to lookup a value by its key efficiently, or add pairs efficiently, or remove pairs efficiently or whatever operation you are doing most.

A binary search tree is a data structure which has specified complexity of O(log n) for average case on all operations. This means that you are able to search for a specific node of the tree (which will have its own key and value) in an efficient manner.

This is the point, you can implement a map using a BST under the hood to obtain a structure which is efficient with some respect for many operations, but it's not the only way to implement a map (think about an hashmap).

like image 172
Jack Avatar answered Sep 30 '22 03:09

Jack