Data Structures: Dr. Abubaker M. F. Abushofa Department of Electrical & Electronic Engineering, Faculty of Engineering

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

http://www.abushofa.net.

ly

Department of Electrical and Electronic Engineering

Data Structures
Data Structures EE432
Fall 2018 - 2019

Data Structures - EE432


Fall 2018 - 2019
Lecture - 08

Dr. Abubaker M. F. Abushofa


Department of Electrical & Electronic
Engineering,
Faculty of Engineering
University of Tripoli, Tripoli, LIBYA
http://www.abushofa.net.ly

Basic Tree Concepts


 A tree consists of a finite set of elements, called nodes, and a
finite set of directed lines, called branches, that connect the
nodes.

Data Structures - EE432


Fall 2018 - 2019
 The number of branches associated with a node is the degree
of the node.
 When the branch is directed towards the node, it is an
indegree branch.
 When the branch is directed away from the node it is an
outdegree branch.
 The sum of the indegree and outdegree branches equals the
degree of the node.

EE432 Dr. Abubaker Abushofa 2/53


http://www.abushofa.net.ly

Basic Tree Concepts


 If the tree is not empty, then the first node is
called the root. The indegree of the root is, by

Data Structures - EE432


definition, zero.

Fall 2018 - 2019


 With the exception of the root, all of the nodes
in a tree must have an indegree of exactly one.
 All nodes in the tree can have zero, one, or
more branches leaving them; that is, they may
have an outdegree of zero, one, or more.

EE432 Dr. Abubaker Abushofa 3/53


http://www.abushofa.net.ly

Basic Tree Concepts


• There are many terms used to describe the attributes
of a tree.
– A leaf is any node with an outdegree of zero.

Data Structures - EE432


Fall 2018 - 2019
– Nodes that are not a root or a leaf are known as
internal nodes because they are found in the middle
portion of a tree.
– A node is a parent if it has successor nodes; that is, it
has an outdegree greater than zero.
– A node with a predecessor is a child.
– A child node has an indegree of at least one.
– Nodes with the same parent are siblings.

EE432 Dr. Abubaker Abushofa 4/53


http://www.abushofa.net.ly

Basic Tree Concepts


• An ancestor is any node in the path from the
root to the node.

Data Structures - EE432


• A descendent is any node in the path below

Fall 2018 - 2019


the parent node; that is, all nodes in the paths
from a given node to a leaf are descendents of
the node.
• A path Is a sequence of nodes in which each
node is adjacent to the next one. Every node
in the tree can be reached by following a
unique path starting from the root.

EE432 Dr. Abubaker Abushofa 5/53


http://www.abushofa.net.ly

Basic Tree Concepts


• The level of a node is its distance from the
root.

Data Structures - EE432


• The children of the root are at level 1, their

Fall 2018 - 2019


children are level 2, and so forth.
• The height of the tree is the level of the leaf in
the longest path from the root plus one.
• By definition, the height of an empty tree is -
1.

EE432 Dr. Abubaker Abushofa 6/53


http://www.abushofa.net.ly

Basic Tree Concepts


ROOT Level 0
A Level:
Distance of a

Data Structures - EE432


Level 1 node from root

Fall 2018 - 2019


B C
Branch Node Height:
BD The maximum
Level 2
D E F level

Level 3
G H I J

Parents: A, B, C, D, F Leaves: E, G, H, I, J
Children: B, C, D, E, F, G, H, I, J Internal Nodes: B, C, D, F
Siblings: {B, C}, {D, E, F}, {G, H}, {I, J}
EE432 Dr. Abubaker Abushofa 7/35
http://www.abushofa.net.ly

Tree Representation
A
Subtree

Data Structures - EE432


B

Fall 2018 - 2019


Subtree
B E F I

C D G H I

EE432 Dr. Abubaker Abushofa 8/35


http://www.abushofa.net.ly

Binary Trees
• A binary tree is a tree in which no node can
have more than two subtrees. In other words,

Data Structures - EE432


Fall 2018 - 2019
a node can have zero, one, or two subtrees,
• These subtrees are designated as the left
subtree and right subtree.

EE432 Dr. Abubaker Abushofa 9/53


http://www.abushofa.net.ly

Binary Trees
A

Data Structures - EE432


Fall 2018 - 2019
B E

C D F

Left Subtree Right Subtree


EE432 Dr. Abubaker Abushofa 10/35
http://www.abushofa.net.ly

Tree Properties
 The height of binary trees can be mathematically
predicted. Given that we need to store N nodes in a
binary tree, the maximum height, Hmax, is:

Data Structures - EE432


Fall 2018 - 2019
Hmax = N
 The minimum height of the tree, Hmin, is:
Hmin = [log2N] + 1
 Given a height of the binary tree, H, the minimum
and maximum number of nodes in the tree are given
as:
Nmin = H and Nmax = 2H - 1

EE432 Dr. Abubaker Abushofa 11/53


http://www.abushofa.net.ly

Binary Tree Structure


• The representation of a binary tree structure is
simple. Each node in the structure must

Data Structures - EE432


Fall 2018 - 2019
contain the data to be stored and two pointers,
one to the left subtree and one to the right
subtree.

EE432 Dr. Abubaker Abushofa 12/53


http://www.abushofa.net.ly

A Binary Search Tree (BST)


• A Binary Search Tree (BST) is a special kind of
binary tree in which:

Data Structures - EE432


1. Each node contains a distinct data value,

Fall 2018 - 2019


2. The key values in the tree can be compared
using “greater than” and “less than”, and
3. The key value of each node in the tree is less
than every key value in its right subtree, and
greater than every key value in its left
subtree.

EE432 Dr. Abubaker Abushofa 13/53


http://www.abushofa.net.ly

Binary Search Trees

Data Structures - EE432


Fall 2018 - 2019
All values in the left subtree are less All values in the right subtree are
than the value in the root greater than the value in the root
EE432 Dr. Abubaker Abushofa 14/53
http://www.abushofa.net.ly

Shape of a Binary Search Tree


• Depends on its key values and their order of
insertion.

Data Structures - EE432


Fall 2018 - 2019
• Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that
order.
• The first value to be inserted is put into the
root node. J

EE432 Dr. Abubaker Abushofa 15/53


http://www.abushofa.net.ly

Inserting ‘E’ into the BST


– Each value to be inserted begins by comparing itself
to the value in the root node, moving left it is less, or

Data Structures - EE432


Fall 2018 - 2019
moving right if it is greater.
– This continues at each level until it can be inserted as
a new leaf.

J
E

EE432 Dr. Abubaker Abushofa 16/53


http://www.abushofa.net.ly

Inserting ‘F’ into the BST

• Begin by comparing ‘F’ to the value in the root node,

Data Structures - EE432


moving left if it is less, or moving right if it is greater.

Fall 2018 - 2019


This continues until it can be inserted as a leaf.

J
E
F
EE432 Dr. Abubaker Abushofa 17/53
http://www.abushofa.net.ly

Inserting ‘T’ into the BST


• Begin by comparing ‘T’ to the value in the root node,
moving left if it is less, or moving right if it is greater. This

Data Structures - EE432


Fall 2018 - 2019
continues until it can be inserted as a leaf.

J
E T
F
EE432 Dr. Abubaker Abushofa 18/53
http://www.abushofa.net.ly

Inserting ‘A’ into the BST

• Begin by comparing ‘A’ to the value in the root node,

Data Structures - EE432


moving left if it is less, or moving right if it is greater.

Fall 2018 - 2019


This continues until it can be inserted as a leaf.

J
E T
A F
EE432 Dr. Abubaker Abushofa 19/53
http://www.abushofa.net.ly

Binary Tree Traversals


 A binary tree traversal requires that each node of
the tree be processed once only in a predetermined
sequence. There are two general approaches to the

Data Structures - EE432


Fall 2018 - 2019
traversal sequence, depth first and breadth first.
1. In the depth-first traversal, the processing proceeds
along a path from the root through one child to the
most distant descendent of that first child before
processing a second child. In other words, in the
depth-first traversal, all of the descendents of a child
are processed before the next child.

EE432 Dr. Abubaker Abushofa 20/53


http://www.abushofa.net.ly

Binary Tree Traversals

2. In a breadth-first traversal, the processing

Data Structures - EE432


proceeds horizontally from the root to all of

Fall 2018 - 2019


its children, then to its children's children,
and so forth until all nodes have been
processed.
In other words, in the breadth-first traversal,
each level is completely processed before
the next level is started.
EE432 Dr. Abubaker Abushofa 21/53
http://www.abushofa.net.ly

Depth-First Traversal
 Given that a binary tree consists of a root, a
left subtree, and a right subtree, we can

Data Structures - EE432


Fall 2018 - 2019
define three different depth-first traversal
sequences.
 The traditional designation of the traversals
uses a designation of node (N) for the root,
left (L) for the left subtree, and right (R) for
the right subtree.

EE432 Dr. Abubaker Abushofa 22/53


http://www.abushofa.net.ly

Depth-First Traversal

Data Structures - EE432


1 2 3

Fall 2018 - 2019


2 3 1 3 1 2
Left Right Left Right Left Right
Subtree Subtree Subtree Subtree Subtree Subtree
(a) Preorder Traversal (b) Inorder Traversal (c) Postorder Traversal

EE432 Dr. Abubaker Abushofa 23/35


http://www.abushofa.net.ly

Depth-First Traversal
A

Data Structures - EE432


Fall 2018 - 2019
B E

C D F

Binary Tree for Traversal


EE432 Dr. Abubaker Abushofa 24/35
http://www.abushofa.net.ly

Preorder Traversal (NLR)


• In the preorder traversal, the root node is
processed first, followed by the left subtree,

Data Structures - EE432


Fall 2018 - 2019
and then the right subtree.
• It draws its name from the Latin prefix, pre,
which means to go before. Thus, the root goes
before the subtrees.

EE432 Dr. Abubaker Abushofa 25/53


http://www.abushofa.net.ly

Preorder Traversal
Process Order A B C D E F

Data Structures - EE432


Fall 2018 - 2019
A

“Walking” Order

B E

C D F

EE432 Dr. Abubaker Abushofa 26/35


http://www.abushofa.net.ly

Inorder Traversal (LNR)


• The inorder traversal processes the left
subtree first, then the root, and finally the

Data Structures - EE432


Fall 2018 - 2019
right subtree.
• The meaning of the prefix in is that the root is
processed in between the subtrees.

EE432 Dr. Abubaker Abushofa 27/53


http://www.abushofa.net.ly

Inorder Traversal
Process Order C B D A E F

Data Structures - EE432


Fall 2018 - 2019
A
“Walking” Order

B E

C D F

EE432 Dr. Abubaker Abushofa 28/35


http://www.abushofa.net.ly

Postorder Traversal (LRN)


• The postorder traversal processes the root
node after (post) the left and right subtrees

Data Structures - EE432


Fall 2018 - 2019
have been processed.
• It starts by locating the leftmost leaf and
processing it.
• It then processes its right sibling, including its
subtrees if any.
• Finally, it processes the root node.

EE432 Dr. Abubaker Abushofa 29/53


http://www.abushofa.net.ly

Postorder Traversal
Process Order C D B F E A

Data Structures - EE432


Fall 2018 - 2019
A

“Walking” Order

B E

C D F

EE432 Dr. Abubaker Abushofa 30/35


http://www.abushofa.net.ly

Tree Traversal

Data Structures - EE432


Fall 2018 - 2019
Preorder: P F B H G S R Y T W Z
Inorder: BFGHPRSTWYZ
Postorder: B G H F R W T Z Y S P
EE432 Dr. Abubaker Abushofa 31/35
http://www.abushofa.net.ly

Breadth Traversal
• In the breadth-first traversal of a binary tree,
we process all of the children of a node before

Data Structures - EE432


Fall 2018 - 2019
proceeding with the next level.
• In other words, given a root at level n, we
process all nodes at level n before proceeding
with the nodes at level n + 1.

EE432 Dr. Abubaker Abushofa 32/53


http://www.abushofa.net.ly

Specification (TreeType.h)
#include <iostream>
struct TreeNode;

Data Structures - EE432


Fall 2018 - 2019
class TreeType
{
public:
TreeType(); // Constructor.
~TreeType(); // Destructor.
TreeType(const TreeType& originalTree);
void operator=(TreeType& originalTree);
void MakeEmpty();
bool IsEmpty() const;

EE432 Dr. Abubaker Abushofa 33/53


http://www.abushofa.net.ly

Specification (TreeType.h)
bool IsFull() const;
int LengthIs() const;
void SearchForItem(char& item, bool& found) const;

Data Structures - EE432


Fall 2018 - 2019
void InsertItem(char item);
void DeleteItem(char item);
void GetNextItem(char& item, bool& finished);
void Print() const;
private:
TreeNode* root;
};

EE432 Dr. Abubaker Abushofa 34/53


http://www.abushofa.net.ly

Structure
#include “TreeType.h”

struct TreeNode

Data Structures - EE432


Fall 2018 - 2019
{
char info;
TreeNode* left;
TreeNode* right;
};

EE432 Dr. Abubaker Abushofa 35/53


http://www.abushofa.net.ly

Tree ADT: IsFull and IsEmpty


bool TreeType::IsFull() const
{
TreeNode* location;

Data Structures - EE432


try {

Fall 2018 - 2019


location = new TreeType;
delete location;
return false;
}
catch(std::bad_alloc exception) {
return true;
}
}
bool TreeType::IsEmpty() const
{
return root == NULL;
}
EE432 Dr. Abubaker Abushofa 36/53
http://www.abushofa.net.ly

CountNodes(tree)
int TreeType::LengthIs() const
{
return CountNodes(root);

Data Structures - EE432


}

Fall 2018 - 2019


int CountNodes(TreeNode* tree)
{
if (tree == NULL)
return 0;
else
return CountNodes(tree->left) +
CountNodes(tree->right) + 1;
}
EE432 Dr. Abubaker Abushofa 37/53
http://www.abushofa.net.ly

The Function RetrieveItem


void TreeType::SearchForItem(char& item, bool& found) const
{

Data Structures - EE432


Retrieve(root, item, found);

Fall 2018 - 2019


}

EE432 Dr. Abubaker Abushofa 38/53


http://www.abushofa.net.ly

The Function RetrieveItem


void Retrieve(TreeNode* tree, char& item, bool&
found)
{
if (tree == NULL)

Data Structures - EE432


Fall 2018 - 2019
found = false;
else if (item < tree->info)
Retrieve(tree->left, item, found);
else if (item > tree->info)
Retrieve(tree->right, item, found);
else {
item = tree->info;
found = true;
}
}
EE432 Dr. Abubaker Abushofa 39/53
http://www.abushofa.net.ly

The Function InsertItem


void TreeType::InsertItem(char item)
{

Data Structures - EE432


Insert(root, item);

Fall 2018 - 2019


}

EE432 Dr. Abubaker Abushofa 40/53


http://www.abushofa.net.ly

The Function InsertItem


void Insert(TreeNode*& tree, char item)
{
if (tree == NULL) {

Data Structures - EE432


tree = new TreeNode;

Fall 2018 - 2019


tree->right = NULL;
tree->left = NULL;
tree->info = item;
}
else if (item < tree->info)
Insert(tree->left, item);
else
Insert(tree->right, item);
}
EE432 Dr. Abubaker Abushofa 41/53
http://www.abushofa.net.ly

The Function DeleteItem


void TreeType::DeleteItem(char item)
{
Delete(root, item);
}

Data Structures - EE432


Fall 2018 - 2019
void Delete(TreeNode*& tree, char item)
{
if (item < tree->info)
Delete(tree->left, item);
else if (item > tree->info)
Delete(tree->right, item);
else
DeleteNode(tree);
}
EE432 Dr. Abubaker Abushofa 42/53
http://www.abushofa.net.ly

The Function DeleteItem


void DeleteNode(TreeNode*& tree)
{

Data Structures - EE432


TreeNode* tempPtr;

Fall 2018 - 2019


tempPtr = tree;
if (tree->left == NULL) {
tree = tree->right;
delete tempPtr;
}
else if (tree->right == NULL) {
tree = tree->left;
delete tempPtr;
}

EE432 Dr. Abubaker Abushofa 43/53


http://www.abushofa.net.ly

The Function DeleteItem


else {
GetPredecessor(tree->left, data);

Data Structures - EE432


tree->info = data;

Fall 2018 - 2019


Delete(tree->left, data);
}
}

EE432 Dr. Abubaker Abushofa 44/53


http://www.abushofa.net.ly

Function GetPredecessor
void GetPredecessor(TreeNode* tree, char& data)
{
while (tree->right != NULL)

Data Structures - EE432


Fall 2018 - 2019
tree = tree->right;
data = tree->info;
}

EE432 Dr. Abubaker Abushofa 45/53


http://www.abushofa.net.ly

Printing all the Nodes inorder

Data Structures - EE432


Fall 2018 - 2019
EE432 Dr. Abubaker Abushofa 46/53
http://www.abushofa.net.ly

Function Print
• Definition: Prints the items in the binary
search tree in order from smallest to largest.

Data Structures - EE432


Fall 2018 - 2019
• Base Case: If tree = NULL, do nothing.
• General Case: Traverse the left subtree in
order.
Then print Info(tree).
Then traverse the right subtree in order.

EE432 Dr. Abubaker Abushofa 47/53


http://www.abushofa.net.ly

Function InOrder Print


void PrintTree(TreeNode* tree)
{
if (tree != NULL) {

Data Structures - EE432


Fall 2018 - 2019
PrintTree(tree->left);
cout << tree->info;
PrintTree(tree->right);
}
}
void TreeType::Print() const
{
PrintTree(root);
}

EE432 Dr. Abubaker Abushofa 48/53


http://www.abushofa.net.ly

Class Constructor and Destructor


TreeType::TreeType()
{
root = NULL;

Data Structures - EE432


Fall 2018 - 2019
}

TreeType::~TreeType()
{
Destroy(root);
}

EE432 Dr. Abubaker Abushofa 49/53


http://www.abushofa.net.ly

Class Constructor and Destructor


void Destroy(TreeNode*& tree)
{
if (tree != NULL) {

Data Structures - EE432


Fall 2018 - 2019
Destroy(tree->left);
Destroy(tree->right);
delete tree;
}
}

EE432 Dr. Abubaker Abushofa 50/53


http://www.abushofa.net.ly

Copying a Tree
TreeType::TreeType(const TreeType& originalTree)
{
CopyTree(root, originalTree.root);

Data Structures - EE432


Fall 2018 - 2019
}
void TreeType::operator=(TreeType& originalTree)
{
if (&originalTree == this)
return; // Ignore assigning self to self.
Destroy(root); // Deallocate existing tree nodes.
CopyTree(root, originalTree.root);
}
}

EE432 Dr. Abubaker Abushofa 51/53


http://www.abushofa.net.ly

Copying a Tree
void CopyTree(TreeNode*& copy, const TreeNode*
originalTree)
{

Data Structures - EE432


Fall 2018 - 2019
if (originalTree == NULL)
copy = NULL;
else {
copy = new TreeNode;
copy->info = originalTree->info;
CopyTree(copy->left, originalTree->left);
CopyTree(copy->right, originalTree->right);
}
}

EE432 Dr. Abubaker Abushofa 52/53


http://www.abushofa.net.ly

Implementation (example)
int main()
{
TreeType tree1, tree2;
char chr;

Data Structures - EE432


for (int i=0; i<5; i++) {

Fall 2018 - 2019


cout << "Enter a character "; cin >> chr;
tree1.InsertItem(chr);
}
tree2 = tree1;
tree1.Print();
cout << "\n" << tree2.LengthIs() << endl;
tree2.Print();
system("pause");
return 0;
}
EE432 Dr. Abubaker Abushofa 53/53
Data Structures - EE432
Fall 2018 - 2019
End of lecture
http://www.abushofa.net.ly

Any question?
Department of Electrical and Electronic Engineering
Data Structures EE432
Fall 2018 - 2019

You might also like