CSE 3358 Note Set 12

1 / 13

# CSE 3358 Note Set 12 - PowerPoint PPT Presentation

CSE 3358 Note Set 12. Data Structures and Algorithms. Exam 1 Next Wednesday 9/24/08. Deletion by Copying. 13. 25. 20. 31. 23. 39. 21. 24. template&lt;class T&gt; void BST&lt;T&gt;:: deleteByCopying ( BSTNode &lt;T&gt;*&amp; node) { BSTNode &lt;T&gt; *previous, * tmp = node;

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'CSE 3358 Note Set 12' - salali

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### CSE 3358 Note Set 12

Data Structures and Algorithms

Exam 1

Next Wednesday 9/24/08

Deletion by Copying

13

25

20

31

23

39

21

24

template<class T>

void BST<T>::deleteByCopying(BSTNode<T>*& node) {

BSTNode<T> *previous, *tmp = node;

if (node->right == 0)

node = node->left;

else if (node->left == 0)

node = node->right;

else {

tmp = node->left;

previous = node;

while (tmp->right != 0) {

previous = tmp;

tmp = tmp->right;

}

node->key = tmp->key;

if (previous == node)

previous->left = tmp->left;

else previous->right = tmp->left;

}

delete tmp;

}

Some Different Cases

//// see prev slides

else {

tmp = node->left;

previous = node;

while (tmp->right != 0) {

previous = tmp;

tmp = tmp->right;

}

node->key = tmp->key;

if (previous == node)

previous->left = tmp->left;

else previous->right = tmp->left;

}

13

Delete 25

25

20

31

16

Some Different Cases

//// see prev slides

else {

tmp = node->left;

previous = node;

while (tmp->right != 0) {

previous = tmp;

tmp = tmp->right;

}

node->key = tmp->key;

if (previous == node)

previous->left = tmp->left;

else previous->right = tmp->left;

}

13

Delete 25

25

20

31

23

22

Balancing Act
• Perfectly Balanced Tree: All leaves are to be found on one level or two levels
• For perfectly balanced tree, height = ceil(lg(n))
• Example: 10000 nodes height = ceil(lg(10000)) = ceil(13.289) = 14
• This means, for a perfectly balanced tree, at most 14 nodes have to be checked to locate a particular element
How to Balance
• Depends on when tree is formed in relation to when searches are performed
• If all data is present before tree is formed,
• Store in array
• Sort using efficient sorting algo
• Used binary search type algo to create tree
Example
• Initial Data Set:
• 5 1 9 8 7 0 2 3 4 6
• Sorted:
• 0 1 2 3 4 5 6 7 8 9

4

Middle element

becomes root

Then recursively process

right subtree then left subtree

Example
• Sorted:
• 0 1 2 3 4 5 6 7 8 9

4

1

Binary Search Balance

template <class T>

void BST<T>::balance (T data[], int first, int last)

{

if(first <= last)

{

int middle = (first + last)/2;

insert(data[middle]);

balance(data, first, middle – 1);

balance(data, middle + 1, last);

}

}

Discussion
• All data must be present to create the tree
• Subsequent inserts can lead to unbalancing tree
• Best case sorting of array is n*lg(n)
• Can produce array/list from tree w/ inorder traversal
• Space needed?
• Then recreate the tree w/ balance() method