Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree_Inorder_Traversal in TypeScript #2945

Merged
merged 1 commit into from
May 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Tree_Inorder_Traversal/Tree_Inorder_Traversal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export {}
// Node
interface node
{
key: number;
parent?: node;
left?: node;
right?: node;
}


// Defining the nodes with their values and left right children
const node9: node = { key: 9, left: undefined, right: undefined };
const node8: node = { key: 8, left: undefined, right: undefined };
const node7: node = { key: 7, left: undefined, right: undefined };
const node6: node = { key: 6, left: undefined, right: undefined };
const node5: node = { key: 5, left: undefined, right: undefined };
const node4: node = { key: 4, left: node8, right: node9 };
const node3: node = { key: 3, left: node6, right: node7 };
const node2: node = { key: 2, left: node4, right: node5 };
const root: node = { key: 1, parent: undefined, left: node2, right: node3 };

// Construct a tree by assigning parents
function constructTree(): node
{
root.parent = undefined;
node2.parent = root;
node3.parent = root;
node4.parent = node2;
node5.parent = node2;
node6.parent = node3;
node7.parent = node3;
node8.parent = node4;
node9.parent = node4;

return root;
}

function inOrderTraversal(node: node) : null
{
if (!node)
return;
inOrderTraversal(node.left);
console.log(node.key + " -> ");
inOrderTraversal(node.right);
}


constructTree();
console.log("The inorder traversal of the binary tree is :");
inOrderTraversal(root);

/*
Output
The inorder traversal of the binary tree is :
8 -> 4 -> 9 -> 2 -> 5 -> 1 -> 6 -> 3 -> 7
*/