3 views

Uploaded by ism33

data structure

- tournament trees
- Overcurrent Protection
- Data Structures
- C++ (2nd Semester)
- Binary Tree Traversal
- On Modeling Product Advertisement in Large-Scale
- 2 3 Trees
- SE-2008-IT
- Generalized Search Trees for Database Systems
- Geometric Sample Technical Placement Paper
- Bst
- International Journal of Computer Networks (IJCN) Volume 3 Issue 1
- Particle Gibbs for Bayesian Additive Regression Trees
- Data Structure Lec14 Handout
- homework_solution_01_knn_dt.pdf
- c++
- 06565409
- Hierarchical Sampling for Active Learning
- Data Structures
- Design Table

You are on page 1of 20

13

Data Structures

Dr. Sohail Aslam

Trace of insert

14

4

15

9

7

5

17, 9, 14, 5

18

16

node

20

17

p->setRight( node );

Cost of Search

Given that a binary tree is level d deep.

How long does it take to find out whether a

number is already present?

Consider the insert(17) in the example

tree.

Each time around the while loop, we did

one comparison.

After the comparison, we moved a level

down.

Cost of Search

With the binary tree in place, we can write

a routine find(x) that returns true if the

number x is present in the tree, false

otherwise.

How many comparison are needed to find

out if x is present in the tree?

We do one comparison at each level of the

tree until either x is found or q becomes

NULL.

Cost of Search

If the binary tree is built out of n numbers,

how many comparisons are needed to find

out if a number x is in the tree?

Recall that the depth of the complete

binary tree built using n nodes will be

log2(n+1) 1.

For example, for n=100,000, log2(100001)

is less than 20; the tree would be 20 levels

deep.

Cost of Search

If the tree is complete binary or nearly

complete, searching through 100,000

numbers will require a maximum of 20

comparisons.

Or in general, approximately log2(n).

Compare this with a linked list of 100,000

numbers. The comparisons required could

be a maximum of n.

A binary tree with the property that items in

the left subtree are smaller than the root

and items are larger or equal in the right

subtree is called a binary search tree

(BST).

The tree we built for searching for

duplicate numbers was a binary search

tree.

BST and its variations play an important

role in searching algorithms.

Suppose we have a binary tree, ordered

(BST) or unordered.

We want to print all the values stored in

the nodes of the tree.

In what order should we print them?

Ways to print a 3 node tree:

14

4

15

(14,4,15), (14,15,4)

(15,4,14), (15,14,4)

In case of the general binary tree:

N

left

subtree

node

right

R

subtree

(L,N,R), (L,R,N)

(N,L,R), (N,R,L)

(R,L,N), (R,N,L)

Three common ways

N

node

left

subtree

Preorder:

Inorder:

Postorder:

right

subtree R

(N,L,R)

(L,N,R)

(L,R,N)

void preorder(TreeNode<int>* treeNode)

{

if( treeNode != NULL )

{

cout << *(treeNode->getInfo())<<"

";

preorder(treeNode->getLeft());

preorder(treeNode->getRight());

}

}

void inorder(TreeNode<int>* treeNode)

{

if( treeNode != NULL )

{

inorder(treeNode->getLeft());

cout << *(treeNode->getInfo())<<"

";

inorder(treeNode->getRight());

}

}

void postorder(TreeNode<int>* treeNode)

{

if( treeNode != NULL )

{

postorder(treeNode->getLeft());

postorder(treeNode->getRight());

cout << *(treeNode->getInfo())<<"

";

}

}

cout << "inorder: ";

preorder( root);

cout << "inorder: ";

inorder( root );

cout << "postorder: "; postorder( root );

14

4

15

9

7

18

16

Preorder: 14 4 3 9 7 5 15 18 16 17 20

20

17

14

4

15

9

7

18

16

Inorder: 3 4 5 7 9 14 15 16 17 18 20

20

17

14

4

15

9

7

5

Postorder:

18

16

20

17

3 5 7 9 4 17 16 20 18 15 14

Recursive Call

Recall that a stack is used during function

calls.

The caller function places the arguments

on the stack and passes control to the

called function.

Local variables are allocated storage on

the call stack.

Calling a function itself makes no

difference as far as the call stack is

concerned.

Here is stack layout when function F calls

function F (recursively):

Parameters(F)

sp

Parameters(F)

Parameters(F)

Local variables(F)

Local variables(F)

Local variables(F)

Return address(F)

Return address(F)

Return address(F)

Parameters(F)

Parameters(F)

sp

Local variables(F)

sp

At point of call

Return address(F)

During execution of F

After call

- tournament treesUploaded byAnonymous Hurq9VK
- Overcurrent ProtectionUploaded byShifa H Rahman
- Data StructuresUploaded byPulkit Kumar
- C++ (2nd Semester)Uploaded byKetan Rajpal
- Binary Tree TraversalUploaded byShravaniSangaonkar
- On Modeling Product Advertisement in Large-ScaleUploaded byRam Kumar
- 2 3 TreesUploaded byapi-3801329
- SE-2008-ITUploaded bysagar
- Generalized Search Trees for Database SystemsUploaded byNikolay Samokhvalov
- Geometric Sample Technical Placement PaperUploaded byPuli Naveen
- BstUploaded byapi-3830748
- International Journal of Computer Networks (IJCN) Volume 3 Issue 1Uploaded byAI Coordinator - CSC Journals
- Particle Gibbs for Bayesian Additive Regression TreesUploaded byKevin P
- Data Structure Lec14 HandoutUploaded byJʋŋaiɗ Aĸɓar
- homework_solution_01_knn_dt.pdfUploaded byCarl Woods
- c++Uploaded byRkred237
- 06565409Uploaded bylogu_thalir
- Hierarchical Sampling for Active LearningUploaded byfoobar89892
- Data StructuresUploaded bySantosh Jhansi
- Design TableUploaded byvkms
- IT- Solivar Software SolvedUploaded byKRISHNAVINOD
- ijcttjournal-v1i2p25Uploaded bysurendiran123
- 10[1].1.1.9.9907Uploaded bybutterflyv
- Sec14 Paper BaoUploaded bynicolasv
- 35950621Uploaded byIshit Mehta
- EEE_SYLLABUS.docxUploaded byupender-durishetti-2781
- 0B_Bf56Yp8VAQODI1ZS1mRzloelkUploaded byYourfriend 2113
- lec7Uploaded byRoka Ma
- Lecture-38-CS210-2012.pptxUploaded byMoazzam Hussain
- 7010_s03_qp_1Uploaded bymstudy123456

- association rulesUploaded byParul Garg
- Compiler Design Class NotesUploaded byHari Prakash
- 2003Data Mining Tut 3Uploaded bysreekarscribd
- Page RankUploaded byism33
- B+ TreeUploaded bybyrusber
- pagerankalgorithmUploaded byism33
- Itemset Rules WorksheetUploaded byism33
- compiler Expressions SolUploaded byism33
- compilerUploaded byism33
- compilerUploaded byism33
- 17683_graph Theory and Related Data Structures (1)Uploaded byism33
- 17-dynprog2Uploaded byAjazKhan
- 264Recursion_1Uploaded byism33
- Lecture 07Uploaded byism33
- Memory ManagementUploaded byism33
- MCS-ORUploaded byism33
- Introduction to Data StructuresUploaded byism33
- imppUploaded byJyotirmoy Deka
- ComplexityUploaded byism33
- Sql interviewUploaded bysagarbolisetti
- EAlecture2aUploaded byism33
- dijkstrasalgorithmUploaded byLipi Sharma
- 4-Assignment of Opration ResearchUploaded byism33
- CS301-lec15Uploaded byism33
- CS301-lec14Uploaded byism33
- CS301-lec10Uploaded byism33
- CS301-lec08Uploaded byism33
- CS301-lec06Uploaded byism33
- CS301-lec02Uploaded byism33

- Practical guide to svmUploaded byRomano
- COMPARATIVE STUDY OF JADE AND SPADE MULTI AGENT SYSTEM.Uploaded byIJAR Journal
- MD-200-300_MD-200MTUploaded bynour1969
- Lecture Note 1. Introduction to Computer and ProgrammingUploaded byPak Tam
- e17341Uploaded byYazid Aguedal
- ATyS predstavljanje.pdfUploaded bydo phuong
- SAP ReferenceUploaded byPablo Barrio
- Copia de Project_dashboard_PM.com_template.xlsxUploaded byedgar_eei
- ArraysUploaded byjosefalarka
- GLB - Gramm-Leach-Bliley ActUploaded byHitachiID
- 74ALS253-KR1533KP12DatasheetUploaded by77nicu
- New Features 11.5.10 EAM [With Demo Test Cases][1]Uploaded byjdevemy
- UTasker on Preemptive RTOSUploaded byGabi Mihaila
- Spreadsheets in Clinical Medicine - A Public Health WarningUploaded bywillhslade
- Desktop ServicesUploaded byNik Rihtaršič
- Lecture4_3.pdfUploaded byDechatorn Subcharoen
- Pdelta & Repeat LoadUploaded byGautam Paul
- Pharos Part 2_TPCUploaded byFlorin Mandric
- Password CrackingUploaded byJohn Howard
- Kuliah 7 - Fuzzy Inference SystemUploaded bytutur ramdani
- HTM_CorticalLearningAlgorithmsUploaded byrenegade.inv
- Unit-1.docxUploaded byrcpasc
- Permiso sUploaded byantony
- MXM365N Parts GuideUploaded byFausto Orquera
- Information System for Managers Complete Book (Lock) GqEdAAVYbsUploaded byGulshan Choudhary
- Simple ProgramsUploaded bySruthi Ma
- Power Consumption of Typical Household AppliancesUploaded byJoren Huerno
- App BuilderconceptsUploaded bySadot Enrique Castillo Galan
- GPS Receiver Interfacing With Microcontroller And Displaying Data On LCD And PC (MAE 5483 Term Final Project Report)Uploaded byshuvrabanik
- vb question paper solvedUploaded bylalitha