anatomy of a bug anatomy of a bug
anatomy of a bug
anatomy of a bug
anatomy of a bug
Anatomy of a bug AVL Trees
I wrote java code for the avl tree: the data structure, a test/demo program and managed to display it … I felt … smug
smug ug sm smug g smu 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. 2. 3. you have code for btree the only difference between btree and avl tree is restoring balance disable “restore balance” to confirm that’s were bug lies
Phew! No duplicates & size is right. Okay, bug is in restore. Balance
Track it down to one of the rotations
Edit out double. Rotate
Compile and test …
YES! The bug’s in double. Rotate!
smug ug sm smug g smu It was written all over me
Let’s get more information out of double. Rotate and fix this baby!
1. Turn double. Rotate back on 2. Print out information on before & after double. Rotate
Turn double. Rotate back on Print out the rotation made
Print out information on nodes k 1, k 2, k 3 before & after double. Rotate Before & after
Compile & run …
arrrrrrrgh!
Too much information! Keep it simple! Get smallest data set possible to demonstrate bug!
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
Is it just a “right thing”?
Arrrrrrgh! It happens on the left too!
Ma ke it go aw ay! !!
A brief intermission is now called for as our hero calms down Hero re-writes code for rotate … Hero wonders if there is something to do with call by reference, call by value … Hero wonders if there is something flawed in java 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?
t twa tw at tw a t twat t twa 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 …
at smug tw u sm t a smug tw at w gt
What lessons have we learned?
Lessons we can learn 1. 2. 3. 4. 5. if your program works … you have a subtle bug (worry) if data structure is complicated write code to test it (I didn’t) produce minimal test cases produce BIG test cases 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!!
This took 1 day of my life
- Slides: 71