Week 10 Trees
Week 10 Trees
Trees
Tree
• Tree is non-linear Data Structures.
• It is collection of different nodes
linked together.
• The node is used to store data.
• The edge is the link that connects
two nodes.
• If there are N nodes then there are
N-1 edges.
• The link between the nodes is uni-
directional
• There are no closed paths in a tree.
Applications of Trees
• One reason to use trees might be because you want to store
information that naturally forms a hierarchy. For example, the file
system on a computer file system.
• Make information easy to search (see tree traversal).
• Manipulate hierarchical data.
Some important terms −
• Node
Node is used to store data and the address of the linked nodes.
• Parent
The immediate predecessor of a node is called parent of that node.
• Child
The immediate successors of a node are called children of that node.
• Root node
The very first node of the tree which has no parent is called root node.
• Leaf Node
The node which has no child is called leaf node.
Some important terms −
• Non-Leaf Node
All the nodes except leaf nodes are called non-leaf nodes.
• Path
The sequence of consecutive nodes from source to destination.
• Ancestor
All the predecessor nodes of a node are called ancestors of that node.
• Descendant
All the successor nodes of a node are called descendant of that node.
Some important terms −
• Subtree
Tree of tree
• Depth of a node
The distance between that node and the root node is called depth of that node.
• Height of a node
The maximum distance between that node and any leaf node is called height of
that node.
• Height of Tree
The longest distance from root to leaf node.
• Level
Level of a node represents the generation of a node. If the root node is at level 0,
then its next child node is at level 1, its grandchild is at level 2, and so on.
Some important terms −
• Siblings
The nodes which have same parents are called siblings.
• Degree of node
The number of children of that node is called the degree of that node.
• Degree of tree
The maximum degree of any node in the tree is called the degree of the
tree.
• Balance factor
Balance factor = height of left sub tree – height of right sub tree
Types of Trees
•Simple Tree
•Binary Tree
•Binary search Tree
•AVL Tree
Simple Tree
Binary Tree
• Every
node can have
maximum two children.
• Oryou can say that the
degree of tree must be two or
less than two.
Full Binary Tree
•A binary tree is full binary tree
if it contains the maximum
possible number of nodes at
all levels
• Maximum number of nodes in
a level can be calculated the
formula:
• No. of nodes= 2L
Complete Binary Tree
•A binary tree is said to be complete binary
tree if all its levels, except possibly the last
level, have maximum number of possible
nodes, and all the nodes in the last level
appear as far left as possible
Binary Search Tree(Assignment 2 )
• It is a binary tree.
• Allthe smaller nodes
at the left side of the
node.
• Allthe greater nodes
at the right side of the
node.
Operations
•Insertion
•Searching
•Deleting
•Display
Insert the following nodes in an empty binary tree.
12,19,5,11,38,49,22,46,2,10
12
Insert the following nodes in an empty binary tree.
12,19,5,11,38,49,22,46,2,10
12
19
Insert the following nodes in an empty binary tree.
12,19,5,11,38,49,22,46,2,10
12
19
5
Insert the following nodes in an empty binary tree.
12,19,5,11,38,49,22,46,2,10
12
19
5
11
Insert the following nodes in an empty binary tree.
12,19,5,11,38,49,22,46,2,10
12
19
5
38
11
Insert the following nodes in an empty binary tree.
12,19,5,11,38,49,22,46,2,10
12
19
5
38
11
49
Insert the following nodes in an empty binary tree.
12,19,5,11,38,49,22,46,2,10
12
19
5
38
11
22 49
Insert the following nodes in an empty binary tree.
12,19,5,11,38,49,22,46,2,10
12
19
5
38
11
22 49
46
Insert the following nodes in an empty binary tree.
12,19,5,11,38,49,22,46,2,10
12
19
5
38
2 11
22 49
46
Insert the following nodes in an empty binary tree.
12,19,5,11,38,49,22,46,2,10
12
19
5
38
2 11
22 49
10
46
Insert the following nodes in an empty binary tree.
12,19,5,11,38,49,22,46,2,10,10
12
19
5
38
2 11
22 49
10
46
Example Tasks
•12 2 15 78 46 7 -5 5
•L A C P E S T B
Delete
•Case 1 ( Node with no left and right child )
•For this we will just simply delete that node
12
19
5
38
2 11
22 49
9
46
Delete 46
12
19
5
38
2 11
22 49
10
46
Tree After deleting 46
12
19
5
38
2 11
22 49
10
Delete 12
12
19
5
38
2 11
22 49
10
Delete 12
12
19
5
38
2 11
22 49
10
Delete 12
Swap 12 with greater Node from Left Subtr
12
19
5
38
2 11
22 49
10
Delete 12
Swapped
11
19
5
38
2 12
Now swap 12
With its left child
22 49
10
Delete 12
11
19
5
38
2 10
Swapped
22 49
12
Delete 12
11
19
5
38
2 10
22 49
12 Now delete 12
Tree After deleting 12
11
19
5
38
2 10
22 49
Display
•InOrderDisplay ( L P R )
•PreOrder Display ( P L R )
•PostOrder Display ( L R P )
INOrder ( L P R )
2 11
2 5 11
PREOrder (P L R )
2 11
5 2 11
POSTOrder ( L R P )
2 11
2 11 5
Practice Quiz(Quiz 5)
Practice Quiz
PreOrder(NLR):a,b,d,g,e,
c,f,h
InOrder(LNR):d,g,b,e,a,c,
h,f
PostOrder(LRN):
g,d,e,b,h,f,c,a