Comments (3)
Thanks for commenting @beblueblue! You are right about losing the left node in the case of a LL rotation. However, the use case for rotations is to balance a tree:
E.g.
1 1
\ \
2* 3
\ --left-rotation(2)-> / \
3 2* 4
\
4
If you have a tree with a left node (as you mentioned) it means that is balanced and it doesn't need to have a rotation.
1*
\
2
/ \
3 4
Do you have a use case where it needed to do a LL rotation on an already balanced tree?
from dsa.js-data-structures-algorithms-javascript.
Thanks for commenting @beblueblue! You are right about losing the left node in the case of a LL rotation. However, the use case for rotations is to balance a tree:
E.g.
1 1 \ \ 2* 3 \ --left-rotation(2)-> / \ 3 2* 4 \ 4
If you have a tree with a left node (as you mentioned) it means that is balanced and it doesn't need to have a rotation.
1* \ 2 / \ 3 4
Do you have a use case where it needed to do a LL rotation on an already balanced tree?
Thank you for your prompt reply.
Now, Let's make a preset, we have a tree.
16* 16*
/ \ --- add two node(8, 2)--> / \
4 32 4 32
/ \
2 8
The tree is still balanced. The next step, we add the node 1.
16* 16*
/ \ / \
4 32 --- add the node (1) --> 4 32
/ \ / \
2 8 2 8
/
1
In this case that Node16.balanceFactor is 2, should we make the RR rotation for the root(16)?
If we have to do it, we use the function.
rightRotation(16)
function rightRotation(node) {
const newParent = node.left; // E.g., node 4
const grandparent = node.parent; // E.g., undefiend
swapParentChild(node, newParent, grandparent);
newParent.setRightAndUpdateParent(node);
// we will lose the node 8!
node.setLeftAndUpdateParent(null);
return newParent;
}
Now, our tree became the one below.
4 4
/ \ / \
2 16 --- the correct tree --> 2 16
/ \ / / \
1 32 1 8 32
rightRotation(16); -- to get right tree;
function rightRotation(node) {
const newParent = node.left; // E.g., node 4
const grandparent = node.parent; // E.g., undefiend
const previousRight = newParent.right || null;
swapParentChild(node, newParent, grandparent);
newParent.setRightAndUpdateParent(node);
// we should make the node-8 to be the left of the node-16
node.setLeftAndUpdateParent(previousRight);
return newParent;
}
Last and foremost, I learn a lot form your article. Thank you!
from dsa.js-data-structures-algorithms-javascript.
Thank you so much @beblueblue for taking the time and providing an example. I made the fixes and added some tests. Let me know if you find anything else.
from dsa.js-data-structures-algorithms-javascript.
Related Issues (20)
- broken links/routes on big-o-examples HOT 2
- Graph nodes store adjacents in HashSet object which does not exist HOT 3
- Difficulty figuring out how to use BFS on the graph structure HOT 6
- pull-issue HOT 1
- Missing word in algorithms analysis part one HOT 1
- Radix sort
- Minor code documentation fix HOT 1
- Linked List Code Typo But Not Sure 😄
- avl-tree.js: deleting root value deletes the tree by setting root to undefined
- removeFirst() function in linked-list.js HOT 1
- removeByPosition () function in linked-list.js HOT 1
- Humble disagreement on straightforward recursive fibonacci implementation being "divide and conquer" HOT 4
- Book in ePub format? HOT 8
- Lorem ipsum in book HOT 4
- A question about balance function in AVL HOT 3
- Typos in book content: makePizza parameter, array.pop() mutation, and BST node values HOT 2
- Better hash function ? HOT 2
- book/asciidoc/pdf: Fix link to appending HOT 1
- Should return the same node when we add a duplicated node HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dsa.js-data-structures-algorithms-javascript.