Binary Tree - Traverse, Search, Insertion,(Linked List Implementation)
Traverse Binary Tree
Traversing all nodes of Binary Tree(Linked List implementation)
Depth first search
- PreOrder Traversal
- InOrder Traversal
- PostOrder Traversal
Breadth first search
- LevelOrder Traversal
* PreOrder Traversal(Using Stack)
- Root
- Left Subtree
- Right Subtree
1 | preorderTraverse(root){ ----------------T(n) |
- Time Complexity - O(n)
- Space Complexity - O(n) : Recursive call로 많은 노드가 스택에 push, pull 되기 때문에
* In-Order Traversal(Using Stack)
- Left Subtree
- Root
- Right Subtree
1 | inorderTraverse(root){ ----------------T(n) |
- Time Complexity - O(n)
- Space Complexity - O(n)
* Post-Order Traversal(Using Stack)
- Left Subtree
- Right Subtree
- Root
1 | postorderTraverse(root){ ----------------T(n) |
- Time Complexity - O(n)
- Space Complexity - O(n)
* Level-Order Traversal(Using Queue)
1 | levelOrderTraversal(root){ |
* Time Complexity - O(n) * Space Complexity - O(n)
* Searching a node(Using Level-order Traversal)
1 | searchForGivenValue(value){ |
* Time Complexity - O(n) * Space Complexity - O(n)
* Insertion of node(Using Level-order Traversal)
- Insertion
- When the root is blank
- Insert at first vacant child
1 | insertNodeInBinaryTree(){ |
* Time Complexity - O(n) * Space Complexity - O(n)
* Deletion of node(Using Level-order Traversal)
- Insertion
- When the value to be deleted is not existing in the tree
- When the value to be deleted exists in the tree
1 | deleteNodeFromBinaryTree() |
* Time Complexity - O(n) * Space Complexity - O(n)