Trees
Trees
• A tree is an acyclic graph in which there exists only one path between any pair of
vertices.
• A tree is a graph with minimum edges to connect every vertex.
• If n= number of vertices, then number of edges=n-1.
Tree
• The root of a tree is the node with no parents. There can be at most one root node in a tree.
• An edge refers to the link from parent to child.
• A node with no children is called leaf node (E,J,K,H and I).
• Children of same parent are called siblings (B,C,D are siblings of A, and E,F are the siblings of
B).
• A node p is an ancestor of node q if there exists a path from root to q and p appears on the
path. The node q is called a descendant of p. For example, A,C and G are the ancestors of K.
• The set of all nodes at a given depth is called the level of the tree (B, C and D are the same
level). The root node is at level zero.
• The depth of a node is the length of the path from the root to the node (depth of G is 2, A – C
– G).
Tree
• The height of a node is the length of the path from that node to the deepest
node.
• The height of a tree is the length of the path from the root to the deepest node in
the tree.
• A (rooted) tree with only one node (the root) has a height of zero. In the previous
example, the height of B is 2 (B – F – J).
• If every node in a tree has only one child (except leaf nodes) then we call such
trees skew trees.
• If every node has only left child then we call them left skew trees.
• Similarly, if every node has only right child then we call them right skew trees.
Binary Trees
• A tree is called binary tree if each node has zero child, one child or two children.
• Empty tree is also a valid binary tree.
Types of Binary Trees
• Strict Binary Tree: A binary tree is called strict binary tree if each node has exactly
two children or no children.
Types of Binary Trees
• Full Binary Tree: A binary tree is called full binary tree if each node has exactly
two children and all leaf nodes are at the same level.
Types of Binary Trees
• Complete Binary Tree: Let us assume that the height of the binary tree is h. A
binary tree is called complete binary tree if all leaf nodes are at height h or h – 1
and also without any missing number in the sequence.
Structure of Binary Trees
struct Node {
int data;
struct Node *left;
struct Node *right;
};
Traversal Methods
• Preorder traversal (NLR)
• Inorder traversal (LNR)
• Postorder traversal (LRN)
Preorder Traversal
• Preorder traversal is defined as follows:
• Visit the root.
• Traverse the left subtree in Preorder.
• Traverse the right subtree in Preorder.
Step 2: END
Insertion in BST
Input: [40,20,60,10,30,50,70,25]
40
20 60
10 30 50 70
9
Extended Binary Trees
• An extended binary tree is also known as a 2-tree.
• A binary tree can be converted to an extended binary tree by adding
new nodes to its leaf nodes and to the nodes that have only one child.
• The new nodes that are added to binary tree (to make it extended
binary tree) are called external nodes.
Extended Binary Trees
General Trees
• General tree is a tree in which each node can have either zero or many
child nodes.
• It can not be empty.
• In general tree, there is no limitation on the degree of a node.
• The topmost node of a general tree is called the root node. There are
many subtrees in a general tree.
• In a general tree, each node has in-degree(number of parent nodes)
one and maximum out-degree(number of child nodes) n.
AVL (Adelson-Velskii and Landis) Trees
• AVL tree is a binary search tree with a balance condition: the
difference between left subtree height and right subtree height is at
most 1.
• For every node, |HLT – HRT| <= 1
struct AVLTreeNode{
struct AVLTreeNode *left;
int data;
struct AVLTreeNode *right;
int height;
};
Rotations
• RR single (anticlockwise)
• Insertion into right subtree of right child of α makes the tree unbalanced.
• LL single (clockwise)
• Insertion into left subtree of left child of α makes the tree unbalanced.
• LR double (RR+LL)
• Insertion into right subtree of left child of α makes the tree unbalanced.
• RL double (LL+RR)
• Insertion into left subtree of right child of α makes the tree unbalanced.
Left Rotation (LL)
Right Rotation (RR)
LR Rotation
RL Rotation
AVL Trees Visualization