You are on page 1of 33

Algorithms and Data

Structures

Introduction to Trees

Dr. Yasir Faheem


Department of Computer Science
Islamabad campus

Lecture 10 With thanks to Dr. Salman Niazi


Overview
2

 Introduction to trees
 Trees as a Tool
 Understanding the terminology
 Linear Lists and Trees
 Hierarchical Data and Trees
 Applications of Trees
 Some more terminologies
Linear Lists And Trees
3
 Linear lists are useful for serially ordered data.
 (e0, e1, e2, …, en-1)
 Days of week
 Months in a year
 Students in this class
 Trees are useful for hierarchically ordered data
 Structure of directories
 Employees of a corporation
◼ President, vice presidents, managers, and so on
Linear vs Non-Linear Data
4
Structures
 Nature of data
 Cost of operations (insertion, searching, deletion)
 Memory usage

Linear Operations Non- Operations


data linear
structures
Array- • Searching [Binary Search O(log2n), Binary • Searching O(n)
based list linear search O(n) search • Insertion O(n)
• Insertion O(n) tree • Deletion O(n)
• Deletion O(n)
Avl-Trees • Searching O(log2n) (only linear search
possible)
Linked list • Searching O(n) (only linear search Insertion O(log2n)
possible) • Deletion O(log2n)
Insertion O(n)
• Deletion O(n) Heap, ???
red-black
Stack & Insertion O(1)
trees, B-
Queue Deletion O(1)
trees
Introduction to Trees
5

 Trees are very common in computer science


 They come in different forms
 They are used as data representation in many applications
 They appear frequently in several algorithmic solutions
 There are several different types of trees
 Binary Trees
 Binary Search Trees
 AVL Trees
Trees as tool for abstraction
6

 We often use trees in our everyday lives


 To keep track of our ancestors: a family tree
◼ Most of the terminology used in trees in computer science here
 When describing the organization of a company: a company chart
 Organization of files in a computer: a directory structure
 In here we want to concentrate on trees as tools for the
implementation of computer programs: trees as abstract
data structures
7
Understanding the terminology
 One thing about studying trees (and graphs for that
matter) is that there are several concepts that must be
learned. Here is just a few definitions
 A tree is a non-empty collection of vertices (or nodes) and
edges (or arcs) which carry some properties
 A vertex is an object that has a name or has some
information associated with it
 An edge is a connection between two vertices which can
also have some information associated with it
 A path is a sequence of adjacent of vertices connected by
edges
A tree
8
Vertices
9
Edges
10
A path
11
Trees, Paths and Forests
12

 The main property of a tree is based on paths


In a tree there is exactly one path between any two nodes
 If there exist more than one path between any pair of nodes
or if there is no path between any of them we do not have a
tree
 A disjoint set of trees is called a forest
Rooted Trees
13

 A rooted tree is a tree where one node is "special" and is


called the root of the tree
 A tree where there is no root is called a free tree
 Rooted trees are the most common in computer applications,
so common that we'll use the term tree as a synonym for
rooted tree
A rooted tree a free tree
Trees and subtrees
14

 In a rooted tree, any node is a root of a subtree consisting of itself and


the nodes "below" it
 By definition there is only one path between the root and each of the
other nodes.
 Because a root is also a node the main property applies

 The convention used in computer science is that roots are drawn on the
top. It may seem strange at the beginning but you'll get used to it

Root
root of
subtree

A subtree
More conventions
15

 Due to the fact that we organize trees with root at the top
(as shown in previous slide) we will often hear
 Node A is below node B
 Node C is above node D
 etc.
 In fact, the most common way to refer to nodes based on
another node is use the idea of a family tree
 Parent node
 Children
 Siblings
 etc
16
Another Important Characteristic
 Another characteristic of a tree which is that every node
(except the root) has only one node immediately above it
(one parent) but can have several immediately below
(children)

parent

node

children
Leaves
17

 Nodes that have no children are called leaves (or


terminal) nodes
 Node with at least one child are called non-terminal
(internal nodes)

Leaves
Trees with specific number of children
18

 It is possible that the order in which children are defined is


important. We call these trees ordered trees
 Sometimes a node must have a maximum number of children.
 If for the whole tree the maximum number of children nodes can
have is M and this tree is ordered we have a M-ary tree.
 A binary tree is a special case of a M-ary tree where all
nodes (except the leaves) have at most 2 children.
 Because the tree is ordered the children have a order and they are
normally referred to as left and right child
Degree of a Node
19

 We define the degree of a node in a tree as the number


of its child nodes.
A

B C D

E F G

 Node C has degree 0 ( leaf node)


 Node D has degree 1 , Node B has degree 2 ,
 Node A has degree 3.
Hierarchical Data And Trees
20

 The element at the top of the hierarchy is the root


 Elements next in the hierarchy are the children of the
root
 Elements next in the hierarchy are the grandchildren
of the root, and so on
 Elements at the lowest level of the hierarchy are the
leaves
Tree applications
21

 Expression evaluations (note how different


traversals result in different notation)
 Storing and retrieving information by a key
 Representing structured objects (e.g. the universe
in an adventure game)
 Useful when needing to make a decision on how
to proceed (tic-tac-toe, chess)
Jake’s Pizza Shop
22

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook


Joe Tim Max Ted Len Dale
A Tree Has a Root Node
23

ROOT NODE Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook


Joe Tim Max Ted Len Dale
Leaf nodes have no children
24

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook


Joe Tim Max Ted Len Dale

LEAF NODES
Sibling nodes have same parent
25

Owner
SIBLINGS

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook


Joe Tim Max Ted Len Dale
Tree Terminology
26

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook


Joe Tim Max Ted Len Dale

SIBLINGS
Tree Terminology
27

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook


Joe Tim Max Ted Len Dale

SIBLINGS
A Tree Has Depths
28

Depth #0
Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook


Joe Tim Max Ted Len Dale
Depth 1
29

Owner

Depth #1 Manager Chef

Delivery Delivery Waiter Waiter Cook Cook


Joe Tim Max Ted Len Dale
Depth 2
30

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook


Depth #2 Joe Tim Max Ted Len Dale
A Subtree
31

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook


Joe Tim Max Ted Len Dale

A SUBTREE OF ROOT NODE


Another Subtree
32

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook


Joe Tim Max Ted Len Dale

ANOTHER SUBTREE
OF ROOT NODE
33

The End

You might also like