HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Can one insert non-unique elements into an AVL tree?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
uniquecaninsertelementsnonintooneavltree

Problem

Is it possible to insert a sequence of non-unique elements into an AVL tree?

For example, what is the AVL tree result of inserting 3, 3, and 3 into an initially empty AVL tree?

Is it:

3
      /   \
     3     3

Solution

A BST (from which the AVL descends) with duplicate keys can have its rotation make nodes with the same key be on both sides of the parent node, in which case, some ambiguity might result. However, you could make your life a whole lot easier and have each node contain a key and the number of times that key had been inserted into the tree, to therefore keep track of successive inserts of successive keys and disambiguate its structure.

You would still insert a node (first by searching for the expected placement for the node) with respect to its key, initialize its count to 0, and towards the end of completing the insert increment that count).

Context

StackExchange Computer Science Q#39954, answer score: 4

Revisions (0)

No revisions yet.