lecture 9: binary tree basics
DESCRIPTION
This slide is part of course designed for placement preparation for students of IIT Guwahati.TRANSCRIPT
![Page 1: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/1.jpg)
Binary Tree Basics
![Page 2: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/2.jpg)
Binary Tree Definition
• Every node has at most two children a left child and a right child.
• Root - the top most node in a tree.
![Page 3: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/3.jpg)
![Page 4: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/4.jpg)
A Basic Tree Structure
![Page 5: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/5.jpg)
Leaf
• A node having no child is a leaf.
![Page 6: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/6.jpg)
Depth of a node
• The length of the path from the root to the node.
• Depth(root)=0
![Page 7: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/7.jpg)
Height of the tree
• The height of a node is the length of the longest downward path between the node and a leaf + 1.
• The height of a node is the length of the longest downward path between the root and a leaf +1.
![Page 8: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/8.jpg)
Depth and height
![Page 9: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/9.jpg)
Full Binary Tree
• Every node has 2 children except the leaves.
![Page 10: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/10.jpg)
Complete Binary Tree
• A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
![Page 11: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/11.jpg)
• A fully complete binary tree has n nodes what is the height of the tree?
O(log n)
![Page 12: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/12.jpg)
• A fully complete binary tree has n nodes what are the number of leaves in the tree?
Ceil(n/2)
![Page 13: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/13.jpg)
Implementation
![Page 14: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/14.jpg)
Search an element in binary tree
![Page 15: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/15.jpg)
Sum of all the nodes of the tree
![Page 16: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/16.jpg)
Inorder traversal
• Visit the left subtree first• Visit the node.• Visit the right subtree.
![Page 17: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/17.jpg)
Code
![Page 18: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/18.jpg)
Postorder traversal
• Visit the left subtree first• Visit the right subtree• Visit the node.
![Page 19: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/19.jpg)
Preorder traversal
• Visit the node.• Visit the left subtree first• Visit the right subtree.
![Page 20: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/20.jpg)
Vector
vector<int> a;a.push_back(value);cout<<a[i]<<endl;Accessing the pushed back value similar to an
array.(REST READ FROM NET)
![Page 21: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/21.jpg)
Oj.leetcode.com
https://oj.leetcode.com/problems/binary-tree-postorder-traversal/https://oj.leetcode.com/problems/binary-tree-preorder-traversal/https://oj.leetcode.com/problems/balanced-binary-tree/https://oj.leetcode.com/problems/balanced-binary-tree/
![Page 22: Lecture 9: Binary tree basics](https://reader035.vdocuments.net/reader035/viewer/2022062514/559137901a28ab07498b45fd/html5/thumbnails/22.jpg)
• https://oj.leetcode.com/problems/binary-tree-level-order-traversal/
• https://oj.leetcode.com/problems/binary-tree-level-order-traversal/
• https://oj.leetcode.com/problems/binary-tree-inorder-traversal/
• https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
• https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/