-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add Tree Postorder Traversal in TypeScript #3008
Merged
asha15
merged 1 commit into
jainaman224:master
from
shivamgupta57121:Tree_Postorder_Traversal
May 31, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//Tree Postorder Traversal in TypeScript | ||
|
||
export {} | ||
|
||
// Tree Nodes | ||
interface treeNodes | ||
{ | ||
val: number; | ||
parent?: treeNodes; | ||
left?: treeNodes; | ||
right?: treeNodes; | ||
} | ||
|
||
// Defining the nodes with their values and left right children. If a value is not given it is undefined. | ||
const node11: treeNodes = { val: 11 }; | ||
const node10: treeNodes = { val: 10 }; | ||
const node9: treeNodes = { val: 9 }; | ||
const node8: treeNodes = { val: 8 }; | ||
const node7: treeNodes = { val: 7 }; | ||
const node6: treeNodes = { val: 6, left: node10, right: node11}; | ||
const node5: treeNodes = { val: 5 }; | ||
const node4: treeNodes = { val: 4, left: node8, right: node9 }; | ||
const node3: treeNodes = { val: 3, left: node6, right: node7 }; | ||
const node2: treeNodes = { val: 2, left: node4, right: node5 }; | ||
const root: treeNodes = { val: 1, left: node2, right: node3 }; | ||
|
||
// Construct a tree by assigning parent | ||
function constructBinaryTree(): treeNodes | ||
{ | ||
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; | ||
node10.parent = node6; | ||
node11.parent = node6; | ||
|
||
return root; | ||
} | ||
|
||
// Recursive Function to call itself on left child, then on right child and then print value of current node | ||
function postOrderTraversal(currentNode: treeNodes) | ||
{ | ||
if (!currentNode) | ||
return; | ||
postOrderTraversal(currentNode.left); | ||
postOrderTraversal(currentNode.right); | ||
console.log(currentNode.val + " -> "); | ||
} | ||
|
||
constructBinaryTree(); | ||
console.log("The postorder traversal of the binary tree is :"); | ||
postOrderTraversal(root); | ||
|
||
/* | ||
Output: | ||
The postorder traversal of the binary tree is : | ||
8 -> | ||
9 -> | ||
4 -> | ||
5 -> | ||
2 -> | ||
10 -> | ||
11 -> | ||
6 -> | ||
7 -> | ||
3 -> | ||
1 -> | ||
*/ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove the extra line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed