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

Chapter 3 - Nonlinear Data Structures - Trees

Uploaded by

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

Chapter 3 - Nonlinear Data Structures - Trees

Uploaded by

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

BIOMEDICAL ENGINEERING

DATA STRUCTURES AND ALGORITHMS


CHAPTER 3
NONLINEAR DATA STRUCTURES : TREES
LEARNING OBJECTIVES

At the end of this chapter, you will be able to:


 Discuss the basic concepts and definitions on trees.
 Identify the properties of a binary tree.
 Enumerate the different types of binary trees.
 Discuss how binary trees are represented in computer memory.
 Analyze and simulate tree operations in terms of insertion, deletion, searching and traversing.
 Discuss properties of a Binary search tree as well as its algorithm in terms of searching,
insertion and deletion.
 Discuss the different algorithms for the different operation of an AVL Tree.

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 3


TREES

 A Tree is a recursive data structure


containing the set of one or more data
nodes where one node is designated as the
root of the tree while the remaining nodes
are called as the children of the root. The
nodes other than the root node are
partitioned into the non-empty sets where
each one of them is to be called sub-tree.

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 4


BASIC TERMINOLOGY

 Root Node:The root node is the topmost node in the tree hierarchy.
 Sub Tree: If the root node is not null, the tree T1,T2 and T3 is called sub-trees of the root node.
 Leaf Node:The node of tree, which doesn't have any child node, is called leaf node.
 Path: The sequence of consecutive edges is called path. In the tree shown in the above image, path to
the node E is A→ B → E.
 Ancestor node:An ancestor of a node is any predecessor node on a path from root to that node.
 Degree: Degree of a node is equal to number of children, a node has.
 Level Number: Each node of the tree is assigned a level number in such a way that each node is
present at one level higher than its parent. Root node of the tree is always present at level 0.

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 5


TYPES OF TREE

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 6


BINARY TREE

 Binary Tree is a special type of generic tree in


which, each node can have at most two
children. Binary tree is generally partitioned
into three disjoint subsets.
− Root of the node
− left sub-tree which is also a binary tree.
− Right binary sub-tree

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 7


TYPES OF BINARY TREE

 Strictly Binary Tree. In Strictly Binary Tree, every non-leaf


node contains non-empty left and right sub-trees. In other
words, the degree of every non-leaf node will always be 2.
A strictly binary tree with n leaves, will have (2n - 1) nodes.
A strictly binary tree is shown in the following figure.
 Complete Binary Tree. A Binary Tree is said to be a
complete binary tree if all of the leaves are located at the
same level d. A complete binary tree is a binary tree that
contains exactly 2^l nodes at each level between level 0 and
d. The total number of nodes in a complete binary tree
with depth d is 2d+1-1 where leaf nodes are 2d while non-
leaf nodes are 2d-1
CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 8
TREE REPRESENTATION

 Using Arrays - static/sequential


representation. Array size
needed will be 2k-1, k is the
height of a given tree.

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 9


TREE REPRESENTATION
CONT…
Using Linked Lists
 A tree is seen as the collection of nodes
where each node contains three parts:
left pointer, data element and right
pointer.
 The following image shows about how the
memory will be allocated for the binary
tree by using linked representation.
There is a special pointer maintained in
the memory which points to the root
node of the tree.
CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 10
PROPERTIES OF BINARY TREE

For a (proper) binary tree of depth k:


 The maximum number of nodes at level i is 2i , i ≥ 0
 The number of nodes is at least 2k + 1 and at most 2k+1 – 1
 The number of external nodes is at least h+1 and at most 2k
 The number of internal nodes is at least h and at most 2k – 1
 If no is the number of terminal nodes and n2 is the number of nodes of degree 2 in a
binary tree, then no = n2 + 1.
CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 11
BINARY TREE TRAVERSAL

 Preorder Traversal. Traverse the root


first then traverse into the left sub-
tree and right sub-tree respectively.
This procedure will be applied to each
sub-tree of the tree recursively. (NLR
Traversal)

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 12


BINARY TREE TRAVERSAL
CONT…

 Inorder Traversal. Traverse the left sub-


tree first, and then traverse the root
and the right sub-tree respectively.
This procedure will be applied to each
sub-tree of the tree recursively. (LNR
Traversal)

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 13


BINARY TREE TRAVERSAL
CONT…

 Postorder Traversal. Traverse the left


sub-tree and then traverse the right
sub-tree and root respectively. This
procedure will be applied to each sub-
tree of the tree recursively. (LRN
Traversal).

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 14


EXAMPLE: TREE TRAVERSAL

 Give the preorder, inorder and postorder traversals given the following Trees:

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 15


COUNTING TREES

Create the corresponding counting trees with the following traversals:


1. Pre: IAMHEDBCFL
Post: EHDMALFCBI
In: AHEMDICFLB
2. Pre: ABDGCEHIF
In: DGBAHEICF
3. Post: CBFEGDA
In: CBAEFDG
4. Post: FABG/+CD - ^*
In: F/AGB*+^C-D

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 16


BINARY SEARCH TREE

 Binary Search tree can be defined as a class of binary


trees, in which the nodes are arranged in a specific
order.This is also called ordered binary tree.
 In a binary search tree, the value of all the nodes in
the left sub-tree is less than the value of the root.
Similarly, value of all the nodes in the right sub-tree is
greater than or equal to the value of the root.
 This rule will be recursively applied to all the left and
right sub-trees of the root.

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 17


OPERATIONS ON A BST

 Searching in BST. If the tree is empty, then the target key is not in the tree. If the target key is
found in the root item then the target key is found in the root item. If the target key is
smaller than the root’s key then search the left subtree, otherwise search the right subtree.
 Insertion on BST. If the tree is empty, then insert the new item in the tree’s root node. If the
root’s key matches the new item’s key then skip insertion. If the new item’s key is smaller
than the root’s key, then insert the new item in the root’s left subtree. Otherwise insert the
new item in the root’s right subtree.
 Deletion on BST. First, we find the deletion node p. Find the successor node of p . Replace
the content of node p with the content of the successor node Delete the successor node.

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 18


EXAMPLE: BINARY SEARCH TREE

 Create the binary search tree using the following data elements.
− 43, 10, 79, 90, 12, 54, 11, 9, 50

 Draw the final BST after performing the following operations:


− Ins: 13, 20, 15, 17, 19, 18, 23, 22, 25, 21, 8, 5, 6, 4, 3, 2
− Del: 13, 22, 19, 6, 18

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 19


AVL TREE

 AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree is
named AVL in honour of its inventors. AVL Tree can be defined as height balanced
binary search tree in which each node is associated with a balance factor which is
calculated by subtracting the height of its right sub-tree from that of its left sub-tree.
 Tree is said to be balanced if balance factor of each node is in between -1 to 1,
otherwise, the tree will be unbalanced and need to be balanced.

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 20


AVL ROTATIONS
 We perform rotation in AVL tree only in case if Balance Factor is other than -1, 0,
and 1.

RR rotation : Inserted node is in the right


subtree of right subtree of A

LL rotation: Inserted node is in the left subtree


of left subtree of A
CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 21
AVL ROTATIONS
CONT…
 LR rotation : Inserted node is in the right subtree of left subtree of A

 RL rotation : Inserted node is in the left subtree of right subtree of A

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 22


OPERATIONS ON AVL TREE: INSERTION

1. Insert the node at the proper point.


2. Starting from the point of insertion, take the first node whose subtree heights differ by
more than one level as A.
3. If the new node is inserted in the left subtree of A, take the left child of A as B; if inserted in
the right subtree of A, take the right child of A as B.
4. If the new node is inserted in the left subtree of B, take the left child of B as C; if inserted in
the right subtree of B, take the right child of B as C.
5. Take the inorder traversal of A, B, and C.
6. Take the middle node as the parent of the two other nodes.
7. Adjust other nodes if necessary.
CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 23
OPERATIONS ON AVL TREE: DELETION

1. Delete the node.


2. If the deleted node is a non-terminal node, the immediate predecessor (immediate successor)
replaces the deleted node.
3. Starting from the deepest level, take the first node whose subtree heights differ by more than one
level as A.
4. Take the right child of A as B if it has more descendants than the left child; otherwise, take the left
child of A as B.
5. Take the right child of B as C if it has more descendants than the left child; otherwise, take the left
child of B as C. If equal prioritize LL over LR or RR over RL.
6. Take the inorder traversal of A, B, and C.
7. Take the middle node as the parent of the two other nodes.
8. Adjust other nodes. Rebalance if needed.
CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 24
EXAMPLE: AVL TREE

 Construct an AVL tree having the following elements (Insertion):


− H, I, J, B,A, E, C, F, D, G, K,

 Delete H. Construct the final AVL tree.

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 25


SUMMARY

 A Tree is a recursive data structure containing the set of one or more data nodes
where one node is designated as the root of the tree while the remaining nodes are
called as the children of the root.
 A tree can be a general tree, forests, binary tree, binary search tree, expression tree
and tournament tree.
 Binary Tree is a special type of generic tree in which, each node can have at most two
children.
 A tree can be traversed preorder, inorder and postorder traversal.
CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 26
SUMMARY CONT…

 A binary can be represented using linked (linked list) and sequential (array) representation.
 Binary Search tree can be defined as a class of binary trees, in which the nodes are arranged
in a specific order.This is also called ordered binary tree.
 BST has an algorithm in Searching, Insertion and Deletion of nodes.
 AVL Tree is a height balanced binary search tree in which each node is associated with a
balance factor which is calculated by subtracting the height of its right sub-tree from that of
its left sub-tree.
 Balancing an AVL tree includes 4 operations, these are: LL Rotation, RR Rotation, LR Rotation
and RL Rotation.

CHAPTER 3: NONLINEAR DATA STRUCUTURES - TREES Sunday, 19 February 2023 27

You might also like