data structure and algorithm design€¦ · tree data structure 3 a tree is a finite nonempty set...

35
Lecture 8, 9 Lecturer Kanar Shukr Muhamad Data Structure and Algorithm Design 2019-2020 University of Salahaddin-Hawler College of Engineering Software and Informatics Engineering Department Second Year Class

Upload: others

Post on 28-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Lecture 8, 9

Lecturer

Kanar Shukr Muhamad

Data Structure

and

Algorithm Design

2019-2020

University of Salahaddin-HawlerCollege of EngineeringSoftware and Informatics Engineering DepartmentSecond Year Class

Page 2: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Lecture Outline

2

The following topics will be studied in this two lectures in details:

1. Introduction to Binary Search Tree

2. Tree Terminology

3. Tree Traversal

4. Binary Search Tree Operations algorithms and implementations

Page 3: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Tree Data Structure

3

A tree is a finite nonempty set of elements.

It is an abstract model of a hierarchical structure.

Consists of nodes with a parent-child relation.

Faster than linear data structures

Applications:

Organization charts

File systems

Programming environments

Page 4: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Tree Terminology

4

Root: node without parent (A)

Siblings: nodes with the same parent (B, C and D), (E and F),…..

Internal node: node with at least one child (A, B, C, F)

Page 5: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Tree Terminology Continue

5

External node(leaf ): node without children (E, I, J, K, G, H, D)

Ancestors of a node: Any node, including itself, on the path from the root to the

node. Ancestors of G: G-C-A

Proper ancestors of a node: Any node, excluding itself, on the path from the root

to the node. Proper ancestors of G: C-A

Descendants of a node: Any node, including itself, on any path from the node to a

leaf node. Descendants of C: C-G –H

Proper descendant of a node: Any node, excluding itself, on any path from the

node to a leaf node. Proper Descendant of C: G-H

Page 6: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Tree Terminology Continue

6

Depth (Level) of a node: number of proper ancestors of a node. Depth of G: 2

Height of a node: The length of the longest path from a node to a leaf node.

Height of C: 1

The height of a tree: is the height of its root node. Height of a tree:3

Degree of a node: the number of its children. Degree of A: 3, Degree of B: 2

Size of a tree: The number of nodes in a tree. Size of the above tree: 11

Sub tree of a node: A tree rooted at a child of a node.

Page 7: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Tree Traversal

7

The process of systematically visiting all the nodes in a tree and performing some

computation at each node in the tree is called a tree traversal.

Pre-order traversal

Visit node, traverse left sub tree, traverse right sub tree

Post-order traversal

Traverse left sub tree, traverse right sub tree, visit node

In-order traversal

Traverse left sub tree, visit node ,traverse right subtree

Page 8: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Tree Traversal Continue

8

Pre–Brown, Truman, Taft, Ralson, Davidson, Rollins, Zuniga

Post–Davidson, Rollins, Ralson, Taft, Zuniga, Truman, Brown

In–Brown, Davidson, Ralson, Rollins, Taft, Truman, Zuniga

Page 9: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Binary Search Tree

9

Every element has a unique key.

The left and right sub trees are also binary search trees.

Values in left sub tree less than parent

Values in right sub tree greater than parent

The elements are arranged as they arrive to the tree, from top to bottom and left to

right.

A binary tree has the following time complexities...

Search Operation –O(logn)

Insertion Operation -O(logn)

Deletion Operation -O(logn)

Page 10: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Binary Search Tree Example Continue

10

Page 11: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Insertion in Binary Search Tree

11

In a binary search tree, the insertion operation is performed with O(log n) time

complexity. In binary search tree, new node is always inserted as a leaf node. The

insertion operation is performed as follows:

Step1: Create a newNode with given value and set its left and right to NULL.

Step2: Check whether tree is Empty.

Step3: If the tree is Empty, then set root to newNode.

Step4: If the tree is Not Empty, then check whether value of newNode is smaller

or larger than the node (here it is root node).

Page 12: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Insertion in Binary Search Tree Continue

12

Step 5: If newNode is smaller than or equal to the node, then move to its left

child. If newNode is larger than the node, then move to its right child.

Step 6: Repeat the above step until we reach to a leaf node (e.i., reach to NULL).

Step 7: After reaching a leaf node, then insert the newNode as left child if

newNode is smaller or equal to that leaf else insert it as right child.

Page 13: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Insertion in Binary Search Tree Continue

13

Example: Construct a Binary Search Tree by inserting the following sequence of

numbers... 10,12,5,4,20,8,7,15 and 13

Above elements are inserted into a Binary Search Tree as follows:

Page 14: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Insertion in Binary Search Tree Continue

14

Page 15: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Search in Binary Search Tree

15

In a binary search tree, the search operation is performed with O(log n) time

complexity. The search operation is performed as follows:

Step 1: Read the search element from the user.

Step 2: Compare, the search element with the value of root node in the tree.

Step 3: If both are matching, then display "Given node found!!!" and terminate the

function

Step 4: If both are not matching, then check whether search element is smaller or

larger than that node value.

Step 5: If search element is smaller, then continue the search process in left

subtree.

Page 16: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Search in Binary Search Tree Continue

16

Step 6: If search element is larger, then continue the search process in right sub

tree.

Step 7: Repeat the same until we found exact element or we completed with a leaf

node

Step 8: If we reach to the node with search value, then display "Element is found"

and terminate the function.

Step 9: If we reach to a leaf node and it is also not matching, then display

"Element not found" and terminate the function.

Page 17: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Deletion in Binary Search Tree

17

In a binary search tree, the deletion operation is performed with O(log n) time

complexity. Deleting a node from Binary search tree has following three cases:

Case 1: Deleting a Leaf node (A node with no children)

Case 2: Deleting a node with one child

Case 3: Deleting a node with two children

Page 18: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Deletion in Binary Search Tree Continue

18

Case 1: Deleting a Leaf node (A node with no children)

We use the following steps to delete a leaf node from BST:

o Step 1: Find the node to be deleted using search operation

o Step 2: Delete the node, and terminate the function.

Page 19: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Deletion in Binary Search Tree Continue

19

E.x/ Delete the node labeled 4. Since node 4 is a leaf, its sub trees are empty.

When we remove it from the tree, the tree remains a valid search tree as shown:

Page 20: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Deletion in Binary Search Tree Continue

20

Case 2: Deleting a node with one child

We use the following steps to delete a node with one child from BST:

Step 1: Find the node to be deleted using search operation

Step 2: If it has only one child, then create a link between its parent and the

chiled node.

Step 3: Delete the node terminate the function.

Page 21: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Deletion in Binary Search Tree Continue

21

E.x/ Delete a node label `3‘:

Page 22: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Deletion in Binary Search Tree Continue

22

Case 3: Deleting a node with two children

We use the following steps to delete a node with two children from 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

Page 23: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Deletion in Binary Search Tree Continue

23

Finding the successor node:

Successor node = the node in the right sub tree that has the minimum value

how to find the minimum value in a BST

Follow the left child in each branch until you reach a node that does not

have a left child

Page 24: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Deletion in Binary Search Tree Continue

24

E.x/ Delete node 9:

Page 25: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Deletion in Binary Search Tree Continue

25

E.x/ Delete 6 from a BST.

Page 26: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Deletion in Binary Search Tree Continue

26

If the right tree of the deletion node does not have a left branch

The minimum value is the root node of the right sub tree !

Page 27: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Binary Search Tree Implementation Continue

27

The following next slides contain the implementation of BST operations:

struct node

{

int value;

node* left;

node* right;

};

node* root;

1- Insert Element

Page 28: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Binary Search Tree Implementation Continue

28

node* insert(node* r, int data)

{

if(r==NULL)

{

r = new node;

r->value = data;

r->left = NULL;

r->right = NULL;

}

else if(data < r->value)

r->left=insert(r->left,data);

else

r->right=insert(r->right,data);

return r;

}

Page 29: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Binary Search Tree Implementation Continue

29

2- BST Traverse

void inOrder(node* r)

{

if(r!=NULL)

{

inOrder(r->left);

cout<<" "<< r->value;

inOrder(r->right);

}

}

E.x/ Write two methods to traverse BST in pre_order and post_order

respectively.

Page 30: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Binary Search Tree Implementation Continue

30

struct node

{

int value;

node* left;

node* right;

};

int main()

{

node* root;

root = NULL;

int n, v;

cout<<"How many data's do you want to insert ?\n";

cin>>n;

Page 31: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Binary Search Tree Implementation Continue

31

for(int i=0; i<n; i++)

{

cout<<"Data : "<< i+1<<" ";

cin>>v;

root = insert(root, v);

}

cout<<"Inorder Traversal: ";

inOrder(root);

cout<<"\n“;

return 0;

}

Page 32: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Binary Search Tree Implementation Continue

32

3- Search in BST

bool search(node* r, int key)

{

if(r==NULL)

return false;

else if(r->value == key)

return true;

else if (r->value < key)

search(r->right, key);

else search(r->left, key);

}

Page 33: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Binary Search Tree Implementation Continue

33

4- Deletion in BST

node* FindMin(node* r)

{

while(r->left != NULL)

r = r->left;

return r;

}

node* Delete(node *r, int data)

{

if(r == NULL)

return r;

else if(data < r->value)

r->left = Delete(r->left,data);

Page 34: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Binary Search Tree Implementation Continue

34

else if(data > r->value)

r->right = Delete(r->right, data);

else// found deletion node

{

if(r->left == NULL && r->right == NULL) //Case: No Child

r = NULL;

// Case 2: one child

else if(r->left == NULL)

{r = r->right; r->right = Delete(r->right, r->value);}

else if(r->right == NULL)

{r = r->left; r->left=Delete(r->left,r->value)}

Page 35: Data Structure and Algorithm Design€¦ · Tree Data Structure 3 A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. Consists of nodes

Binary Search Tree Implementation Continue

35

else //case 3

{

node *temp = FindMin(r->right);

r->value = temp->value;

r->right = Delete(r->right, temp->value);

}

}

return r;

}