Anatomy of a bug. AVL Trees Anatomy of a bug

Preview:

Citation preview

anatomy of a bug

anatomy of a bug

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

smug

smugsmug

smug

smug

It was written all over me

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

Dr. Chris “unworthy” Unsworth

… and this is what happened

Calm down … think

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

Phew!

No duplicates & size is right.

Okay, bug is in restoreBalance

Track it down to one of the rotations

Edit out doubleRotate

Compile and test …

YES!

The bug’s in doubleRotate!

smug

smugsmug

smug

smug

It was written all over me

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

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

Turn doubleRotate back onPrint out the rotation made

Before & after

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

Compile & run …

arrrrrrrgh!

Too much information!

Keep it simple!

Get smallest data set possible to demonstrate bug!

ll pp mm

ll

pp

mm

RLRotate

Heck! It looks okay.

What’s up?

Maybe you should show the whole

tree dude.

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

Is it just a “right thing”?

Arrrrrrgh!It happens on the left too!

Make it go away!!!

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

Hero has lunch at 12.30

Hero returns from lunch and tries again …

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

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

twat

twat

twat

twattwat

twat

It was written all over him …

It does this loop an extra time!

Compile and run …

YES!

Now do it on the left …

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

Edit out prints …

Go for it fat boy …

smug twat

smug twatsmug twat

smug twat

smug tw

at

What lessons have we learned?

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

This took 1 day of my life

Recommended