Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




TREES


What is the first thing that comes to your mind when you hear the term 'Tree'? Well ! Off Course, the 'Trees' all around us.


But in case of Algorithms, the concept of 'Tree' is sort of a family tree. i.e. A family tree can have a Parent, child, ancestor and so on. Kind of same concepts can be seen here.


java_Collections

Let us understand some of the terms that are used in Tree :


  1. Root

    The top element in above tree is called the 'root'.

    i.e. 'A' is the 'root' element of this 'Tree'.
  2. Parent

    Let us consider the relation between 'B', 'D' and 'E' from the above 'Tree'.

    Well ! 'B' is the 'parent' of 'D' and 'E'. Why ?

    Because, 'D' and 'E' are derived from 'B'.
  3. Children/Child

    Similarly, 'D' and 'E' are the children of the 'B'.
  4. Sibling

    Again, 'D' and 'E' are 'siblings' as they are derived from the same parent 'B'.

    Similarly, 'B' and 'C' are 'siblings' as they are derived from the same parent 'A'.

    But 'D' and 'C' are not siblings as they have different parents.
  5. Ancestor

    Now, if you consider 'D'. The 'ansestor' of 'D' is 'B' and 'A'. Because 'D' is derived from 'B' and 'B' is derived from 'A'.

    Similarly,'C' and 'A' are 'ansestor' of 'G'. Because 'G' is derived from 'C' and 'C' is derived from 'A'.
  6. Descendant

    Again, if you consider 'D'. 'D' is the 'descendant' of 'A'. Also 'D' is a 'descendant' of 'A'.

    Because 'D' is connected with 'B' directly and since, 'B' is derived from 'A'. So, indirectly 'D' is connected to 'A' and can be called as a 'descendant' of 'A'.
  7. Leaf/Leaves

    'D', 'E', 'F', 'G', 'H' and 'I' are the leaves of the tree as they dont have any child.

    But 'H' is not a 'leaf' because 'H' has a child 'I'.
  8. Node

    A simple and straight definition would be, all the elements i.e A, B, C, D, E, F, G, H and 'I' are the 'Nodes' of the tree.
  9. Edge

    As you can see in the above tree, all the 'Nodes' are connected to each other by lines.These lines that connects 'A' and 'B' or 'C' and 'F' are called 'Edges'.
  10. Height of a Tree

    If you look at the tree. You can find that there are 4 levels in the tree.

    java_Collections


    Now, the number of levels is the height of the tree. And since the tree has 4 levels. So, the height of the tree is 4.
  11. Height of a Node

    To find the 'Height of a Node', let us take the same tree.

    java_Collections


    Now, if we want to find the height of Node 'C'.

    We will start from Node 'C' and try finding a path that leads us to the leaf that is a descendant of 'C'. And that leaf is 'I'.

    And the path would be, 'C' to 'H' then from 'H' to 'I'.

    So, we had to travel through two edges. And 2 is the height of the Node 'C'.

    Similarly, if we need to find the height of Node 'B' then we will try finding a path that leads us to the leaf that is a descendant of 'B'. And that is 'D'.

    And the path would be 'B' to 'D'.

    And the height of Node 'B' would be 1.

    So, can you say what would be the height of Node 'E'.

    Well ! It would be 0. Because there would be no paths at all.
  12. Depth of a Node

    'Depth of a Node' is calculated just the opposite way the Height of a Node is calculated.

    java_Collections


    Say, if we want to calculate the depth of the Node 'G'. We will find a path that will lead us to the root node 'A'.

    So, the path would be, 'G' to 'C' and 'C' to 'A'. So, there are two edges in the path.

    And the depth of Node 'G' would be 2.
  13. Degree of a Node

    The 'Degree of a Node' would be the number of children a Node has.

    So, the Degree of the Node 'B' is 2, as 'B' has 2 children.

    And the degree of Node 'C' is 3, as 'C' has 3 Children.

Types of Tree


  1. Binary Tree
  2. Binary Search Tree
  3. AVL Tree
  4. B-Tree
  5. Red Black Tree