Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 52

Tree data structure

Introduction

• So far , we have discussed only linear data


structure e.g. Arrays , Stack and Queue , Lists
• Each have Pros and Cons
• No single data structure that is good for all
purposes
• Advantages and Disadvantage of Some
Linear data Structure
Array
Advantage :
Quick insertion, very fast access if index known

Disadvantage :

Slow search, slow deletion, fixed size.


Stack
Advantage:

Provides last-in, first-out access.

Disadvantage:

Slow access to other items.


Queue
Advantage :

Provides first-in, first-out access.

Disadvantage :

Slow access to other items.


Linked-List
• Advantage :

Quick insertion, quick deletion.

Disadvantage :

Slow search.
Introduction to Trees

• Now , we will discuss Non-Liner data structure


• Tree is a non-linear data structure
• Use mainly to represent hierarchical
relationship between elements e.g. records ,
family tree and table of contents
Introduction to Trees.
• Fundamental data storage structures used in
programming.
• Combines advantages of an ordered array and
a linked list.
• Searching as fast as in ordered array.
• Insertion and deletion as fast as in linked list.
Why trees ?

Faster than linear data


structures
Tree structures are important because they allow us to implement a host
of algorithms much faster than when using linear data structures, such
as list.
Definition.
• Definition : A tree is a finite set of one or more
nodes .

• There are different kinds of tree : Binary tree ,


2-Tree or extended binary tree and complete
binary tree.
Example of Tree
root
Sami’s Home Page

Teaching Research Activities

CS101 CS211 Papers Presentations


Cont.
• According to the definition of trees, a node
can have any number of children.

• A binary tree is restricted to only having 0, 1,


or 2 children
Applications of Tree

• Trees also provide a natural way to organize data in


many areas such as:
– File systems
– Graphical User Interfaces (GUI)
– Databases
– Web Sites
– and many other computer systems.
Tree Terminology
• Internal node : node that contains at least one child
• External node ( Leaf) : Sometimes called terminal
node and does not contain any child (node)
• Degree of node is the number of children it has
• Edge : Is the line from node to successor
• sub-tree: A portion of a tree which can be viewed as
a tree in itself, is called a sub-tree.
Tree Terminology.
• Path : Sequence of edges (Connection of two or
more edges)
• Level of a node : the distance ( in number of
nodes ) of a node form the root. The root always
lies at level 0
• Siblings/Brothers :Children of the same node are
called siblings of each other
• Depth of a tree : Depth of a tree is one more
then the maximum number of levels of a tree
Tree Terminology
• Parent: Any node, except root has exactly one
edge running upward to another node. The
node above it is called parent.
• Child: Any node may have one or more lines
running downward to other nodes. Nodes
below are children.
• Leaf: A node that has no children.
Tree Terminology
• Visiting: A node is visited when program
control arrives at the node, usually for
processing.
• Traversing: To traverse a tree means to visit all
the nodes in some specified order.
Tree Terminologies.
• Similar Trees : Binary trees are similar if they
have the same structure or shape .

• Tree are said to be copy if they are similar and


if they have the same contents at
corresponding nodes
Special Tree
• An empty tree has a height of zero.
• A single node tree is a tree of height 1.
– This is the only case where a node is both a root
and a leaf.
Ordered Tree
• A tree is ordered if there is a linear ordering defined
for the children of each node;
• That’s, we can identify the children of a node as
being the first, second, third, and so on.
• Such an ordering is usually shown by arranging
siblings left to right, according to their ordering.
• Ordered trees typically indicate the linear order
among siblings by listing them in the correct order.
• A famous example of ordered trees is the family tree.
Binary Tree
• As discussed an ordered tree is one in which
the children of the each node are ordered

• Binary Tree : It is an ordered tree with all node


having at most two children
Binary Tree
• A binary tree T is defined as a finite set of
elements , called nodes , such that :

(a) T is empty (Called the null tree or empty


tree).
(b) T contains a distinguish node R , called Root
and the remaining nodes form an ordered
pair of disjoint binary trees T1 and T2 .
Binary tree.
• A binary tree (very common!) allows each
node to have at most two children.
Complete Binary Tree
 A tree is said to be complete if all its level , except
possibly the last , have the maximum number of
possible nodes , and if all nodes at the last level
appear as far left as possible. OR

 A complete binary tree is one where all the levels


are full with exception to the last level and it is
filled from left to right
Extended Binary Trees : 2-Trees
• A binary Tree T is said to be a 2-Tree or an
extended binary tree if each node N has either 0
or 2 children.
• Node with 0 Children are called external node
and are represented by square
• Node with 2 children are called internal node
and are represented with circle
• Any binary tree can be change to extended
binary tree
Full Binary Tree
• A full binary tree is a binary tree in which
every node in the tree has 2 children except
the leaves of the tree . OR
• A full binary tree is a binary tree in which
every node in the tree has exactly zero or 2
children.
Full Binary Tree theorem
• Theorem : Let T be a non-empty full binary
tree then ;
 If T has I internal nodes , the number of
leaves is L = I +1
 If T has I internal node , the total number of
nodes is N = 2I + 1
If T has a total of N nodes , the number of
internal nodes is I = (N-1)/2
Full Binary Tree theorem
If T has a total of N nodes , the number of
leaves is L = (N+1)/2
If T has L leaves , the total number of nodes is
N = 2L-1
If T has L leaves , the number of internal
nodes is I = L - 1
Application of binary trees
• Some of the applications of binary trees are

(1) Decision Tree


(2) Arithmetic Expression or Expression Tree
Decision Tree
 Binary tree associated with a decision process
• internal nodes: questions with yes/no answer
• external nodes: decisions
 Example: dining decision

Want a fast meal?

Yes No

How about coffee? On expense account?

Yes No Yes No
Starbucks Spike’s Al Forno Café Paragon

Trees
Arithematic Expression
• Binary Trees are used to represent arithemetic
expressions

• Internal node : Operators


• External Node : Operands
Example
• Consider algebraic expression E below ;

E =2*(a - 1)+(3*b)
This expression can be represented in binary
Tree and every algebraic expression will
correspond to a unique tree and vice versa .
Cont.
• Below is the Corresponding Tree

 

2 - 3 b

a 1
Expression tree

You can generate a formula of expression


called postfix, prefix, infix expressions.

 Inorder: 3 * 7 + 4 ^ 2 (infix form)


 Preorder: + * 3 7 ^ 4 2 (prefix form)
 Postorder: 3 7 * 4 2 ^ + (postfix form)
Tree traversal
• Three ways to traverse a tree

(1) Pre-order
(2) In-order
(3) Post-order
Conti
(a) PreOrder : Node-Left-Right (NLF)

1 . Process the root R


2 . Traverse the left subtree of R in preorder
3 . Traverse the right subtree of R in preorder
Cont.
(a) InOrder : Left-Node-Right(LNR)

1 . Traverse the left subtree of R in inorder


2 . Procces the root R
3 . Traverse the right subtree of R in inorder
Cont.
(a) Post-order : Left-Node-Right(LNR)

1 . Traverse the left subtree of R in postorder


2 . Traverse the right subtree of R in postorder
3 . Procces the root R
Example
• Pre – Brown, Truman, Taft, Ralson, Davidson, Rollins, Zuniga
• Post – Davidson, Rollins, Ralson, Taft, Zuniga, Truman, Brown
root
Brown

Truman

Taft Zuniga

Ralson

Davidson Rollins
Another Example

• Pre-order : ..?
• Post-order : ?

root
Rollins

Truman
Davidson

Taft Zuniga
Brown Ralson
In-order Traversal

• Traverse left subtree, visit node, traverse right


subtree
– Brown, Davidson, Ralson, Rollins, Taft, Truman,
Zuniga
root
Rollins

Truman
Davidson

Taft Zuniga
Brown Ralson
Another Example
• In-order – Brown, Davidson, Ralson, Rollins, Taft, Truman,
Zuniga

root
Brown

Truman

Taft Zuniga

Ralson

Davidson Rollins
Binary Tree Representation

1. Linked Structure
2. Array List
Linked representation
• Uses three (3) parallel arrays INFO ,LEFT and
RIGHT
• Uses two pointers ROOT and AVAIL
• The ROOT contains the location of the ROOT
of tree
• AVAIL contains address of any invalid address
and denoted by NULL
Cont.
• Example 1 : a tree with node A , B ,C ,D , E
,F ,G ,H , J, K
• Example 2 : Given the tree table , draw the
corresponding binary tree.
Sequential representation
• If the tree is complete or nearly complete ,
then sequential representation is an efficient
way of maintaining T in memory.
• It uses Linear Array TREE as follows:
1. Root is sorted in TREE [1]
2. If a node occupies TREE [K] , then its left child is
stored in TREE[2*K] and its right child is stored in
TREE [2 * K +1]
Null is used to indicate empty subtree
Sequential representation
• Example : A tree with nodes as follows 45 ,
22 , 77 , 11 , 30 , 90, 15 ,25, 85 .
• Represent the above tree in a memory using
sequential representation
Cont.
• The example shows that we require 14 locations
in the array TREE , even though , the tree has
only 9 nodes
• If we included the null entries for the successor
of the terminal nodes , then we would actually
require TREE [29] for the right successor of the
TREE [14]
• Generally , sequential representation of a tree
with depth d requires array with 2d+1
Cont.
• It is inefficient to use sequential
representation , unless if T is complete or
nearly complete
THANK YOU

You might also like