You are on page 1of 24

DATA STRUCTURES

Lecture 8
TREES
• A data structure is said to be linear if its elements form a sequence or a linear
list. Previous linear data structures that we have studied like an array, stacks,
queues and linked lists organize data in linear order.
• A data structure is said to be nonlinear if its elements form a hierarchical
classification where, data items appear at various levels.
• Trees and Graphs are widely used non-linear data structures.
• Graphs are nothing but trees with certain restrictions removed.
TREES
TREES
• Tree and graph structures represents hierarchical
relationship that organizes data elements called nodes
by connecting them using links called edges.

• A tree consists of:


✓ A node called Root.
✓ zero or more nonempty subtrees T1, T2, ...., Tk, each of whose subtrees are
connected by an edge from Root.
TREE APPLICATIONS

a *

+ d

b c

Folders and files Expression parse tree


TREE APPLICATIONS

Binary Search Tree Decision trees


TREE TERMINOLOGIES
• The root, is at the top of the hierarchy.
• Each node can have at most one link coming into it.
• The node where the link originates is called the parent node.
• The root node has no parent.
• The links leaving a node point to child nodes.
• Trees are recursive structures. Each child node is itself the root of a subtree.
• At the bottom of the tree are leaf nodes, which have no children.
• The internal node is a node which is neither a root nor a leaf
• Sibling : a node with a common parent node.
TREE TERMINOLOGIES
• Check the following tree:

• (a) is the root,


• (a) has no parents
• (a) is the parent of (b) and (c)
• (b) and (c) are the children of (a)
• (b) is the parent of (d)
• (d) is the child of (b)
• (c) is the parent of (e) and (f)
• (e) and (f) are the children of (c)
• (b) and (c) are internal nodes
• (d), (e) and (f) are leaf nodes
• (e) and (f) are siblings
TREE TERMINOLOGIES
• Path : a sequence of edges.
Path from A to O is : A - C - G - O
• Size : The number of nodes in a tree.
• Subtree : a smaller tree of nodes,
which is one of the current node children.
TREE TERMINOLOGIES
• Height : The number of edges on the longest path from the node to a leaf.
➢ The height of this tree is 3.
➢ The height of (B) is 2.

• Depth : The number of edges from the node to the tree’s root node.
➢ Node C has a depth of 1.

• Degree : The maximum number of subtrees.


TREE TERMINOLOGIES
•Level :
➢The level of the node refers to its distance from the root.
➢The root of the tree has level 0, and the level of any other node in the tree is one more
than the level of its parent.
➢Node F is at level 2 and node H is at level 3.
➢The maximum number of nodes at any level is 2^n.
BINARY TREES
• In general, tree nodes can have any number of children.
• In a binary tree, each node can have at most two children and one parent.
• A binary tree is either empty or consists of a node called the root together
with two binary trees called the left subtree and the right subtree.
• A tree with no nodes is called as a null tree.
BINARY TREE EXAMPLES
BINARY TREE TRAVERSALS

• Traversal: An examination of the elements of a tree.

• Common orderings for traversals:


– pre-order: process root node, then its left/right subtrees (VLR)
– in-order: process left subtree, then root node, then right (LVR)
– post-order: process left/right subtrees, then root node (LRV)
TRAVERSAL EXAMPLE
Root

• in-order (LVR):
17
41
6
41 9
17
81
9
6 81
TRAVERSAL EXAMPLE
Root

• pre-order (VLR): 17
17
41 41 9
6
9
6 81
81
TRAVERSAL EXAMPLE
Root
• post-order (LRV):
6 17
41
81 41 9
9
17 6 81
EXERCISE 1
– Pre-order: Root
42 15 27 48 9 86 12 5 3 39
42

– In-order:
15 9
15 48 27 42 86 5 12 9 3 39
27 86 3
– Post-order:
48 27 15 5 12 86 39 3 9 42 48 12 39

5
EXERCISE 2
EXAMPLE: EXPRESSION TREES

• It is a binary tree contains an arithmetic


expression with some operators and operands.

• Leaves are operands (constants or variables) *


a b
• The internal nodes contain operators
a* b
• For each node contains an operator, its left subtree
gives the left operand, and its right subtree gives
the right operand.
EXAMPLE: EXPRESSION TREES

• Building Expression Trees has great importance in syntactical analysis


and parsing, along with the validity of expressions
EXAMPLE: EXPRESSION TREES
(d * e + f ) *g -
b
-b

+
* +
a
a b
a++
a* b
EXAMPLE: C TREES
TREE IMPLEMENTATION
root

left A right

left B right left C right

left D right left E right left F right

left G right left H right

Tree Node Model

You might also like