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

Lecture 6 Tree Mod.pptx

Uploaded by

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

Lecture 6 Tree Mod.pptx

Uploaded by

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

Data Structures

and Algorithms
PREPARED BY
PROF. DR . AMANY SARHAN
2023
Tree ADT
Nature View of a Tree
leaves

branches
root
Computer Scientist’s
View
root

leaves

branches
nodes
Why do we need tree
?DTS
Linked lists work well for representing linear relationships, but not all relationships
are linear.
In this lecture, we look specifically at the problem of representing rooted trees by
linked data structures.
We first look at binary trees, and then we present a method for rooted trees in which
nodes can have an arbitrary number of children.
We represent each node of a tree by an object.
As with linked lists, we assume that each node contains a key attribute.
The remaining attributes of interest are pointers to other nodes, and they vary
according to the type of tree.
Trees
A rooted tree data structure stores
information in nodes
:Similar to linked lists ◦
There is a first node, or root ◦
Each node has variable number of references to successors ◦
Each node, other than the root, has exactly one node pointing to it ◦
:Terminologies
:Terminologies
Child
.Each root ri of subtree Ti of tree T is called a child of r !
Parent
The root node r of tree T is the parent of all the roots r !
.i of the subtrees Ti, 1 < i < n
:Terminologies
Degree
.Refers to the number of subtrees associated with that node !
Leaf
A node of degree zero, i.e, no subtree(s) !
Height of a node
.The height of a node in a tree T is the length of the longest path from node to the leaf !
:Terminologies
Path and Path Length
Given a tree T containing the set of nodes R, a path in T is !
defined as a non-empty sequence of nodes
}P = (r1, r2, …, rk
The length of path P is k-1 !
Terminology
All nodes will have zero or more child nodes or
children
I has three children: J, K and L ◦

For all nodes other than the root node, there is


one parent node
H is the parent I ◦
Terminology
The degree of a node is defined as the
deg(I) = 3number of its children:

Nodes with the same parent are siblings


J, K, and L are siblings ◦
Terminology
Phylogenetic trees have nodes with degree 2 or 0:
Terminology
Nodes with degree zero are
also called leaf nodes

All other nodes are said to be


internal nodes, that is, they
are internal to the tree
Terminology
Leaf nodes:
Terminology
Internal nodes:
Terminology
These trees are equal if the order of the children is ignored
unordered trees ◦

They are different if order is relevant (ordered trees)


We will usually examine ordered trees (linear orders) ◦
In a hierarchical ordering, order is not relevant ◦
Terminology
The shape of a rooted tree gives a natural flow from
the root node, or just root
Terminology
A path is a sequence of nodes
)a0, a1, ..., an(
where ak + 1 is a child of ak is

The length of this path is n

E.g., the path (B, E, G) has length 2


Paths of length 10 (11 nodes) and 4 (5 nodes)

Start of these paths

End of these paths


Terminology
For each node in a tree, there exists a unique path from the
root node to that node

,.The length of this path is the depth of the node, e.g


E has depth 2 ◦
L has depth 3 ◦
Terminology
0 Nodes of depth up to 17

14

17
Terminology
The height of a tree is defined as the maximum depth of any node within the tree

The height of a tree with one node is 0


Just the root node ◦

For convenience, we define the height of the empty tree to be –1


Terminology
The height of this tree is 17

17
:Applications
Organization charts ◦
File systems ◦
Programming environments ◦
HTML pages ◦
Computers”R”U
s

Sale Manufacturin R&


s g D

U Internationa Laptop Desktop


S l s s

Europ Asi Canad


e a a
Tree Structure
Examples
It follows from the definition that a minimal tree comprised of a single root node
Ex: T1 = {A} !
If there is more than one node, the remaining
.nodes are partitioned into subtrees
Ex: T2 = {B, {C}} !
:Tree examples
:Example
:Sample Tree
Example: XHTML and CSS
Consider the following XHTML document
>html<
>head<
>title>Hello World!</title<
>head/<
>body<
>h1>This is a <u>Heading</u></h1<
p>This is a paragraph with some<
>u>underlined</u> text.</p<
>body/<
Example: XHTML and CSS

title Consider the following XHTML document


>html<
heading >head<
>title>Hello World!</title<
>head/<
>body<
underlining
>h1>This is a <u>Heading</u></h1<

body of page
paragraph
p>This is a paragraph with some<
>u>underlined</u> text.</p<
>body/<
>html/<
4.1.3

Example: XHTML and CSS

The nested tags define a tree rooted at the HTML tag


>html<
>head<
>title>Hello World!</title<
>head/<
>body<
>h1>This is a <u>Heading</u></h1<

p>This is a paragraph with some<


>u>underlined</u> text.</p<
>body/<
>html/<
Web browsers render this
tree as a web page
Example: XHTML and CSS
XML tags <tag>...</tag> must be nested

:For example, to get the following effect


987654321
you may use
>u>1 2 3 <b>4 5 6</b></u><b> 7 8 9</b<

:You may not use


>u>1 2 3 <b>4 5 6</u> 7 8 9</b<
Example: XHTML and CSS
Cascading Style Sheets (CSS) make use of this tree
structure to describe how HTML should be displayed
:For example ◦
>"style type="text/css<
h1 { color:blue; }
>style/<
indicates all text/decorations descendant from an h1 header
should be blue
:For example, this style renders as follows
>"style type="text/css<
h1 { color:blue; }
>style/<
:For example, this style renders as follows
>"style type="text/css<
h1 { color:blue; }
u { color:red; }
>style/<
Binary Tree
Binary Tree
 Applications:
A binary tree is a tree with the following  arithmetic expressions
:properties  decision processes
Each internal node has at most two ◦  searching
children (degree of two)
A
The children of a node are an ordered pair ◦

B C
We call the children of an internal node left
child and right child
The root of the entire tree T is pointed to by D E F G
. the attribute T: root
.If T: root D NIL, then the tree is empty H I
Binary Tree
 Applications:
If node x has no left child, then x: left D NIL,  arithmetic expressions
.and similarly for the right child  decision processes
 searching
Alternative recursive definition: a binary tree
is either A

a tree consisting of a single node, OR ◦


a tree whose root has an ordered ◦ B C
pair of children, each of which is a
binary tree
D E F G

H I
Inserting the integers 5, 2, 8, 4, and 1 into
.a binary search tree

Add 2
Add 8
Add 4

Add 1
Binary Tree
Draw the binary tree that contains the following Numbers
56, 81, 22, 30, 77, 10, 92
Decision Tree
Binary tree associated with a decision process
internal nodes: questions with yes/no answer ◦
external nodes: decisions ◦
Example: dining
Want a fast decision
meal?
Yes No
How about On expense
coffee? account?
Yes No Yes No

Starbucks Spike’s Al Forno Café Paragon


Arithmetic Expression
Tree
Binary tree associated with an arithmetic expression
internal nodes: operators ◦
external nodes: operands ◦
Example: arithmetic expression tree for the expression
+ ) (a - 1) + (3  b) 2(
 

2 - 3 b

a 1
Expression Tree
Consider the following expression:

a x (b + c) - (d - (e + g / h))

a) Draw the expression tree of this expression


Expression Tree
-

* -

a + d +

b c e /

g h
a x (b + c) - (d - (e + g / h))
Abstract Trees: Design
We implement an abstract tree or hierarchy by using a
:class that
Stores a value ◦
Stores the children in a linked-list ◦
Implementation
.In python, we consider a tree as list of sublists
There are many libraries to deal with trees: to create, add
,new branches, add values to branches, get values of nodes
.…
:Example

:def BinaryTree(r)
return [r, [ ], [ ]] // root and two branches (i.e. sublists)
--- ---
first second
child child
Implementation
:Create the tree in figure
mytree = ['a', #root
b', #left subtree'[
d has no children # ,]] [ ,] [ ,'d'[
e has no children # ,] ]] [ ,] [ ,'e'[
c', #right subtree'[
f has no children # , ]] [ ,] [ ,'f'[
no second child for c # ]][
]
Implementation
Rooted trees with
unbounded branching
It’s simple to extend the scheme for representing a binary tree to
any class of trees in which the number of children of each node is at
most some constant k
Replace the left and right attributes by child 1 ; child 2 ; : : : ; child
k.
This scheme no longer works when the number of children of a
node is unbounded, however, since we do not know how many
attributes to allocate in advance.
Moreover, if k, the number of children, is bounded by a large
constant but most nodes have a small number of children, we may
waste a lot of memory.
Rooted trees with
unbounded branching
As before, each tree contains a parent pointer p, and T: root
points to the root of tree T .
Instead of having a pointer to each of its children, however,
each node x has only two pointers:
1. x: left-child points to the leftmost child of node x , and
2. x: right-sibling points to the sibling of x immediately to its
right.
If node x has no children, then x: left-child D NIL, and if node
x is the rightmost child of its parent, then x: right-sibling D NIL.
Rooted trees with
unbounded branching

You might also like