You are on page 1of 26

Data Structure and

Algorithms
Lecture # 5
Tree Data Structure

1
Binary Tree Data Structure
• A tree whose elements have at most 2 children is called a binary tree. Since each element in a
binary tree can have only 2 children, we typically name them the left and right child.

• A Binary Tree node contains following parts.


• Data
• Pointer to left child
• Pointer to right child

2
Binary Tree (Introduction)
• Trees: Unlike Arrays, Linked Lists, Stack and queues, which are linear data structures, trees are
hierarchical data structures.
• Tree Vocabulary: The topmost node is called root of the tree. The elements that are directly under
an element are called its children. The element directly above something is called its parent. For
example, ‘a’ is a child of ‘f’, and ‘f’ is the parent of ‘a’. Finally, elements with no children are
called leaves.

3
Trees
Root
D u s ty

H oney B ear B ra n d y

B r u n h ild e T e rry C o y o te N ugget

G ill T an sey T w eed Zoe C ro c u s P r im r o s e N ous B e lle

leaf

4
Definition of Tree
A tree is a finite set of one or more nodes such that:
There is a specially designated node called the root.
The remaining nodes are partitioned into n>=0 disjoint sets T1, ..., Tn, where each of these is
a tree.
We call T1, ..., Tn the subtrees of the root.

5
Tree Terminology
• A tree is a collection of elements (nodes)
• Each node may have 0 or more successors
• (Unlike a list, which has 0 or 1 successor)
• Each node has exactly one predecessor
• Except the starting / top node, called the root
• Links from node to its successors are called branches
• Successors of a node are called its children
• Predecessor of a node is called its parent
• Nodes with same parent are siblings
• Nodes with no children are called leaves
6
Tree Terminology
• Root: node without parent (A)
• Degree of a tree: the
• Siblings: nodes share the same parent maximum number of its node.
• Internal node: node with at least one • Subtree: tree consisting of a
child (A, B, C, F) node and its descendants
• External node (leaf ): node without
children (E, I, J, K, G, H, D)
• Ancestors of a node: parent, A
grandparent, grand-grandparent, etc.
• Descendant of a node: child,
grandchild, grand-grandchild, etc. B C D
• Depth of a node: number of ancestors
• Height of a tree: maximum depth of
any node (3) E F G H

• Degree of a node: the number of its


children
I J K
7
Binary Tree
• A tree in which every node can have a maximum of two children is
called as Binary Tree.

8
Full Binary Tree
• A binary tree in which every node has either two or zero number of
children is called Full Binary Tree

9
Complete Binary Tree
• A binary tree in which every internal node has exactly two children and all leaf
nodes are at same level is called Complete Binary Tree. Complete binary tree is
also called as Perfect Binary Tree
• Number of nodes = 2d+1 – 1
• Number of leaf nodes = 2d
• Where, d – Depth of the tree

10
Left Skewed and Right Skewed Trees

• Binary tree has only left sub trees - Left Skewed Trees
• Binary tree has only right sub trees - Right Skewed Trees

11
Binary Tree Representation
1. Sequential representation using arrays
2. List representation using Linked list

12
Sequential representation

• To represent a binary tree of depth ‘d' using array


representation, we need one dimensional array with a
maximum size of 2d+1 - 1.
13
Sequential representation
• Advantages:
• Direct access to all nodes (Random access)
• Disadvantages:
• Height of tree should be known
• Memory may be wasted
• Insertion and deletion of a node is difficult

14
List representation
struct node
{
int data;
struct node *left;
struct node *rifgt;
}

15
List representation
• Advantages:
• Height of tree need not be known
• No memory wastage
• Insertion and deletion of a node is done without affecting other nodes
• Disadvantages:
• Direct access to node is difficult
• Additional memory required for storing address of left and right node

16
Why Trees?
• 1. 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:

• 2. Trees (with some ordering e.g., BST) provide moderate access/search (quicker than Linked List and
slower than arrays).
3. Trees provide moderate insertion/deletion (quicker than Arrays and slower than Unordered Linked
Lists).
4. Like Linked Lists and unlike Arrays, Trees don’t have an upper limit on number of nodes as
nodes are linked using pointers.
17
Main applications of trees include:
1. Manipulate hierarchical data.
2. Make information easy to search (see tree traversal).
3. Manipulate sorted lists of data.
4. As a workflow for compositing digital images for visual effects.
5. Router algorithms
6. Form of a multi-stage decision-making (see business chess).

18
• Example
A is the root node Property: (# edges) = (#nodes) - 1
B is the parent of D and E
C is the sibling of B
D and E are the children of B
D, E, F, G, I are external nodes, or leaves
A, B, C, H are internal nodes
The level of E is 3 Level
The height (depth) of the tree is 4 A 1
The degree of node B is 2
The degree of the tree is 3
B C
The ancestors of node I is A, C, H 2
The descendants of node C is F, G, H, I
H
3
D E F G
I
4
19
Tree Node Insertion
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};

20
Search Operation
• Whenever an element is to be searched,
start searching from the root node, then if
the data is less than the key value, search
for the element in the left subtree.
Otherwise, search for the element in the
right subtree. Follow the same algorithm
for each node.

21
Traversal in Tree
• Traversal is a process to visit all the nodes of a tree and may print
their values too. Because, all nodes are connected via edges (links) we
always start from the root (head) node. That is, we cannot randomly
access a node in a tree. There are three ways which we use to
traverse a tree −
• In-order Traversal
• Pre-order Traversal
• Post-order Traversal

22
In-order Traversal

• In this traversal method, the left subtree is


visited first, then the root and later the right
sub-tree. We should always remember that
every node may represent a subtree itself.
• If a binary tree is traversed in-order, the
output will produce sorted key values in an
ascending order.
• We start from A, and following in-order
traversal, we move to its left subtree B. B is
also traversed in-order. The process goes on
until all the nodes are visited. The output of
inorder traversal of this tree will be −
• D→B→E→A→F→C→G

23
Pre-order Traversal

• In this traversal method, the root node


is visited first, then the left subtree and
finally the right subtree.
• We start from A, and following pre-
order traversal, we first visit A itself
and then move to its left subtree B. B is
also traversed pre-order. The process
goes on until all the nodes are visited.
The output of pre-order traversal of this
tree will be −
• A→ B → D→ E→ C → F→ G

24
Post-order Traversal
• In this traversal method, the root node is
visited last, hence the name. First we
traverse the left subtree, then the right
subtree and finally the root node.
• We start from A, and following Post-order
traversal, we first visit the left
subtree B. B is also traversed post-order.
The process goes on until all the nodes are
visited. The output of post-order traversal
of this tree will be −
• D→E→B→F→G→C→A
25
Binary Tree Traversals
• Preorder: Visit root, traverse left, traverse right
• Inorder: Traverse left, visit root, traverse right
• Postorder: Traverse left, traverse right, visit root

26

You might also like