cse 3358 note set 12 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CSE 3358 Note Set 12 PowerPoint Presentation
Download Presentation
CSE 3358 Note Set 12

Loading in 2 Seconds...

play fullscreen
1 / 13

CSE 3358 Note Set 12 - PowerPoint PPT Presentation


  • 67 Views
  • Uploaded on

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;

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

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


An Image/Link below is provided (as is) to download presentation

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

CSE 3358 Note Set 12

Data Structures and Algorithms

slide2

Exam 1

Next Wednesday 9/24/08

deletion by copying
Deletion by Copying

13

25

20

31

23

39

21

24

slide4

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
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 cases1
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
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
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
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

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

4

1

binary search balance
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
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