SimpleMinded Insertions into a Binary Search Tree GM
Simple-Minded Insertions into a Binary Search Tree G<M B<M G<J B<J Insertion order B<E B<C E G>E M J T L G>F P W F insert(M) V C K insert(T) B>A insert(J) G A D insert(P) insert(L) B insert(E) • That’s it; simple minded insertion is pretty simple; the fact that it • The tree as you see it now, above, insert(W)preserves the binary search tree property is nice (vital, in fact) but the fact was build by performing the insertions insert(Q) • A “simple-minded” insertion is based only onispreserving the that it doesn’t preserve any particular shape constraint going to be aa insert(C) shown on the left (M wasregard first, then T, binary search tree property, without to shape AVL trees fix insert(F) problem that we’ll need then J, and sotoon) insert(K) • The new node becomes the correct child of some existing node so as • Worst case here, the insertion is O(h), where h is the height of the tree • Now lets see an animation of the next insert(A) to preserve the binary search tree property and such that the pointer insert(D) couple of insertions • In general (after just simple minded and in this case from the new parent is the only insertions), link adjusted to certainly do the insertion insert(G)here, the tree will not be almost perfect, so we have no idea what the Oinsert(G) insert(B) notation would be in terms of n, the number of nodes; worst case would be O(n), if the tree has only one node at each level
- Slides: 1