0% found this document useful (0 votes)
5 views

10.-Tree

Uploaded by

Poushie
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

10.-Tree

Uploaded by

Poushie
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Data

CSE 203: Data Structure Structure


TREE:

What is this?
Data
CSE 203: Data Structure Structure
TREE:

What is this?
Data
CSE 203: Data Structure Structure
TREE:

Cropped tree.
Data
CSE 203: Data Structure Structure
TREE:

Identify the
branching points.
Data
CSE 203: Data Structure Structure
TREE:

Identify the leaf


points.
Data
CSE 203: Data Structure Structure
TREE:

Number each 1
interior points/
nodes. 2 3

4 7
5 6
Data
CSE 203: Data Structure Structure
TREE:

Number each leaf 1


points/ nodes.
2 3

4 7
5 6
8 9
10 11 13 14 15
12
Data
CSE 203: Data Structure Structure
TREE: 1

2 3
1
4 7
5 6
2 3 8 9
10 11 13 14 15
12
4 7
5 6
8 9 Drawing an instance
10 11 13 14 15
of the tree.
12
Data
CSE 203: Data Structure Structure
TREE: 1

2 3
1
4 7
5 6
2 3 8 9
10 11 13 14 15
Reshaping the 12
4 7
5 6 drawn tree. 1
8 9
10 11 13 14 15
12
2 3

4 5 6 7

8 9 10 11 12 13 14 15
Data
CSE 203: Data Structure Structure
TREE: 1

This is called tree.


2 3

4 5 6 7

A tree T has a root node, R 8 9 10 11 12 13 14 15


Each inner node contains one or more successors
If the root R has successors, each successor branches to a subtree.
The terminal nodes do not have any successor. These are called leaf nodes.
The tree T is called empty tree, if its root R is empty.
If N is a node in T with left successor S1 and right successor S2, N
N is called the parent of S1 and S2.
S1 and S2 are called child of N
S1 and S2 are called siblings.
Line drawn from a node N to its successor S is called edge. S1 S2
A sequence of consecutive edges is called a path.
A path ending in a leaf is call branch
Data
CSE 203: Data Structure Structure
TREE: 0 1

1 2 3

2 4 5 6 7

3 8 9 10 11 12 13 14 15

Level of Tree:
The root R is assigned a level number 0
Every node is assigned a level number that is 1 more than its parent.
The nodes with the same level are called same generation.
Data
CSE 203: Data Structure Structure
TREE:
Depth/Height of Tree:
Maximum number of nodes in a branch.
It is 1 more than the largest level

Depth is 5

Similar Tree:
Two tree T1 and T2 are called similar if they have
the same structure
Data
CSE 203: Data Structure Structure
TREE: 1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Binary Tree:
A binary tree T contains a distinguished node R.
Each contains no more than two children.
Remaining nodes of T forms an order pair of disjoint binary tree T1 and T2
Data
CSE 203: Data Structure Structure
TREE: 1

Complete Binary Tree:


A tree is said complete if all its levels, except the last, 2 3
have the possible maximum number of nodes.
4 5 6 7

8 9 10 11 12

2 3
Not complete

4 5 6

7 8 9 10 11
Data
CSE 203: Data Structure Structure
TREE: 1

Extended Binary Tree:


A tree is said extended binary or 2-tree if each node 2 3
either have 0 or 2 children.
Nodes with 2 children are called internal node 7
4 5 6
Nodes with 0 children are called external nodes
Internal nodes are represented by circles
External nodes are represented by rectangles 8 9 10 11 12
What will happen for such a tree?
Convert it to 2-tree by replacing each
empty sub-tree by a new node
Data
CSE 203: Data Structure Structure
TREE: Representation of a binary tree in memory 1

2 3

4 5 6 7

8 9 10 11 12 13 14 15
Link List Representation:
Data
CSE 203: Data Structure Structure
TREE: Representation of a binary tree in memory
Link List Representation:

LEFT INFO RIGHT


Data
CSE 203: Data Structure Structure
TREE: Representation of a binary tree in memory
Link List Representation:
Data
CSE 203: Data Structure Structure
TREE: Representation of a binary tree in memory 1

2 3

4 5 6 7

8 9 10 11 12 13 14 15
Sequential Representation:
Store items level by level and sequentially in an array

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Data
CSE 203: Data Structure Structure
TREE: Representation of a binary tree in memory a

b c

d e f g

h i j k l m n o
Sequential Representation:
Store items level by level and sequentially in an array

a b c d e f g h i j k l m n o
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Data
CSE 203: Data Structure Structure
TREE: Representation of a binary tree in memory
Sequential Representation:
Data
CSE 203: Data Structure Structure
TREE: Traversing
Preorder
Inorder
Postorder
Preorder: A, B, D, E, C, F

Inorder: D, B, E, A, C, F

Postorder: D, E, B, F, C, A
Data
CSE 203: Data Structure Structure
TREE: Binary Search Tree
A tree T is a binary search tree if each node N contains a value that is greater than
the nodes in the left sub-tree and smaller than the nodes in the right sub-tree.
Data
CSE 203: Data Structure Structure
TREE: Binary Search Tree
Items: 40, 60, 50, 33, 55, 11
Data
CSE 203: Data Structure Structure
TREE:
Data
CSE 203: Data Structure Structure
TREE:
Data
CSE 203: Data Structure Structure
TREE:
Data
CSE 203: Data Structure Structure
TREE:
Data
CSE 203: Data Structure Structure
TREE:
Data
CSE 203: Data Structure Structure
TREE:
Data
CSE 203: Data Structure Structure
TREE:
Data
CSE 203: Data Structure Structure
TREE: Application to coding Label each edge to a left child by 0
Label each edge to a right child by 1

Huffman’s Code:
0 1 Traverse from root to leaf
and print the traversed
edge labels
0 1 0 1
Example:
A: 00
0 1 1
0 B: 11011
C: 011
D: 111
0 1 E: 11010
F: 010
G: 10
0 1 H: 1100

You might also like