coderz.py

Keep Coding Keep Cheering!

Types of Views & Binary Trees

Trie in DSA

In binary trees, the views and types refer to different perspectives and classifications of the tree structure.

Types of Views in Binary Tree are :
                     1
                   /   \
                 2       3
                / \     / \
               4   5   6   7
              / \   \     /
             8   9   10  11
  • Horizontal View: The horizontal view of a binary tree shows the nodes from left to right at each level. Nodes at the same level are listed in the order of their appearance, from left to right.
1 2 3 4 5 6 7 8 9 10 11
  • Vertical View: The vertical view of a binary tree shows the nodes from top to bottom, where nodes in the same vertical line are displayed in the order of their occurrence from top to bottom.
8
4
2 9 10
1 5 6 11
3 7
  • Left View: The left view of a binary tree shows the leftmost node at each level. It represents the visible nodes when looking at the tree from the left side.
1 2 4 8
  • Right View: The right view of a binary tree shows the rightmost node at each level. It represents the visible nodes when looking at the tree from the right side.
1 3 7 11
  • Top View: The top view of a binary tree displays the nodes visible when viewing the tree from the top. It shows the nodes on the vertical lines that intersect the topmost node at each horizontal distance.
8 4 2 1 3 7 11
  • Bottom View: The bottom view of a binary tree displays the nodes visible when viewing the tree from the bottom. It shows the nodes on the vertical lines that intersect the bottom-most node at each horizontal distance.
8 9 10 5 6 11 7
Time & Space complexity for each type of view in a binary tree:
View TypeTime ComplexitySpace Complexity
Horizontal ViewO(n)O(n)
Vertical ViewO(n log n)O(n)
Left ViewO(n)O(n)
Right ViewO(n)O(n)
Top ViewO(n log n)O(n)
Bottom ViewO(n)O(n)
Types of Binary Trees:
  • Full Binary Tree: A binary tree in which every node has either 0 or 2 children.
        1
       / \
      2   3
     / \
    4   5
  • Complete Binary Tree: A binary tree in which all levels, except possibly the last one, are completely filled, and all nodes are as left as possible.
        1
       / \
      2   3
     / \  /
    4   5 6
  • Perfect Binary Tree: A binary tree in which all internal nodes have two children, and all leaf nodes are at the same level.
        1
       / \
      2   3
     / \ / \
    4  5 6  7
  • Balanced Binary Tree: A binary tree in which the heights of the left and right subtrees of every node differ by at most one.
        1
       / \
      2   3
     /     \
    4       5
  • Binary Search Tree (BST): A binary tree in which for every node, all values in its left subtree are less than its value, and all values in its right subtree are greater than its value.
        4
       / \
      2   5
     / \   \
    1   3   6
  • Skewed Binary Tree: is a special type of binary tree in which all the nodes are either in the left subtree or the right subtree.
         1
        /
       2
      /
     3
    /
   4

OR

   1
    \
     2
      \
       3
        \
         4
Binary Tree TypeTime ComplexitySpace Complexity
Full Binary TreeO(n)O(n)
Complete Binary TreeO(n)O(n)
Perfect Binary TreeO(log n)O(log n)
Balanced Binary TreeO(log n)O(log n)
Binary Search Tree (BST)Average Case: O(log n)O(n)

Note: also read about Binary Trees: Structure & Tree travels

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement