Trees
Trees
Definition
1. Root-
• The first node from where the tree
originates is called as a root node.
• In any tree, there must be only one
root node.
• We can never have multiple root nodes
in a tree data structure.
2. Edge
• The connecting link between any two
nodes is called as an edge.
• In a tree with n number of nodes, there
are exactly (n-1) number of edges.
3. Parent
• The node which has a branch from it to any
other node is called as a parent node.
• In other words, the node which has one or
more children is called as a parent node.
• In a tree, a parent node can have any Here,
•Node A is the parent of nodes B and C
number of child nodes. •Node B is the parent of nodes D, E and F
•Node C is the parent of nodes G and H
•Node E is the parent of nodes I and J
•Node G is the parent of node K
4. Child
• The node which is a descendant of
some node is called as a child node.
• All the nodes except root node are
Here,
child nodes. •Nodes B and C are the children of node A
•Nodes D, E and F are the children of node B
•Nodes G and H are the children of node C
•Nodes I and J are the children of node E
•Node K is the child of node G
5. Siblings
• Nodes which belong to the same
parent are called as siblings.
• In other words, nodes with the same
parent are sibling nodes.
Algorithm-
3.GCDBFEA
4.FDEGCBA
– Which of the following binary trees
has its inorder and preorder
traversals as BCAD and ABCD
respectively-
Here,
•Number of leaf nodes = 3
•Number of nodes with 2 children = 2
•Clearly, number of leaf nodes is one greater than numbe
of nodes with 2 children.
– Maximum number of nodes at any level ‘L’ in a
binary tree= 2L
– Maximum number of nodes at level-2 in
a binary tree
= 22
=4
Thus, in a binary tree, maximum number
of nodes that can be present at level-2 =
4.
Binary Search Tree
Inorder Traversal-
Postorder Traversal-
In this question,
We are provided with the preorder traversal sequence.
We write the inorder traversal sequence by arranging all the numbers in
ascending order.
Then-
Preorder Traversal : 30 , 20 , 10 , 15 , 25 , 23 , 39 , 35 , 42
Inorder Traversal : 10 , 15 , 20 , 23 , 25 , 30 , 35 , 39 , 42
Now,
We draw a binary search tree using these traversal results.
Binary Search Tree Operations-
– In AVL tree,
• Balance factor is defined for every node.
• Balance factor of a node = Height of its left subtree – Height of
its right subtree
• Balance factor of every node is either 0 or 1 or -1.
AVL Tree Operations-
– Case-01:
• After the operation, the balance factor of each node is either 0 or 1 or -1.
• In this case, the AVL tree is considered to be balanced.
• The operation is concluded.
– Case-02:
• After the operation, the balance factor of at least one node is
not 0 or 1 or -1.
• In this case, the AVL tree is considered to be imbalanced.
• Rotations are then performed to balance the tree.
AVL Tree Rotations-
– Property-01:
Maximum possible number of nodes in AVL tree of height H = 2^(H+1) – 1
Property-02:
– Minimum number of nodes in AVL Tree of height H is given by a
recursive relation N(H) = N(H-1) + N(H-2) + 1
– Base conditions for this recursive relation are-
• N(0) = 1
• N(1) = 2
– Property-03:
– Minimum possible height of AVL Tree using N nodes = ⌊log2N⌋
– Minimum possible height of AVL Tree using 8 nodes
= ⌊log28⌋
= ⌊log223⌋
= ⌊3log22⌋
= ⌊3⌋
=3
– Property-04:
• Maximum height of AVL Tree using N nodes is calculated
using recursive relation- N(H) = N(H-1) + N(H-2) + 1
– Base conditions for this recursive relation are-
• N(0) = 1
• N(1) = 2
– NOTE-
• If there are n nodes in AVL Tree, its maximum height can
not exceed 1.44log2n.
• In other words, Worst case height of AVL Tree with n nodes
= 1.44log2n.
Insertion in AVL Tree-
– Case-02:
• After the insertion, the balance factor of at least one node is not 0 or 1 or -1.
• In this case, the tree is considered to be imbalanced.
• Perform the suitable rotation to balance the tree.
• After the tree is balanced, insert the next element if any.
– Rules To Remember-
– Rule-01:
– After inserting an element in the existing AVL tree,
• Balance factor of only those nodes will be affected that lies
on the path from the newly inserted node to the root node.
– Rule-02:
– To check whether the AVL tree is still balanced or not after
the insertion,
• There is no need to check the balance factor of every node.
• Check the balance factor of only those nodes that lies on
the path from the newly inserted node to the root node.
– Rule-03:
– After inserting an element in the AVL tree,
• If tree becomes imbalanced, then there exists one particular node in the tree by
balancing which the entire tree becomes balanced automatically.
• To re balance the tree, balance that particular node.