8 views

Uploaded by ham1988riz

- data mining
- Data Structure
- Elmasri 6e_ISM 18
- Qbasic for SLC
- Binary Search Tree - Java Snippets _ Dream.in
- Composite Coding Style
- Essential C - Nick Parlante
- Sorting&Searchng Algo
- Lohpetch and Corne - Discovering Effective Technical Trading Rules With Genetic Programming - Towards Robustly Outperforming Buy-And-Hold
- Ec8393 Imp Ques
- CSE(MCQ)
- t-tree
- Dictionaries
- Week16 Trees
- Thm07 - Augmenting Ds p1
- DS lab pgm
- Revision Questions
- Tree.pdf
- Heike Mildenberger and Saharon Shelah- Specialising Aronszajn Trees by Countable Approximations
- Ankith Technical Seminar

You are on page 1of 21

Implementation

CSC2100B Data Structures Tutorial 5

Outline

Implementation

Declaration

MakeEmpty

Find

FindMin

FindMax

Insert

Delete

Retrieve

Test Example

Appendix

BinarySearchTree.h

BinarySearchTree.c

TestBinarySearchTree.c

Binary Search Tree Properties

For every node T in the tree, the values of all the items in its left subtree

are smaller than the item in T

The values of all the items in its right subtree are larger than the item in

T.

Average depth of a binary search tree is O(logN).

Binary Search Tree Properties

6 6

2 8 2 8

1 4 1 4

3 3 7

Binary Search Tree Properties

6 6

2 8 2 8

1 4 1 4

3 3 7

Implementation: Declaration (.h header file)

BinarySearchTree.h

#ifndef _BINARY_SEARCH_TREE_H

#define _BINARY_SEARCH_TREE_H

struct TreeNode;

typedef struct TreeNode* Position;

typedef struct TreeNode* SearchTree;

Position Find(ElementType X,SearchTree T);

Position FindMin(SearchTree T);

Position FindMax(SearchTree T);

SearchTree Insert(ElementType X,SearchTree T);

SearchTree Delete(ElementType X,SearchTree T);

ElementType Retrieve(Position P);

#endif

Implementation: .c file

BinarySearchTree.c

#include<stdio.h>

#include "BinarySearchTree.h“

struct TreeNode

{

ElementType data;

SearchTree Left;

SearchTree Right;

};

…//implementation of all the functions

Implementation: MakeEmpty

Function:

Make a tree become empty.

//use the idea of recursion

SearchTree MakeEmpty(SearchTree T)

{

if(T!=NULL)

{

MakeEmpty(T->Left);

MakeEmpty(T->Right);

free(T);

}

return NULL;

}

Implementation: Find

Function:

find the tree node which equals to certain value

{ 6

if(T==NULL)

return NULL; 2 8

else if(X<T->data)

return Find(X,T->Left); 1 4

else if(X>T->data)

return Find(X,T->Right);

3

else

return T;

}

Implementation: FindMin

Function:

find the minimum tree node if available.

Position FindMin(SearchTree T) 6

{

if(T!=NULL) 2 8

{

while(T->Left!=NULL)

T=T->Left; 1 4

}

return T; 3

}

Implementation: FindMax

Function:

find the maximum tree node if available.

//use recursion

Position FindMax(SearchTree T) 6

{

if(T==NULL) 2 8

return NULL;

else if(T->Right==NULL)

return T; 1 4

else

return FindMax(T->Right); 3

}

Implementation: Insert

Function:

Insert a data into the tree and return the new tree.

{

if(T==NULL) 6

{

T = (SearchTree)malloc(sizeof(struct TreeNode)); 2 8

T->data = X;

T->Left = NULL;

T->Right = NULL; 1 4

}

else if(X<T->data) 3 5

T->Left = Insert(X,T->Left);

else if(X>T->data)

T->Right = Insert(X,T->Right);

else

;

return T;

}

Implementation: Delete

Function:

Delete a tree node with certain value.

6 6

2 8

can be deleted directly.

1 4 •If the node has one

1 4

child, the node can be

3 deleted after its parent

3 adjusts a link to

bypass the node.

delete

node 4

Implementation: Delete

Function:

Delete a tree node with certain value.

6 6

3 8

children, replace the

data of this node with

1 5 1 5 the smallest data of

the right subtree and

3 4 recursively delete that

node.

4

delete

node 2

Implementation: Delete

SearchTree Delete(ElementType X,SearchTree T)

{

Position Tmp;

if(T==NULL)

;

else if(X<T->data)

T->Left = Delete(X,T->Left);

else if(X>T->data)

T->Right = Delete(X,T->Right);

else if(T->Left!=NULL && T->Right!=NULL)

{

Tmp = FindMin(T->Right);

T->data = Tmp->data;

T->Right = Delete(T->data,T->Right);

}

else

{

Tmp = T;

if(T->Left==NULL)

T = T->Right;

else if(T->Right==NULL)

T = T->Left;

free(Tmp);

}

return T;

}

Implementation: Retrieve

Function:

Get the data of a tree node.

ElementType Retrieve(Position P)

{

return P->data;

}

Test Example

TestBinarySearchTree.c

Appendix

BinarySearchTree.h

BinarySearchTree.c

TestBinarySearchTree.c

- data miningUploaded bystrangertheajnabi
- Data StructureUploaded byraunak19
- Elmasri 6e_ISM 18Uploaded byAutumn Hernandez
- Qbasic for SLCUploaded bylahansanjeev
- Binary Search Tree - Java Snippets _ Dream.inUploaded byRan Run
- Composite Coding StyleUploaded byn456ks
- Essential C - Nick ParlanteUploaded byPetru Agapi
- Sorting&Searchng AlgoUploaded byAnkur Gupta
- Lohpetch and Corne - Discovering Effective Technical Trading Rules With Genetic Programming - Towards Robustly Outperforming Buy-And-HoldUploaded byegonfish
- Ec8393 Imp QuesUploaded byMuthu Ram
- CSE(MCQ)Uploaded bythirumal
- t-treeUploaded byDarek Non
- DictionariesUploaded byArnold Aravind
- Week16 TreesUploaded byDylan Gray
- Thm07 - Augmenting Ds p1Uploaded bySirc Ann Ares
- DS lab pgmUploaded byBala Subramani
- Revision QuestionsUploaded byJai Gaizin
- Tree.pdfUploaded byfsshidiq
- Heike Mildenberger and Saharon Shelah- Specialising Aronszajn Trees by Countable ApproximationsUploaded bySakoMC
- Ankith Technical SeminarUploaded byAnkith Hegde
- Von Heydebreck a.-maximum Likelihood Estimation of Oncogenetic Tree Models (2004)Uploaded byIsris Hernández Luna
- module02-1upUploaded byTiasa Mondol
- 1%2E26311Uploaded byedmundac
- b+treeUploaded byrebv
- Fuzzy Decision TreesUploaded byVu Trung Nguyen
- Do LinerUploaded byEpic Win
- Cpphtp5 22 IMUploaded byClaudio De Conti
- 03-DecisionAnalysis.pptxUploaded bykhamaludin
- Chapter5-Structures and UnionsUploaded bytsegay
- dsassgnmUploaded byjasmine_6

- 38229540 Excel FormulaUploaded bypizzaboy4u
- tdt1 task 1-2Uploaded byapi-302243217
- 4-Dof Vehicle Ride ModelUploaded bykeval patel
- F4b 2019 LEEA Training Course Assessment Enrollment Form Version 1 November 2018Uploaded bymhmd telfah
- Imcs Cbn-030 Hmi Scada Cimplicity FundamentalsUploaded bykhanghanh2017 a
- queuing_formulas.pdfUploaded byPrateek Chopra
- 0417_s10_ms_11Uploaded byKassem Farhat
- Workbench Tutorial AirfoilUploaded byfab1991
- setbreakpointmeterreadingdataprocessing-160103205411Uploaded byCristina Cunha
- Howto Create a GAL FileUploaded byajalles
- DE164 100 Series Windows 7 Setup Guide PrintUploaded bydrjperumal
- Business Plan 1Uploaded bysmartsonia_1
- GTC Export Matrix Q1 011617 - Final LockedUploaded bynbonina
- Advanced Sub Queries in Proc Sq lUploaded byMangeshWagle
- MT6572 Android Scatter 1Uploaded byJustin Reynolds
- 5_CoarraysUploaded bymero8484
- Caps - FinalUploaded byTanya Joubert
- Discuss the Methodology of Operations ResearchUploaded byankitoye
- Technical Article Pse 45Uploaded byPradyumn Dwivedi
- CodeIgniter User Guide Version 1.7.2Uploaded bylifedj
- Mobile-IP goals,entities, packet delivery, encapsulations, tunnelling and DHCP (Unit-3)Uploaded byMukesh
- Steps to Fill Online Application FormUploaded byFirdosh Khan
- Sap Sd Course lineUploaded bynitish07singh
- V6_1_sg247933Uploaded bywernereggli
- Automatic Real Time Auditorium Power Supply Control.Uploaded byKishor Mhaske
- Tc9990701d Layout Iw1Uploaded byarpitnepalia1032
- IELL-LogicalNotationUploaded byzodaken
- Safest Route Prediction in Urban Areas Final PresentationUploaded byGabriel Thomas
- stephen - manage network OpenSourceUploaded byjaskrzy
- CAD Translation GuideUploaded byIonut Daniel Epure