searchtree.icl 3.01 KB
Newer Older
Diederik van Arkel's avatar
Diederik van Arkel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/************************************************/
/* Module:	searchtree 							*/
/* 												*/
/* Auteurs:	Tim Nieuwenhuis, Coen Goedegebure 	*/
/*												*/
/* Datum:	14/07/00							*/
/*												*/
/************************************************/

implementation module searchtree

import StdEnv, StdIO	//, StdDebug

// Definition Type
:: Definition = { funName	:: String
			     ,funDef	:: String
			    }	

definitionsFileName	:==	"Defs.def"

// Tree Type
::	Tree
	=	Leaf !Definition
	|	Node Tree !Definition Tree

// Empty Tree
newTree :== Leaf { funName= "" 
				  ,funDef = ""
		  	     }

// insert the definition in the Tree
insertIntoTree :: !Definition !Tree -> Tree
insertIntoTree d b=:(Node l e r)
	| e.funName ==""		= Node l d r
	| d.funName < e.funName	= Node (insertIntoTree d l) e r
	| d.funName > e.funName	= Node l e (insertIntoTree d r)
	| otherwise	= b
insertIntoTree d b=:(Leaf e)
	| e.funName ==""		= Node (newTree) d (newTree)
	| d.funName < e.funName	= Node (Leaf d) e (newTree)
	| d.funName > e.funName	= Node (newTree) e (Leaf d)
	| otherwise	= b

// Returns True if The String occurs in the tree
inTree :: !String !Tree -> Bool
inTree x (Leaf e)		= e.funName==x
inTree x (Node l e r)	= e.funName==x || (x<e.funName && inTree x l) || (x>e.funName && inTree x r)

// Similar to inTree, more a test-function
John van Groningen's avatar
John van Groningen committed
50
inBoom :: !String !Tree -> String
Diederik van Arkel's avatar
Diederik van Arkel committed
51 52 53 54 55 56 57 58 59 60 61 62 63
inBoom x b=:(Leaf e)		
	| (inTree x b)		= x
	| otherwise			= "dacht het niet LEAF" 
inBoom x b=:(Node l e r)	
	| (inTree x b)		= x
	| otherwise			= "echt niet NODE"

// Returns True if the Leaf in the Tree is empty
isLeafEmpty :: !Tree -> Bool
isLeafEmpty (Leaf e) = e.funName=="" 
isLeafEmpty (Node _ e _) = e.funName=="" 

// Test function... Writes the Tree to stderr
John van Groningen's avatar
John van Groningen committed
64
showTree :: !Tree -> String
Diederik van Arkel's avatar
Diederik van Arkel committed
65 66 67 68 69 70
showTree b=:(Node l e r)					
	| e.funName == "" = "*"
	| otherwise		  =  "<<-"+++(showTree l)+++"->>" +++"<<"+++ e.funName +++ ">>" +++ "<<-"+++(showTree r)+++"->>"
showTree b=:(Leaf e)  = e.funName 

// Test function.. fills the tree with numbers 0 to n
John van Groningen's avatar
John van Groningen committed
71
fillTree :: !Int !Tree -> Tree
Diederik van Arkel's avatar
Diederik van Arkel committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
fillTree n b=:(Node _ _ _)
	| n==0		= b
	| otherwise	= fillTree (n-1) (insertIntoTree ({funName=toString n,funDef=toString n +++" def"}) b)
fillTree n b=:(Leaf _)
	| n==0		= b
	| otherwise	= fillTree (n-1) (insertIntoTree ({funName=toString n,funDef=toString n +++" def"}) b)

// Returns the size of the tree
sizeTree :: !Tree -> Int
sizeTree (Leaf _)		= 1
sizeTree (Node l _ r)	= sizeTree l+sizeTree r+1

// Returns the depth of the tree
depthTree :: !Tree -> Int
depthTree (Leaf _)		= 1
depthTree (Node l _ r)	= max (depthTree l+1) (depthTree r+1)

// Find a definition-name in the Tree and returns the definition, that should be showed in the tooltip
John van Groningen's avatar
John van Groningen committed
90
searchTree :: !String !Tree -> String
Diederik van Arkel's avatar
Diederik van Arkel committed
91 92 93 94 95 96 97 98 99
searchTree x (Leaf e)  
	| ((x==" ") || (x=="\t") || (x==""))	= "space or tab"
	| e.funName==x				= e.funDef
	| otherwise					= "not found"
searchTree x (Node l e r)	
	| ((x==" ") || (x=="\t") || (x==""))	= "space or tab"
	| e.funName==x				= e.funDef
	| x<e.funName				= searchTree x l
	| x>e.funName				= searchTree x r