Data Structures and Algorithms 4.2 - Trees
Data Structures and Algorithms 4.2 - Trees
Root Node
Parent Node
Child Node
Grand Parent Node
Leaf Node
Non-leaf Nodes
Path
Predecessor
Successor
Ancestor
Descendant
Siblings
Degree of Node
Degree of Tree
Depth of Node
Height of Node
Tree Terms
1.Root – Root is a special node in a tree. The entire tree is referenced
through it. It does not have a parent.
2.Parent Node – Parent node is an immediate predecessor of a node.
3.Child Node – All immediate successors of a node are its children.
4.Siblings – Nodes with the same parent are called Siblings.
5.Leaf – Last node in the tree. There is no node after this node.
6.Edge – Edge is a connection between one node to another. It is a line
between two nodes or a node and a leaf.
7.Path – Path is a number of successive edges from source node to
destination node.
Tree Terms
1.Tree can be termed as a RECURSIVE data structure.
2.In a valid tree for N Nodes we have N-1 Edges/Links
3.Depth of Node – Depth of a node represents the number of edges
from the tree’s root node to the node.
4.Height of Node – Height of a node is the number of edges on the
longest path between that node & a leaf.
5.Height of Tree – Height of tree represents the height of its root node.
Types of Trees
• General Tree
• Binary Tree
• Binary Search Tree
• AVL Tree
• Heap
• Spanning Tree
• B-Tree
• B+ Tree
• Multi Tree
Tree Traversal
• Traversing a tree means visiting every node in the tree. You might, for
instance, want to add all the values in the tree or find the largest one.
• Depending on the order in which we do this, there can be three types
of traversal.
• Inorder traversal
1.First, visit all the nodes in the left subtree
2.Then the root node
3.Visit all the nodes in the right subtree
• Preorder traversal
1.Visit root node
2.Visit all the nodes in the left subtree
3.Visit all the nodes in the right subtree
• Postorder traversal
1.Visit all the nodes in the left subtree
2.Visit all the nodes in the right subtree
3.Visit the root node
• Breadth-first search is a tree traversal
algorithm that explores nodes level by
level. Using a queue to store frontier
nodes supports the behavior of this
search.
• BFS(Breadth First Search) uses Queue
data structure
• ABCDEFG
• Here, siblings are visited before the
children.
• Nodes that are traversed several times
are deleted from the queue.
• In BFS there is no concept of
backtracking.
• Depth-first search is another tree traversal
algorithm that goes deep into a tree exploring
for nodes branch by branch. Using a stack to
store frontier nodes supports the behavior of
this search.
• (ABDECFG)
• DFS(Depth First Search) uses Stack data
structure.
• DFS builds the tree sub-tree by sub-tree.
• Here, children are visited before the siblings.
• The visited nodes are added to the stack and
then removed when there are no more nodes to
visit.
Tree Applications
• Store hierarchical data, like folder structure, organization structure data.
• Binary Search Tree is a tree that allows fast search, insert, delete on a
sorted data.
• Heap is a tree data structure which is implemented using arrays and
used to implement priority queues.
• B-Tree and B+ Tree are used to implement indexing in databases.
• Used to store router-tables in routers.
• Used by compilers to build syntax trees.
• Used to implement expression parsers and expression solvers.
Binary Tree
A binary tree is a tree data structure in which each parent node can have
at most two children. Each node of a binary tree consists of three items:
• data item
• address of left child
• address of right child
Types of Binary Tree
Full Binary Tree, Perfect Binary Tree, Complete Binary Tree, Degenerate
Tree, Skewed Tree, Balanced Tree.
Types 0f Binary Tree
• Full Binary tree- It is a special kind of a binary tree that has either zero
children or two children. It means that all the nodes in that binary tree
should either have two child nodes of its parent node or the parent node
is itself the leaf node or the external node.
• Complete Binary Tree - A complete binary tree is another specific type of
binary tree where all the tree levels are filled entirely with nodes, except
the lowest level of the tree. Also, in the last or the lowest level of this
binary tree, every node should possibly reside on the left side
• Perfect Binary Tree- A binary tree is said to be ‘perfect’ if all the internal
nodes have strictly two children, and every external or leaf node is at the
same level or same depth within a tree.
Types 0f Binary Tree
• Degerate Binary Tree - A binary tree is said to be a degenerate binary
tree or pathological binary tree if every internal node has only a single
child.
• Balanaced Binary Tree - In a balanced binary tree, the height of the
left and the right subtrees of each node should vary by at most one.
Df= Height of left sub tree – Height of right sub tree
Binary Tree Representation
• A node of a binary tree is represented by a structure containing a data
part and two pointers to other structures of the same type.
• There are three binary tree components. Every binary tree node has
these three components associated with it. It becomes an essential
concept for programmers to understand these three binary tree
components:
1.Data element
2.Pointer to left subtree
3.Pointer to right subtree
Binary Search Tree (BST)
• Binary search tree is a data structure that quickly allows us to
maintain a sorted list of numbers.
• Each Node has at most 2 nodes.
• Maximum No. of possible nodes at each level n is 2n
• Maximum no. of possible nodes at height is 2^(h+1) -1
• Minimum no. of possible nodes at height is h+1
• If there are n nodes in a binary search tree, maximum height of the
binary search tree is (log2(n+1))-1.
Type of Tree Max. Node Min. Node
Binary Tree 2^(h+1) -1 h+1
Complete Binary Tree 2^(h+1) -1 2h
Full Binary Tree 2^(h+1) -1 2h+1
3 + ((5+9)*2)
Types of expression trees
• Infix expression
• Prefix expression and
• Postfix expression
• In order traversal of expression tree produces infix version of given
postfix expression (same with postorder traversal it gives postfix
expression)
Expression Tree from stack
1.If a character is an operand push that into the stack
2.If a character is an operator pop two values from the stack make them
its child and push the current node again.
• In the end, the only element of the stack will be the root of an
expression tree.
Practice - Construct Expression tree for
• Infix Expression
• ( a + b* c ) + ( ( d * e + f ) * g)
• a*b/c + e/f * g+k-x*y
• (a+(b*c))+(d*(e + f))
• Prefix Expression
• -++/*abc*/efgk*xy
• ++a*bc*d+ef
• Postfix Expression
• ab*c/ef/g*+k+xy*-
• abc*+def+*+
Practice - Write the inorder, postorder and
preorder traversal of the Expression Tree
AVL Tree
• An AVL tree defined as a self-balancing
Binary Search Tree (BST) where the difference
between heights of left and right subtrees for
any node can only be [-1,0,1]
• The difference between the heights of the left
subtree and the right subtree for any node is
known as the balance factor of the node.
• The above tree is AVL because the differences
between the heights of left and right subtrees
for every node are less than or equal to 1.
Types of Rotations
Left Rotation
• When a node is added into the right subtree of the right subtree, if the
tree gets out of balance, we do a single left rotation.
Right Rotation
• If a node is added to the left subtree of the left subtree, the AVL tree
may get out of balance, we do a single right rotation.
Left Right Rotation
• A left-right rotation is a combination in which first left rotation takes
place after that right rotation executes.
Right Left Rotation
• A right-left rotation is a combination in which first right rotation takes
place after that left rotation executes.
Insertion Operation in AVL
• Step 1 - Insert the new element into the tree using Binary Search Tree
insertion logic.
• Step 2 - After insertion, check the Balance Factor of every node.
• Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for
next operation.
• Step 4 - If the Balance Factor of any node is other than 0 or 1 or -
1 then that tree is said to be imbalanced. In this case, perform
suitable Rotation to make it balanced and go for next operation.
Construct AVL tree for the following data 21,26,30,9,4,14,28,18,15,10,2,3,7
Practice questions
Construct the AVL tree of the following elements. For each question
delete 5, 6 then 12.
• 1,2,3,4,5,6,7,8
• 5,10,3,4,7,11,9,8,6,12,2,1