Docsity

Tree Data Structure, Binary Tree - Data Stuctures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Tree Data Structure, Binary Tree - Data Stuctures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Code for Simulation

// print the final avaerage wait time. double avgWait = (totalTime1.0)/count; cout << "Total time: " << totalTime << endl; cout << ā€œCustomer: " << count << endl; cout << "Average wait: " << avgWait << endl;*

Priority Queue

#include "Event.cpp" #define PQMAX 30 class PriorityQueue { public: PriorityQueue() { size = 0; rear = -1; }; ~PriorityQueue() {}; int full(void) { return ( size == PQMAX )? 1 : 0; };

Priority Queue

int insert(Event e) { if( !full() ) { rear = rear+1; nodes[rear] = e; size = size + 1; sortElements(); // in ascending order return 1; } cout << "insert queue is full." << endl; return 0; }; int length() { return size; }; };*

Tree Data Structures

• There are a number of applications where

linear data structures are not appropriate.

• Consider a genealogy tree of a family.

Mohammad Aslam Khan Sohail Aslam Javed Aslam Yasmeen Aslam Haaris Saad Qasim Asim Fahd Ahmad Sara Omer

Binary Tree

• A binary tree is a finite set of elements that is

either empty or is partitioned into three disjoint

subsets.

• The first subset contains a single element called

the root of the tree.

• The other two subsets are themselves binary

trees called the left and right subtrees.

• Each element of a binary tree is called a node of

the tree.

Binary Tree

• Binary tree with 9 nodes.

A

B

D

H

C

E

F

G

I

Binary Tree

• Recursive definition

A

B

D

H

C

E

F

G

Left subtree I root Right subtree

Binary Tree

• Recursive definition

A

B

D

H

C

E

F

G

I

Left subtree root

Binary Tree

• Recursive definition

A

B

D

H

C

E

F

G

I

root Right subtree

Binary Tree

• Recursive definition

A

B

D

H

C

E

F

G

I

root Right subtree Left subtree

Not a Tree

• Structures that are not trees.

A

B

D

H

C

E

F

G

I

Not a Tree

• Structures that are not trees.

A

B

D

H

C

E

F

G

I

Binary Tree

• If every non-leaf node in a binary tree has non-

empty left and right subtrees, the tree is termed

a strictly binary tree.

A

B

D

H

C

E

F

G

I

J

K

Level of a Binary Tree Node

• The level of a node in a binary tree is

defined as follows:

ļ‚§ Root has level 0,

ļ‚§ Level of any other node is one more than the

level its parent (father).

• The depth of a binary tree is the maximum

level of any leaf in the tree.