Lecture 6 Tree Mod.pptx
Lecture 6 Tree Mod.pptx
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 ◦
14
17
Terminology
The height of a tree is defined as the maximum depth of any node within the tree
17
:Applications
Organization charts ◦
File systems ◦
Programming environments ◦
HTML pages ◦
Computers”R”U
s
body of page
paragraph
p>This is a paragraph with some<
>u>underlined</u> text.</p<
>body/<
>html/<
4.1.3
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
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
2 - 3 b
a 1
Expression Tree
Consider the following expression:
a x (b + c) - (d - (e + g / h))
* -
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