Tree
Linear and Non Linear Data Structure
• Arrays, Linked lists, Stacks, Queues are linear structure.
Why linear
• The elements are totally ordered.
• For any pair of elements, one precedes the other.
• We can have non-linear structure
Trees, Graphs are non linear data strucutre
• Elements are not totally ordered.
Tree Data structure
● A tree is a finite set of one or more nodes such that:
o There is a specially designated node called the root.
o The remaining nodes are partitioned into n>=0 disjoint sets T1, ..., Tn, where each of these sets is a tree.
o We call T1, ..., Tn the subtrees of the root.
Tree representation
A
J I
H
F E
D C
M
P Q
L K
Tree representation
A
J I
H
F D E
C
M
P Q
L K
Subtree with single node
Subtree with root D
Subtree with root E
Subtree with root F Root
Tree Terminology
A
G F
D E B C
H
Root Parent Child Siblings Leaves
Root is the node which has no parent.
Siblings are nodes which has common parent.
Leaves are node which has no child node.
Every node except Root node has parent.
Level 0
Level 1
Level 2
I Level 3
Tree Terminology
A
G F
D E B C
H
Root Parent Child Siblings Leaves
A is Root Node
A is Parent of B, C, D and E F and G are Child of D
F, and G are Siblings
B, C, F, G and I are leaves
Level 0
Level 1
Level 2
I Level 3
Tree Terminology
Height (Depth) of Tree is= max. level number +1.
The Degree of node is equal to its Childs. Or in other words, it is the number f subtrees of a node in a given tree.
External Node: a node which has no child.
Internal Node: a node which has at least one child.
Terminal Node (s): A node with degree zero is called a terminal node or a leaf.
Non-terminal node: A node (except the root) whose degree is not zero
Tree Terminology
A
G F
D E B C
Height of Tree is 4 H
The Degree of node D is 2
B, C, F, G and I are External
A , D, E, H are internal node of Tree
Level 0
Level 1
Level 2
I Level 3
Binary Trees
Binary Tree
Binary Tree:
Ordered tree with all nodes having at most two children
Some examples
A
H G
F E
D
C B
I
Binary tree
Structures which are not binary trees
A
G
F E
D
C B
I
Various binary trees
• Ordered binary Tree:
Is a tree in which the children of each node are ordered.
• Strictly Binary Tree:
If every non leaf node in a binary tree has non empty left and right sub trees then such tree is termed as a strictly binary tree.
• Complete Binary Tree:
A complete binary tree of height h is the strict binary tree all of whose leaves are at level h.
A
E G D
C B
H I
F
J
1
2 3
4 5 6 7
8 9 10
Ordered Binary Tree
Ex: of a strictly binary tree
A
E C
B
D
F G
A
J N E F D
C B
H I K O
G
L M
A COMPLETE BINARY TREE OF DEPTH 3
Complete Binary Tree
• Level i has 2i nodes
• in a tree of height h
Leaves are at level h No of leaves is 2h
Binary Search Tree
Binary Search Tree
• Stores keys in the nodes in a way so that searching, insertion and deletion can be done efficiently.
Binary Search tree property
For every node X, all the keys in its left subtree are smaller than the key value in X, and all the keys in its right subtree are larger than the key value in X
Binary Search Trees
A binary search tree
Not a binary search tree
Design a Binary Search Tree
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13 Root
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
3
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
3
4
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
3
4
12
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
3
4
12
14
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
3
4
12
14
10
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
3
4
12
14
10
5
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
3
4
12
14
10
5 1
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
3
4
12
14
10
5 1
8
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
3
4
12
14
10
5 1
8 2
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
3
4
12
14
10
5 1
8 2
7
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
3
4
12
14
10
5 1
8 2
7 9
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
3
4
12
14
10
5 1
8 2
7 9
11
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
3
4
12
14
10
5 1
8 2
7 9
11
6
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
3
4
12
14
10
5 1
8 2
7 9
11
6
18
Operations on Binary Trees
• INSERT
• SEARCH
• DELETE
Binary Tree Traversals
Tree Traversal
• Many binary tree operations are done by performing a traversal of the binary tree.
• In a traversal, each element of the binary tree is
visited exactly once.
Three tree traversal methods
• Preorder traversal
• Inorder traversal
• Postorder traversal
Preorder Traversal
Steps:
• Visit the root
• Traverse the left subtree in preorder
• Traverse the right subtree in preorder
Traversing a Tree Preorder
A
C B
D E F G
I H
Traversing a Tree Preorder
A
C B
D E F G
I H
Result: A
Traversing a Tree Preorder
A
C B
D E F G
I H
Result: AB
Traversing a Tree Preorder
A
C B
D E F G
I H
Result: ABD
Traversing a Tree Preorder
A
C B
D E F G
I H
Result: ABDE
Traversing a Tree Preorder
A
C B
D E F G
I H
Result: ABDEH
Traversing a Tree Preorder
A
C B
D E F G
I H
Result: ABDEHC
Traversing a Tree Preorder
A
C B
D E F G
I H
Result: ABDEHCF
Traversing a Tree Preorder
A
C B
D E F G
I H
Result: ABDEHCFG
Traversing a Tree Preorder
A
C B
D E F G
I H
Result: ABDEHCFGI
Inorder Traversal (symmetric order)
Steps for a nonempty binary tree:
• Traverse the left subtree in inorder
• Visit the root
• Traverse the right subtree in inorder
Traversing a Tree Inorder
A
C B
D E F G
I H
Traversing a Tree Inorder
A
C B
D E F G
I H
Traversing a Tree Inorder
A
C B
D E F G
I H
Traversing a Tree Inorder
A
C B
D E F G
I H
Result: D
Traversing a Tree Inorder
A
C B
D E F G
I H
Result: DB
Traversing a Tree Inorder
A
C B
D E F G
I H
Result: DB
Traversing a Tree Inorder
A
C B
D E F G
I H
Result: DBH
Traversing a Tree Inorder
A
C B
D E F G
I H
Result: DBHE
Traversing a Tree Inorder
A
C B
D E F G
I H
Result: DBHEA
Traversing a Tree Inorder
A
C B
D E F G
I H
Result: DBHEA
Traversing a Tree Inorder
A
C B
D E F G
I H
Result: DBHEAF
Traversing a Tree Inorder
A
C B
D E F G
I H
Result: DBHEAFC
Traversing a Tree Inorder
A
C B
D E F G
I H
Result: DBHEAFCG
Traversing a Tree Inorder
A
C B
D E F G
I H
Result: DBHEAFCGI
Postorder Traversal
Steps:
• Traverse the left subtree in postorder
• Traverse the right subtree in postorder
• Visit the root
Traversing a Tree Postorder
A
C B
D E F G
I H
Traversing a Tree Postorder
A
C B
D E F G
I H
Result:
Traversing a Tree Postorder
A
C B
D E F G
I H
Result:
Traversing a Tree Postorder
A
C B
D E F G
I H
Result: D
Traversing a Tree Postorder
A
C B
D E F G
I H
Result: D
Traversing a Tree Postorder
A
C B
D E F G
I H
Result: DH
Traversing a Tree Postorder
A
C B
D E F G
I H
Result: DHE
Traversing a Tree Postorder
A
C B
D E F G
I H
Result: DHEB
Traversing a Tree Postorder
A
C B
D E F G
I H
Result: DHEB
Traversing a Tree Postorder
A
C B
D E F G
I H
Result: DHEBF
Traversing a Tree Postorder
A
C B
D E F G
I H
Result: DHEBF
Traversing a Tree Postorder
A
C B
D E F G
I H
Result: DHEBFI
Traversing a Tree Postorder
A
C B
D E F G
I H
Result: DHEBFIG
Traversing a Tree Postorder
A
C B
D E F G
I H
Result: DHEBFIGC
Traversing a Tree Postorder
A
C B
D E F G
I H
Result: DHEBFIGCA
Tree Traversal – Example 1
A
D F
C B
G
E
I H
Preorder:
Inorder:
Postorder:
ABDGCEHIF DGBAHEICF GDBHIEFCA
Tree Traversal – Example 2
A
F C
B
I J
E H
D
G
L K
Preorder: ABCEIFJDGHKL
Inorder: EICFJBGDKHLA
Postorder: IEJFCGKLHDBA
General Trees
Forests
• Definition: A forest is a set of n ≥ 0 disjoint trees.
• When we remove a root from a tree, we’ll get a forest. E.g., Removing the root of a binary tree will get a forest of two trees.
A
I H
E G
D
B C F
Figure 5.34 Three-tree forest
Transforming A Forest Into A Binary Tree
• T1, …, Tn
forest of trees
• B(T1, …, Tn)
binary tree
• Algorithm
Is empty if n = 0
Has root equal to root(T1)
Has left subtree equal to B(T11, …, T1m) Has right subtree equal to B(T2, …, Tn)
Transforming A Forest Into A Binary Tree
A
I H
E
G
D B
C F
A
I H
E G
D
B C
F