Understanding the Need to Sort and Search
Key: 0 Value: Magenta
Key: 1 Value: Green
Key: 2 Value: Red
Key: 3 Value: White
Key: 4 Value: Yellow
Key: 5 Value: Orange
Key: 6 Value: Teal
Key: 7 Value: Purple
Item 3 contains: White
The maximum item is: (7, 'Purple')
To create a binary tree, you must supply key and value pairs. One way to perform
this task is to create a dictionary as shown. After you create the tree, you can use
the
update
function to add new entries. The entries must include a key and value
pair as shown.
This example uses a function to perform a task with the data in
tree
. In this case,
the function merely prints the key and value pairs, but you could use the tree as
input to an algorithm for analysis (among other tasks). The function,
display
KeyValue
, acts as input to the
foreach
function, which displays the key and value
pairs as output. You also have access to myriad other features, such as using
get
to obtain a single item or
max_item
to obtain the maximum item stored in
tree
.
Do'stlaringiz bilan baham: |