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

Week 7 Trees

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

Week 7 Trees

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

Trees

→ A tree is a connected simple graph with no


simple cycles
→ In a tree there is
unique simple path between Kro
a
any
of its vertices

→ If we add an edge to a tree it creates a


cycle
→ if we remove an
edge from a tree it ,
becomes not
connected

Rooted Tree

→ A rooted tree is a tree in which one vertex has been

designated as the root .


we can change an un rooted tree into a rooted tree by choosing
vertex as the root
any
→ Two rooted trees are isomorphic if there is a
bijection
between their vertices that
↳ Takes the root to root
to non

edges
to
edges
and non
edges edges
→ if vertices u and connected
u
by an edge and u
are

is closer to the root than v , then


↳ u is called the parent of v and v is called a child ,

↳ of u
vertices with same parent are called siblings
→ a childless vertex is called a kat

vertices with at least one child are called internal

→ The ancestors of non root vertex ✓


a are the vertices in
the ( unique) simple path from the root v

→ The descendants of vertex rare vertices that have ✓ as its


ancestor .

→ The level (or depth) of a vertex v is the length of the


( unique) path from the root to v

e-
g level of root is 0

→ The
height of a rooted tree is the man level of its vertices

→ a rooted tree of height n is balanced if all its leaves


are of level n or n -
1

Subtrees

→ if v is a vertex in a rooted tree T the subtree with u


.

as its root is the subgraph of T consisting of v all its ,

descendants and all edges incident to the descendants


Special Trees
→ A rooted tree is called an m any tree -

if every internal
vertex has no more than m children g binary tree
e.

→ A rooted tree is called a full n-ary tree if every internal


vertex has
exactly m children

→ a
full binary tree with n internal vertices
contains 2n + 1 vertices in total
↳ internal vertex has 2 children so 2h vertices
every ,

plus root so In + I

→ a
full m -

any
tree with n internal vertices contains
man +1 vertices in total .

Search Trees
Binary
Linearly ordered list
sequence ( list) whose
→ : A

ordered / not the


elements are
linearly necessarily in
order of listing)

searching for items in linearly ordered lost is an
a

important task Binary trees are very useful for


,

this
↳ searching for data in
binary search tree
data

revising n u n u

↳ converting linearly ordered lists


to
binary search tree and
back
two
→ We are
given things
↳ a list L of items
↳ linear order 4 on them

search for

A
binary tree L and L is
binary
a tree
which every vertex is labelled
in an with item
from L such that :

1) The label of each vertex


↳ is <
greater than the labels of all vertices
- in its
left subtree
↳ is 4- less than the labels of all vertices in its
right subtree

2)
Every path in the tree is compatible with the order
of listing
e.
9 for the list ( 5,128,3 ,
2
, 15,4 ,
20 ) and linear order

5
/
3 728
2
/ I
15
-
go
How to build a
binary search tree from linearly ordered list
we list L and linear order them
given
→ are a a 4 on .

→ we
go through each member of the list left b- night
,

first item it the label of root of tree


:
assign
comparing : we take the next item on the list and first
it with the labels of old vertices
we
compare
already in the tree starting from root and
↳ moving to left if the new item is 4- less
than the label of the respective old vertex

,

if it has a left child


or
moving to right if new item is < greater -

than the label of


respective old vertex if it has
,

a
right child
Adding :

↳ When the new item is 3 -


less than the label
of old vertex and the vertex has no left child

[
an

then we insert a new left child to old vertex and


label it with new item
but the
same for 4
thing -

greater on
night
Tree Traversal

Rooted trees have traversal algorithms to access or add
data


Important traversal algorithms :

HiFi
f g
:
/ elk
j /
in
n Tip I

Preorder traversal
→ visit the root , then continue traversing subtrees in
preorder from left to right
g for tree above a b e , j K, N , O , P , F 9 , I m oh , i
e. : C d
, , , , ,
, ,

In order traversal

Begin traversing leftmost subtree in order then visit ,

root then continue


, traversing subtrees in in order from left ,

to
right .

e.
9 For tree above : j ,
e
,
n
,
K ,
O
, P, b
,
f- ,
a
,
c
, 1,9 ,
m
,
d,h i
,

Post order traversal



Begin traversing leftmost subtree in post order then continue ,

traversing subtrees in postorder from left to right finally visit


root
, ,

above j N O P K e it b C I m 9 h i d a
g for tree
e. :
, , ,, , , , , , , , , ,
,

You might also like