Week09 Tree
Week09 Tree
AND ALGORITHMS
OBJECTIVES
After the lesson, students can
1. Understand the concept of tree data structures and related concepts.
3
CONTENTS 1. DEFINITIONS AND GENERAL CONCEPTS
7 8
1. DEFINITIONS AND GENERAL CONCEPTS 1. DEFINITIONS AND GENERAL CONCEPTS
Manager
… …
9 10
11 12
1. DEFINITIONS AND GENERAL CONCEPTS 1. DEFINITIONS AND GENERAL CONCEPTS
13 14
15 16
1. DEFINITIONS AND GENERAL CONCEPTS 1. DEFINITIONS AND GENERAL CONCEPTS
17 18
19 20
1. DEFINITIONS AND GENERAL CONCEPTS CONTENTS
• 1.2. Terminology
1. Definitions and general concepts
Labeled Tree (Cây có nhãn)
Each node of the tree has a label or value
n1
* 2. Binary tree
The node's label is not the name of the node but the
value stored in it
n2 n3 -
For example: Consider a tree with 7 nodes n1, ..., n7. +
Label the buttons:
Node n1 is labeled *;
n4
Node n2 is labeled +; a n5 b n6 a n7 c
Node n3 has label -;
Node n4 has label a; Expression tree (a+b)*(a-c)
Node n5 has label b;
Node n6 has label a;
Node n7 is labeled c.
21 22
• Left child and right child • Each leaf node has the same depth
23 24
2. BINARY TREE 2. BINARY TREE
Example:
1. Which tree is complete?
2. Which tree is balance?
3. Which tree is full?
OBJECTIVES
27
CONTENTS 1. THE DATA STRUCTURE REPRESENTS THE TREE
29 30
1. THE DATA STRUCTURE REPRESENTS THE TREE 1. THE DATA STRUCTURE REPRESENTS THE TREE
• 1.1. Represent the tree by array • 1.1. Represent the tree by array
With this representation: the parent of a node can be determined in constant time. Example 1
31 32
1. THE DATA STRUCTURE REPRESENTS THE TREE 1. THE DATA STRUCTURE REPRESENTS THE TREE
• 1.2. Represent the tree by list of children • 1.2. Represent the tree by list of children
Each node of the tree stores a list of its children. 1 1 2 3
There is an array of pointers to the beginning of the child list of nodes 1, 2, . . . , 10:
header[i] points to the child list of node i.
33 34
1. THE DATA STRUCTURE REPRESENTS THE TREE 1. THE DATA STRUCTURE REPRESENTS THE TREE
• 1.2. Represent the tree by list of children • 1.2. Represent the tree by list of children
Example: The following description can be used to represent trees Below is an illustration of the leftmost_child operation settings. The
typedef ? NodeT; /* sign ? needs to be replaced by a suitable type definition */ implementation of the remaining operations is considered an exercise.
typedef ? ListT; /* sign ? needs to be replaced by a suitable list type definition */
typedef ? position; NodeT leftmost_child (NodeT n, TreeT T)
typedef struct /* return the leftmost child of node n in tree T */
{ {
ListT header[maxNodes]; ListT L; /* list of child of node n */
labeltype labels[maxNodes];
L = T.header[n];
NodeT root;
if (empty(L)) /* n is leaf */
} TreeT;
return(0);
Assume that the root of the tree is stored in the root field and 0 to represent an empty else return(retrive ( first(L), L));
node. }
35 36
1. THE DATA STRUCTURES REPRESENTS THE TREE 1. THE DATA STRUCTURE REPRESENTS THE TREE
• 1.3. Represent the tree with the leftmost child and the right sibling • 1.3. Represent the tree with the leftmost child and the right sibling
• 1.3. Represent the tree with the leftmost child and the right sibling
1. The data structure represents a tree
Comment:
With this representation, basic operations are easy to implement. 2. Tree traversal
Only the parent operation requires traversing the list, so it is inefficient. 2.1. Preorder
• In cases where this operation must be used frequently, add another field to the 2.2. Postorder
record to store the node's parent.
2.3. Inorder
3. Binary tree
39 40
2. TREE TRAVERSAL 2. TREE TRAVERSAL
If tree T is empty, then the empty list is the preorder, postorder, and T1 T2 Tk
h i
• The following are nodes of T2, . . . , Tk, each group is in InOrder.
j
45 46
3.1. Representation of binary tree Consider a complete binary tree T with n nodes, where each node contains a value.
3.2. Tree traversal Assign lables to the nodes of the complete binary tree T from top to bottom and from left
to right using the numbers 1, 2,..., n.
Let tree T correspond to array A in which the ith element of A is the value stored in the ith
node of tree T, i = 1, 2, ..., n.
49 50
F G D F G D
struct Tnode {
DataType Item; // DataType –data type of node on the tree H
H
I
struct Tnode *left; I
struct Tnode *right; J
}; J
K K
typedef struct Tnode treeNode; Binary tree is represented by pointers
Binary tree
53 54
61
• 1.1. Definition
1. Definitions of height and depth of the node
The height of a node in the tree is equal to the length of the longest path
1.1. Definition
from that node to the leaf plus 1.
1.2 Example
The height of a tree is the height of the root.
2. Algorithm to find the height and depth
The depth/level of a node is equal to 1 plus the length of the unique path
from the root to it.
63 64
1. DEFINITION OF THE HEIGHT AND DEPTH OF NODE 1. DEFINITION OF THE HEIGHT AND DEPTH OF NODE
depth=2 height h = 3
B C D depth=2 8 12 11 2 depth 3
height=2
height=1
h=2 6 5 1 depth 4
depth=3 E
depth=3 F
height=1 height=1
9 depth 5
height h=1 Height of the tree: 5
65 66
Tree representation
1. Definitions of height and depth of the node
struct Node{
2. Algorithm to find the height and depth
int id;
2.1 Height of the node Node* leftMostChild;
Node* rightSibling;
2.2 Depth of the node
};
Node* root;
67 68
2. ALGORITHM TO FIND THE HEIGHT AND DEPTH 2. ALGORITHM TO FIND THE HEIGHT AND DEPTH
• 2.1. Find the height of a node • 2.1. Find the height of a node
Height of a node Height of a node
7 int height(Node* p){ 7 int height(Node* p){
if(p == NULL) return 0; if(p == NULL) return 0;
int maxh = 0; int maxh = 0;
Node* q = p->leftMostChild; Node* q = p->leftMostChild;
h =? 3 10 4 h =? 3 10 4
while(q != NULL){ while(q != NULL){
int h = height(q); int h = height(q);
if(h > maxh) maxh = h; if(h > maxh) maxh = h;
h=? 8 12 11 2 q = q->rightSibling; h=? 8 12 11 2 q = q->rightSibling;
} }
return maxh + 1; return maxh + 1;
} }
h=? 6 5 1 h=1 6 h=? 5 1
2. ALGORITHM TO FIND THE HEIGHT AND DEPTH 2. ALGORITHM TO FIND THE HEIGHT AND DEPTH
• 2.1. Find the height of a node • 2.1. Find the height of a node
Height of a node Height of a node
7 int height(Node* p){ 7 int height(Node* p){
if(p == NULL) return 0; if(p == NULL) return 0;
int maxh = 0; int maxh = 0;
Node* q = p->leftMostChild; Node* q = p->leftMostChild;
h =? 3 10 4 h =4
=? 3 10 4
while(q != NULL){ while(q != NULL){
int h = height(q); int h = height(q);
if(h > maxh) maxh = h; if(h > maxh) maxh = h;
h=3
h=? 8 h=? 12 11 2 q = q->rightSibling; h=3 8 h=2 12
h=? 11 2 q = q->rightSibling;
} }
return maxh + 1; return maxh + 1;
} }
h=1 6 h=2
h=? 5 h=? 1 h=1 6 h=2 5
h=? h=1 1
h=1 9 h=1 9
71 72
2. ALGORITHM TO FIND THE HEIGHT AND DEPTH
THANK YOU !
Node* p = r->leftMostChild;
while(p != NULL){
d =2 3 10 4 if(p->id == v) return d+1;
int dv = depth(p,v,d+1);
if(dv > 0) return dv;
d=3 8 d=3 12 11 2 p = p->rightSibling;
}
return -1;
}
d=4 6 d=4 5 1 d =?
=4
int find_depth(Node* r, int v){
return depth(r,v,1);
d=5 9 }
73 74