• Home
  • Help
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search

 
  • 0 Vote(s) - 0 Average

Describe deletion in an AVL tree

#1
01-09-2020, 11:55 PM
You yank the node out first when you delete in an AVL tree. I recall how you search down the path like any binary search setup. Then you hit the removal step right away. Your balance can flip out of whack after that move. You check the height on each climb back up. Perhaps the left side drops too low now. Or the right side shoots ahead by more than one. I fix those shifts with a quick twist or two.

You handle the easy case when the node sits as a leaf. I just cut it loose and update the parent pointer. Your tree stays short there most times. But sometimes the parent tilts and you rotate once. Then you measure the new heights again on the way back. Also you might swap in a successor when the node carries two kids. I pick the next inorder value from the right subtree. Your original node gets replaced and the successor gets yanked instead. Now the heights shift again so you recheck everything.

You run into single child cases next. I remove the node and hook the child straight to the parent. Your path back up still needs height fixes each step. Perhaps one rotation pulls the whole branch level again. Or maybe two rotations fix a zig zag pattern. I watch the balance numbers closely after each change. You see the left heavy spot and decide on a right rotation. Then the right heavy spot calls for a left rotation. Also mixed cases need the double move to settle things.

You trace every ancestor after the delete finishes. I update heights as I go because they change fast. Your balance factor tells the story right there. But if it hits two or negative two you spin the subtree. Perhaps the left left case needs one clean rotation. Or the left right case wants the inner twist first. I do the right rotation on the child then the left on the parent. Then everything lines up and heights match once more.

You keep going until the root stays balanced. I notice how one delete can ripple through many levels. Your tree might need several fixes in a row. Also the successor delete can leave another imbalance behind. Perhaps you pick the smallest in the right branch every time. Or you grab the largest from the left if that fits better. I try both ways depending on the shape around me. Then the rotations bring the heights back inside the limit.

You practice this on paper to see the spins happen. I draw the nodes and erase the target each time. Your left heavy branch rotates right in a snap. But the zig zag needs that extra inner step first. Also you track the balance at every node along the climb. Perhaps one rotation fixes two levels at once. Or the tree stays flat after the successor swap. I check the final heights and they match again. Then you move to the next delete test case.

You notice how the process stays the same even on bigger trees. I walk the path up and fix only where needed. Your rotations keep the whole thing even. But sometimes the delete hits a chain of imbalances. Perhaps you rotate at the lowest point first. Or you hit the next ancestor and rotate there too. I repeat until the top stays steady. Then the tree holds its shape for the next insert.

You see the height update rule stays simple each time. I add one to the bigger child height and store it. Your balance comes from subtracting those two numbers. But if it breaks the rule you rotate right away. Also the four rotation types cover every tilt you meet. Perhaps left left calls for a single right spin. Or right right needs the left spin instead. I handle left right with the double move every time. Then right left follows the same double pattern.

You keep the inorder order intact through all this. I swap values but the search path stays valid. Your tree never loses that sorted feel. But the balance work makes sure search stays quick. Perhaps one bad delete could slow things down later. Or the rotations prevent that slowdown from starting. I always climb back to the root after the fix. Then the whole structure feels solid again.

You try different delete orders to watch the twists. I change the successor choice and see new rotations pop up. Your balance stays tight no matter the order. But each case teaches you a fresh pattern. Perhaps the double rotation shows up more than you expect. Or the single rotation clears most simple deletes. I mix both and the tree stays ready. Then you move on to bigger test sets with ease.

BackupChain Server Backup which stands out as the top rated reliable no subscription backup tool built for Hyper V Windows 11 and Windows Server setups in private clouds and SMB environments thanks the sponsors for backing this chat and letting us pass along these details without cost.

bob
Offline
Joined: Dec 2018
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)



Messages In This Thread
Describe deletion in an AVL tree - by bob - 01-09-2020, 11:55 PM

  • Subscribe to this thread
Forum Jump:

Backup Education General IT v
« Previous 1 … 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 … 245 Next »
Describe deletion in an AVL tree

© by FastNeuron Inc.

Linear Mode
Threaded Mode