71
anatomy of a bug

Anatomy of a bug. AVL Trees Anatomy of a bug

Embed Size (px)

Citation preview

Page 1: Anatomy of a bug. AVL Trees Anatomy of a bug

anatomy of a bug

Page 2: Anatomy of a bug. AVL Trees Anatomy of a bug

anatomy of a bug

Page 3: Anatomy of a bug. AVL Trees Anatomy of a bug

anatomy of a bug

Page 4: Anatomy of a bug. AVL Trees Anatomy of a bug

AVL Trees

Anatomy of a bug

Page 5: Anatomy of a bug. AVL Trees Anatomy of a bug

I wrote java code for the avl tree: the data structure, a test/demo programand managed to display it …

I felt … smug

Page 6: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 7: Anatomy of a bug. AVL Trees Anatomy of a bug

smug

smugsmug

smug

smug

It was written all over me

Page 8: Anatomy of a bug. AVL Trees Anatomy of a bug

… so smug that I just HAD to demo it to a friend/colleague …

Dr. Chris “unworthy” Unsworth

Page 10: Anatomy of a bug. AVL Trees Anatomy of a bug

… and this is what happened

Page 11: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 12: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 13: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 14: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 15: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 16: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 17: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 18: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 20: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 21: Anatomy of a bug. AVL Trees Anatomy of a bug

Calm down … think

Page 22: Anatomy of a bug. AVL Trees Anatomy of a bug

1. you have code for btree2. the only difference between btree and avl tree is restoring balance3. disable “restore balance” to confirm that’s were bug lies

Page 23: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 24: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 25: Anatomy of a bug. AVL Trees Anatomy of a bug

Phew!

No duplicates & size is right.

Okay, bug is in restoreBalance

Page 26: Anatomy of a bug. AVL Trees Anatomy of a bug

Track it down to one of the rotations

Page 27: Anatomy of a bug. AVL Trees Anatomy of a bug

Edit out doubleRotate

Page 28: Anatomy of a bug. AVL Trees Anatomy of a bug

Compile and test …

Page 29: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 30: Anatomy of a bug. AVL Trees Anatomy of a bug

YES!

The bug’s in doubleRotate!

Page 31: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 32: Anatomy of a bug. AVL Trees Anatomy of a bug

smug

smugsmug

smug

smug

It was written all over me

Page 33: Anatomy of a bug. AVL Trees Anatomy of a bug

Let’s get more information out of doubleRotate and fix this baby!

Page 34: Anatomy of a bug. AVL Trees Anatomy of a bug

1. Turn doubleRotate back on2. Print out information on before & after doubleRotate

Page 35: Anatomy of a bug. AVL Trees Anatomy of a bug

Turn doubleRotate back onPrint out the rotation made

Page 36: Anatomy of a bug. AVL Trees Anatomy of a bug

Before & after

Print out information on nodesk1, k2, k3 before & after doubleRotate

Page 37: Anatomy of a bug. AVL Trees Anatomy of a bug

Compile & run …

Page 38: Anatomy of a bug. AVL Trees Anatomy of a bug

arrrrrrrgh!

Page 39: Anatomy of a bug. AVL Trees Anatomy of a bug

Too much information!

Keep it simple!

Get smallest data set possible to demonstrate bug!

Page 40: Anatomy of a bug. AVL Trees Anatomy of a bug

ll pp mm

ll

pp

mm

RLRotate

Page 41: Anatomy of a bug. AVL Trees Anatomy of a bug

Heck! It looks okay.

What’s up?

Page 42: Anatomy of a bug. AVL Trees Anatomy of a bug

Maybe you should show the whole

tree dude.

Page 43: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 44: Anatomy of a bug. AVL Trees Anatomy of a bug

It’s back! It’s back! It’s back

Page 45: Anatomy of a bug. AVL Trees Anatomy of a bug

Is it just a “right thing”?

Page 46: Anatomy of a bug. AVL Trees Anatomy of a bug

Arrrrrrgh!It happens on the left too!

Page 47: Anatomy of a bug. AVL Trees Anatomy of a bug

Make it go away!!!

Page 48: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 49: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 50: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 51: Anatomy of a bug. AVL Trees Anatomy of a bug

A brief intermission is now called for as our hero calms down

Hero re-writes code for rotate …

Hero wonders if there is something flawed in java

Hero wonders if there is something to do with call by reference, call by value …

Our hero clutches at straws …

Hero even reads the book again!!!

Page 52: Anatomy of a bug. AVL Trees Anatomy of a bug

Hero has lunch at 12.30

Page 53: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 54: Anatomy of a bug. AVL Trees Anatomy of a bug

Hero returns from lunch and tries again …

Page 55: Anatomy of a bug. AVL Trees Anatomy of a bug

Holy smoke! Size is 4! I only entered 3 strings!!

How the heck can that be?Where is size updated?

Page 56: Anatomy of a bug. AVL Trees Anatomy of a bug

twat

twat

twat

twattwat

twat

It was written all over him …

Page 57: Anatomy of a bug. AVL Trees Anatomy of a bug

It does this loop an extra time!

Page 58: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 59: Anatomy of a bug. AVL Trees Anatomy of a bug

Compile and run …

Page 60: Anatomy of a bug. AVL Trees Anatomy of a bug

YES!

Page 61: Anatomy of a bug. AVL Trees Anatomy of a bug

Now do it on the left …

Page 62: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 63: Anatomy of a bug. AVL Trees Anatomy of a bug

Get rid of comments etc and try on large data set …

Page 64: Anatomy of a bug. AVL Trees Anatomy of a bug

Edit out prints …

Page 65: Anatomy of a bug. AVL Trees Anatomy of a bug

Go for it fat boy …

Page 66: Anatomy of a bug. AVL Trees Anatomy of a bug
Page 67: Anatomy of a bug. AVL Trees Anatomy of a bug

smug twat

smug twatsmug twat

smug twat

smug tw

at

Page 68: Anatomy of a bug. AVL Trees Anatomy of a bug

What lessons have we learned?

Page 69: Anatomy of a bug. AVL Trees Anatomy of a bug

1. if your program works … you have a subtle bug (worry)2. if data structure is complicated write code to test it (I didn’t)3. produce minimal test cases4. produce BIG test cases5. imagine if I didn’t have the btree code … would that be dangerous?

What would your data structure testing program do?• check that we have a tree

• no loops• check we have no orphans (nodes without parents)• no duplicates• height is okay• size is okay• avl balance is present• check EVERYTHING you can think of!!

Lessons we can learn

Page 70: Anatomy of a bug. AVL Trees Anatomy of a bug

This took 1 day of my life