0% found this document useful (0 votes)
13 views14 pages

lec7data

Uploaded by

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

lec7data

Uploaded by

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

Data Structures – Seventh Lecture

2- Non Linear Data Structures


1. Tree
2. Graph
3. Network
1. Tree
A Tree: is a branching structure such as following figure:

Root node A
Subtree

B C D

E F G H I

J K
Leaves nodes Leaves nodes

 The point at which lines come together in a tree called “nodes”.


 The top most node at the tree is called a “root” and the bottom most
node is called a “leaf”. The lines connecting the nodes are called
“branches”.
 ;A node is said to be the “parent” of these bellow it immediately which
are said to be “children”, children at the same parent called “brothers”
or “siblings” or “twins”.
 Any node of the tree is itself a tree, which is said to be “subtree” at the
origin tree.
 The entire tree is subtree of itself.
 Each node is a subtree consisting of only a single node.
Data Structures 2024-2025

 Tree can be represented in a computer memory by linked structure that


corresponds directly to the diagram used to represent trees on printed
page.
 We say that a node is visited when that node is processed. A visit to
every node in a tree is said to be “traversed”.
 The direction from the node to the leaf is down and the opposite
direction is up. Coming from the leaves to the root called “climbing”
the tree while going from the root to the leaf is called “descending” the
tree.
 “Ancestor”, A node's parent is its first ancestor, the parent of the parent
is the next ancestor, and so on. The root is an ancestor of each other
node.
 The “level” of a node refers to its distance from the root.
 Maximum number of levels is called the “depth” of tree .
 The “degree” of the node is the number of children that branched from
it.
 The “degree” of the tree is the maximum degree of nodes.

Binary Tree

A binary tree is a finite set of "nodes". The set might be empty (no nodes,
which is called empty tree). But if the set is not empty, it follows these rules:
1. There is one special node called the root.
2. Each node may be associated with up to two other different nodes,
called its “left child” and its “right child”. If a node c is the child of
another node p, then we say that "p is c's parent".
3. Each node, except the root, has exactly one parent; the root has no
parent.

Prepared by Dr. Dunia H. Hameed Page 47


Data Structures 2024-2025

Sub trees

Binary Search Tree


Binary Search Tree: is a binary tree, in which left child (if any) of any node
contains a smaller value than does the parent node and the right child (if any)
contains a larger value than does the parent node.

Types of Trees
1- Full Binary Tree: is a binary tree in which all of the leaves are on the same
level and every non leaf node has two children. The basic shape of a full binary
tree is triangular.

2- Complete Binary Tree: is the binary tree that is either full or full through
the next to the last level, which leaves on the last level as far left as possible.
The shape of complete binary tree is either triangular (if tree is full) or
something like the following:

Prepared by Dr. Dunia H. Hameed Page 48


Data Structures 2024-2025

3- Heap Tree: is a data structure that satisfies two properties one concerning
it’s shape: A heap tree must be a complete tree, and the other concerning the
order of its elements: For every node in the heap (Max Heap), the value stored
in that node is greater than or equal to the value in each of its children. The
root node will always contains the largest value in the heap and thus we always
know where the maximum value is in the root.
4- Strictly Binary Tree: is a binary tree in which each node except the leaf
has two children. A strictly binary tree which has (n) leaves always contains
(2*n-1) nodes.
5- Balanced Binary Tree: In a binary tree, each node has a factor called
“balance factor”. Balance factor of a node is the height of the left subtree
minus the height of the right subtree. If each node in the binary tree has a
balance factor equal to -1 or 0 or 1 then this binary tree is called “balanced”.

Converting Tree into Binary Tree


To convert tree into binary tree, we follow these steps:
1- We take all nodes on the left and connecting them vertically.
2- We take all nodes on the right and connecting them horizontally.
3- We must keep on brother’s level. A
A

B
B C
D C

D M N K L
V M K

V W S
W N S L

Tree Binary Tree

Prepared by Dr. Dunia H. Hameed Page 49


Data Structures 2024-2025

Application of binary tree


1- To represent any arithmetic expression.
2- To obtain sorted data by building Binary Search Tree (BST).
3- To find all duplicated data.
1- To represent any arithmetic expression:
Arithmetic expression can be represented as tree, such expression trees are
particularly interested because the prefix and postfix (suffix) notations for the
arithmetic expression correspond to important ways of traversing. When we use a
binary tree to represent an expression, parentheses are not needed to indicate
precedence. The levels of nodes in the tree indicate the relative precedence of
evaluation implicitly. The operations at higher levels of the tree are evaluated later
than those below them. The operation at the root will always be the last opearation
performed.

Operator

Operand Operand
1 2
The simplest arithmetic expression consisting of single constant, The
corresponding tree consists of a single node.

Y=3 3

The simplest expression consisting of two constants compound by any operator


such as 3+5. We can represent the binary expression as a two level binary tree.
The root of tree is operator and its two children (subtrees) are operands.

3+5
3 5

Prepared by Dr. Dunia H. Hameed Page 50


Data Structures 2024-2025

A more complex expression can be represented as any root in a tree or any subtree
is operator and leaves contain operands (variables and constants).

The following examples show the representation of expressions as trees:-

1- 5-3*6-9 -

- 9

5 *

3 6

2- a+(b-c)*d↑(e*f)
+

a *

- ↑

b c d *

e f

3- (a+b+c)*(e-f/h)

+ -

+ c /
e

a b f h
c
Prepared by Dr. Dunia H. Hameed Page 51
Data Structures 2024-2025

Tree traversals
Tree traversals means visit all the nodes in a tree only once. There are 3 common
ways for traversals of binary tree: in-order traversal, pre-order traversal, and post-
order traversal.
1- Inorder traversal: each node is visited in between its left and right
subtrees.
a. left subtree
A +
b. root
c. right subtree
B C a b
B A C
a + b (infix expression)
2- Preorder traversal: each node is visited before its left and right subtrees.
a. Root
b. left subtree
c. right subtree
A B C
+ a b (prefix expression)
3- Postorder traversal: each node is visited after its left and right subtrees.
a. left subtree
b. right subtree
c. root
B C A
a b + (suffix expression)

Prepared by Dr. Dunia H. Hameed Page 52


Data Structures 2024-2025

Note that there are another methods to traverse general tree, a tree if we convert
left by right.
1- Converse inorder: each node is visited in between its right and left
subtrees.
a. right subtree
b. root
c. left subtree
2- Converse preorder: each node is visited after its right and left subtrees.
a. Root
b. right subtree
c. left subtree
3- Converse postorder: each node is visited before its right and left subtrees.
a. right subtree
b. left subtree
c. root

Note that there are two methods depend on level concept to traverse general
tree.
1- Level by Level:
A. Top-Down: Nodes are visited starting from top level (level 0) down
to the last level. In each level we start from left to right.
B. Bottom-Up: Nodes are visited from the last level up to the top level
(level 0). In each level we start from left to right.
2- Converse Level by Level:
A. Top-Down: Nodes are visited starting from top level (level 0) down
to the last level. We start from right to left.
B. Bottom-Up: Nodes are visited from the last level up to the top level
(level 0). In each level we start from right to left.

Prepared by Dr. Dunia H. Hameed Page 53


Data Structures 2024-2025

Example 1: Write all the traversals for the following trees:


30

20 50

18 25 40 55

22 33 70

37

Inorder: 18-20-22-25-30-33-37-40-50-55-70
Preorder: 30-20-18-25-22-50-40-33-37-55-70
Postorder: 18-22-25-20-37-33-40-70-55-50-30
Converse Inorder: 70-55-50-40-37-33-30-25-22-20-18
Converse Preorder: 30-50-55-70-40-33-37-20-25-22-18
Converse Postorder: 70-55-37-33-40-50-22-25-20-18-30
Level by Level (top-down): 30-20-50-18-25-40-55-22-33-70-37
Level by Level (bottom-up): 37-22-33-70-18-25-40-55-20-50-30
Converse Level by Level (top-down): 30-50-20-55-40-25-18-70-33-22-37
Converse Level by Level (bottom-up): 37-70-33-22-55-40-25-18-50-20-30

Prepared by Dr. Dunia H. Hameed Page 54


Data Structures 2024-2025

Example2: Given the following tree, write the inorder, postorder and preorder
traversals?

B C

R Z M L

H K

R B Z H A M C LK (inorder)
A B R Z H C M LK (preorder)
R H Z B M K L C A (postorder)

Example 3: ((A+B)-(C↑D))+(E/F)
Given the above expression, draw the expression tree then use the preorder and
preorder traversals to obtain prefix and suffix forms(notations)?

- /

+ ↑ E F

A B C D

Inorder Traversal: A+B-C↑D+E/F


Preorder Traversal: +-+AB↑CD/EF
Postorder Traversal: AB+CD↑-EF/+

Prepared by Dr. Dunia H. Hameed Page 55


Data Structures 2024-2025

Example 4: Given the following tree, write the inorder, postorder, preorder
traversals and the original arithmetic expression?

- /

+ C B ↑

A B Z R

A+B-
C+B/Z↑R
(inorder)
AB+C-
BZR↑/+
(postorder)
+-
+ABC/B↑ZR
(preorder)
(((A+B)-C)+(B/(Z↑R))) (original arithmetic expression)

Prepared by Dr. Dunia H. Hameed Page 56


Data Structures 2024-2025

Example 5: Given the following tree:

+ 5

- /

* C B 3

A B

Write the original arithmetic expression?


(((A*B)-C)+(B/3))↑5
(A*B-C+B/3)↑5
Example 6: Given the following trees, write the inorder, postorder and preorder
traversals?
a
1)

b c

d k

h l

badhflck (inorder traversal)


bhlfdkca (postorder traversal)
abcdfhlk (preorder traversal)

Prepared by Dr. Dunia H. Hameed Page 57


Data Structures 2024-2025

2) a

b f

c m h l

d n x

dcbmanhxfl (inorder traversal)


dcmbnxhlfa (postorder traversal)
abcdmfhnxl (preorder traversal)

3) a

abcde (inorder traversal)


edcba (postfix traversal)
abcde (prefix traversal)

Prepared by Dr. Dunia H. Hameed Page 58


Data Structures 2024-2025

4)
a

edcba (inorder traversal)


edcba (postfix traversal)
abcde (prefix traversal)

Remark1: We can know the root from postorder traversal then we can know
the left subtree and the right subtree from the inorder traversal.
Remark2: If the tree is in one side left or right then there are two equal
traversals:
1- If inorder = preorder means that the tree is on the right.
2- If inorder = postorder means that the tree is on the left.
3- If inorder=postorder=preorder means that the tree consisted
from one node.
Remark3: If the word left interchanged by right in the traversal, we obtain
convert inorder, converse preorder and converse postorder.

Prepared by Dr. Dunia H. Hameed Page 59

You might also like