0% found this document useful (0 votes)
8 views22 pages

10 DSA Trees

Uploaded by

seharamjadnuml
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views22 pages

10 DSA Trees

Uploaded by

seharamjadnuml
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

DATA STRUCTURES

Trees
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
Terminology: Parent Child Relations
 All nodes 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 of: I
Terminology: Degree
 The degree of a node is defined as the number of its children
 deg(I)=3
 Nodes with the same parent are siblings
 J, K, and L are siblings
Terminology: Leaf And Internal Nodes
 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 Examples
Terminology: Internal Nodes Example
Terminology: Path
 A path is a sequence of nodes (a 0 , a 1 , . . . , an)
 Where ak + 1 is a child of ak

 The length of this path is: n = |nodes i n the p a t h | - 1


 For example, the path (B, E, G) has length 2
Terminology: Path Example
 Paths of length 10 (11 nodes) and 4 (5 nodes)

Start of these
paths

End of these
paths
Terminology: Depth (or Level)
 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: Depth Example
• Nodes of depth up to
17
0

14

1
7
Terminology: Height
 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: Height Example
• Height of this tree is
17

1
7
Terminology: Ancestors And Descendants
 If a path exists from node a to node b
 a is an ancestor of b
 b is a descendent of a

 The root node is an ancestor of all nodes


Ancestors And Descendants Example
 The descendants of node B are C, D, E, F,
and G

 The ancestors of node I are H and A


Descendants Example
 All descendants (including itself) of the indicated node
Ancestors Example
 All ancestors (including itself) of the indicated node
Terminology: Subtree
 Another approach to a tree is to define the tree recursively
 A degree-0 node is a tree

 A node with degree n is a tree if it has n children


 All of its children are disjoint trees (i.e., with no intersecting
nodes)

 Given any node a within a tree


with root r, the collection of a and
all of its descendants is said to be a
subtree of the tree with root a
Tree Properties
Property Valu
Number of e
A
nodes

B C Height

Root Node
D E F
Leaves

Ancestors of
G
H

H I Descendants of
B
Example: HTML
 HTML document has a tree structure

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

<p>This i s a paragraph wit h some


<u>underlined</u> t e x t . < / p >
</body>
</html>
Example: HTML
 HTML document has a tree structure

<html> titl
<head e
> <title>Hello World!</title> headin
</head> g
<body>
<h1>This i s a <u>Heading</u></h1>
body of
page <p>This i s a paragraph wit h some
<u>underlined</u> t e x t . < / p >
</body>
</html>

paragra
underlini ph
ng
Example: HTML
 The nested tags define a tree rooted at the HTML tag
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>This i s a <u>Heading</u></h1>

<p>This i s a paragraph w i t h some


<u>underlined</u> t e x t . < / p >
</body>
</html>

You might also like