10.-Tree
10.-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:
Number each 1
interior points/
nodes. 2 3
4 7
5 6
Data
CSE 203: Data Structure Structure
TREE:
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
4 5 6 7
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
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
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:
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