Unformatted text preview: 17
18
8 Insert_In_Order(),
Insert_In_Order(), BST
!
!
!
!
! Inserting creates a new leaf node
Its
Its parent is an existing leaf node, or a node
with
with only one child
With
With BST properties, we can find the location of
insert in O(log2n)
To
To link this new node to the rest of the tree,
only
only one link needs to be updated
Assuming
Assuming that memory allocation happens in
constant time, the entire Insert_In_Order()
algorithm is on average O(log2n)
9 Lists
Lists as a BT
! Lists,
Lists, while good dynamic data structures, have
some
some operations that are not quite efficient
– Finding the Kth element, Find_Kth()
– Inserting/deleting at location k, Add_Kth(),
Inserting/deleting
Remove_Kth()
Remove_Kth() !
! BT
BT make these operations efficient
BT structure:
– Leaf nodes are elements from the list
– Nonleaf nodes store the number of elements in the
Nonleaf
left
left child
– Use a heterogeneous tree (each node in the tree can
Use
store
store different types)
10 Example
Example
1
A 2
B 3
C 4
D 5
E # element 3
1
1 A Nonleaf nodes store the
leaf
number of elements in the
left
left child 1 B D E C
11 Implementation:
Implementation: F...
View
Full Document
 Spring '08
 Staff
 Binary Search, Data Structures

Click to edit the document details