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

Tree

The lecture notes cover the fundamentals of trees as a data structure, including definitions, types of nodes, and properties such as height and depth. It also discusses binary trees, their representations, and various traversal methods including breadth-first and depth-first traversals. Additionally, examples of full and complete binary trees are provided to illustrate these concepts.

Uploaded by

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

Tree

The lecture notes cover the fundamentals of trees as a data structure, including definitions, types of nodes, and properties such as height and depth. It also discusses binary trees, their representations, and various traversal methods including breadth-first and depth-first traversals. Additionally, examples of full and complete binary trees are provided to illustrate these concepts.

Uploaded by

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

Data Structures and Algorithms

Lecture notes: Trees

Lecturer: Michel Toulouse

Hanoi University of Science and Technology


[email protected]

31 mai 2021
Outline

Tree definitions

Tree declaration in C

Binary tree

Binary trees traversal


What is a tree

I Here a tree is a data structure (like


arrays, stacks, queues), but more
generally a tree is a form of graph,
so trees have nodes and edges
I Nodes in a tree, like nodes in a
queue, have a data field and have
pointer field(s).
What is a tree

I Like in queues, nodes are connected


by pointers to form a sequence
(called levels in a tree), usually the
beginning of the sequence is the
node at the highest level in the tree
I Unlike queues, nodes in a tree can
branch out in several directions from
level i to level i + 1
Not a tree

I In a queue, the pointer to the first


node in the sequence was called the
”head”, for a tree it will be named
the root
I A tree has a unique path from the
root to every other node
I Thus the graph on the right is not a
tree as there exists two paths from
the root A to the leaf D
Types of nodes in a tree

I The first node in a tree is also called


the root of the tree
I The nodes at the end of a branch in
the tree are called leaves
I The other nodes are named internal
nodes
Tree : parent, child, siblings

I A node y at level i connected to a node


x at level i − 1 is said to be the child of
node x and x is the parent of node y
I The root node has no parent
I Leave nodes have no child
I The child nodes w , y , z of a same
parent node x are said to be siblings
with respect to each other
Tree : ancestors, height, depth

I The ancestors of a node u are all the


nodes on the path from u to the root
I According to CLRS textbook, u is his
own ancestor
I The root has only itself as ancestor
I The height of a node u in a tree is the
length of the longest path from u to
any leaf
I The height of a leaf is 0
I The height of u in the tree is 2
I The height of a tree is the height of its
root
I The depth of tree is the number of
levels in the tree, the present tree has
depth 5
I The depth of a node is the level of that
node in the tree. The depth of the root
is 0, the depth of node u in the tree is 3
Tree : descendants and subtrees

I The descendants of a node u are the


nodes on all the paths from node u to
leaves
I According to CLRS textbook, u is his
own descendant
I The descendants of a node u form of
subtree rooted at node u
I If n is the number of nodes in a tree,
there are n − 1 subtrees
I A subtree where the root is a leave has
only itself as descendant
I The degree of a node u is the number
children of u
I The degree of a tree is equal to the
largest degree of its nodes
Node declaration in C
Nodes in a tree are declared in the same way as for nodes of a queue. Nodes have
two pointers, one for the leftmost child and one for the right-sibling of the leftmost
child

Data
Leftmost Child Right-sibling

typedef struct
{
int data; // data of each node
struct treeNode * leftmost_child;
struct treeNode * right_sibling;
}treeNode;
treeNode * Root;
Tree example

A Data
Leftmost Child Right-sibling

B C D

E F G

A
H I J K

B C D

E F G

H I J K
Tree second example

1 Data
Leftmost Child Right-sibling

2 1

2
3 5

4 11 7
3 5

6 4
11 7
8
6
-2 9

-2 9
Binary trees
A binary tree is a tree such that
I every node has at most 2 children
I each node is labeled as being either a left child or a right child

Right child of a
Left child of a

Data
left right
pointer pointer
Full versus complete binary trees

• Full binary tree: a binary tree in which 4


• every parent has 2 children,
1 3
• every leaf has equal depth
2 16 9 10

Full binary tree


• Complete binary tree: a binary tree in which
• every level is full except possibly the deepest level
• if the deepest level isn’t full, leaf nodes are as far to the left as possible

Complete binary trees


Examples

Complete binary tree Full and complete Complete

Neither full nor complete Full and complete Neither full nor complete
Height of binary tree

I The maximum height of a


binary tree with n nodes is the
same as the length of a link
list with n nodes, i.e. n
I The minimum height of a
binary tree with n nodes is
dlog(n + 1)e − 1
I Full and complete binary trees
have minimum height
Binary tree representation
Binary trees are represented using pointers in a similar ways as ordinary trees :
I Each node contains the address of the left child and the right child
I If any node has its left or right child empty then it will have in its respective
pointer a null value
I A leaf has null value in both of its pointers

typedef struct
{
DataType data; /*data of node; DataType: int, char, double..*/
struct node *left ; /* points to the left child */
struct node *right; /* points to the right child */
}node;

Point to left child Point to right child


data
Example

B A

E C
B C D
F G D

H E F G

J
H I J K
K
Binary tree traversal
A traversal is a systematic way to visit all nodes of a graph, a binary tree in the
present case
There are two very common traversals :
I Breadth First
I Depth First
Breadth First : In a breadth first traversal all of the nodes on a given level are
visited and then all of the nodes on the next level are visited. Usually in a left to
right fashion

On the tree below Breadth − first − search(2) visits the nodes in this order : 2, 4, 5,
7, 3, 10, 8, 1, 9, 11, 6

4 5

7 3 10 8
1 9
11

6
Depth first traversals

In a depth first traversal all the nodes of a subtree are visited prior to
visit another subtree
There are three common depth first traversals
I Inorder
I Preorder
I Postorder
Inorder tree traversal
Traverse the left subtree ; Visit the root ; Traverse the right subtree
InorderTreeWalk(x)
if x 6= NIL
InorderTreeWalk(x.left) ;
print(x.key) ;
InorderTreeWalk(x.right) ;

Call: InorderTreeWalk(A);
E D G F B H A L C M J

left subtree right subtree


Preorder tree traversal
Visit the root ; Traverse the left subtree ; Traverse the right subtree
PreorderTreeWalk(x)
if x 6= NIL
print(x.key) ;
PreorderTreeWalk(x.left) ;
PreorderTreeWalk(x.right) ;

Call: PreorderTreeWalk(A);
A B D E F G H C L J M

left subtree right subtree


Postorder tree traversal
Traverse the left subtree ; Traverse the right subtree ; Visit the root
PostorderTreeWalk(x)
if x 6= NIL
PostorderTreeWalk(x.left) ;
PostorderTreeWalk(x.right) ;
print(x.key) ;

Call: PostorderTreeWalk(A);
E G F D H B L M J C A

left subtree right subtree

You might also like