Skip to content

Instantly share code, notes, and snippets.

@ramsunvtech
Last active July 14, 2019 06:48
Show Gist options
  • Save ramsunvtech/7270ababa71e65bf8d8a8e4ae48f0b7b to your computer and use it in GitHub Desktop.
Save ramsunvtech/7270ababa71e65bf8d8a8e4ae48f0b7b to your computer and use it in GitHub Desktop.
Tree Traversing
"use strict";
const tree = {
left: null,
right: null,
parent: null,
value: '0',
};
const node1 = {
left: null,
right: null,
parent: tree,
value: '1',
};
tree.left = node1;
const node2 = {
left: null,
right: null,
parent: tree,
value: '2',
};
tree.right = node2;
const node3 = {
left: null,
right: null,
parent: node1,
value: '3',
};
node1.left = node3;
const node4 = {
left: null,
right: null,
parent: node1,
value: '4',
};
node1.right = node4;
const node5 = {
left: null,
right: null,
parent: node2,
value: '5',
};
node2.left = node5;
const node6 = {
left: null,
right: null,
parent: node2,
value: '6',
};
node2.right = node6;
const node7 = {
left: null,
right: null,
parent: node3,
value: '7',
};
node3.left = node7;
const node8 = {
left:null,
right: null,
parent: node3,
value: '8',
};
node3.right = node7;
const node9 = {
left: null,
right: null,
parent: node4,
value: '9',
};
node4.left = node9;
const node10 = {
left: null,
right: null,
parent: node4,
value: '10',
};
node4.right = node10;
const node11 = {
left:null,
right: null,
parent: node5,
value: '11',
};
node5.left = node11;
const node12 = {
left:null,
right: null,
parent: node5,
value: '12',
};
node5.right = node12;
const node13 = {
left:null,
right: null,
parent: node6,
value: '13',
};
node6.left = node13;
const node14 = {
left: null,
right: null,
parent: node6,
value: '14',
};
node6.right = node14;
function findNextRightSiblingNode(node) {
if (node === node.parent.right) {
console.log('if')
return findNextRightSiblingNode(node.parent).left;
} else if (node === node.parent.left) {
console.log('else if')
return node.parent.right;
}
}
findNextRightSiblingNode(node10);
// Below Output
/*
{ left: null,
right: null,
parent:
{ left: [Circular],
right: { left: null, right: null, parent: [Circular], value: '12' },
parent:
{ left: [Circular],
right: [Object],
parent: [Object],
value: '2' },
value: '5' },
value: '11' }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment