functionNode (data, left, right) { This. data =data; This. Count = 1; This. left =Left ; This. right =Right ; This. Show =Show; } functionShow () {return This. Data; } functionBST () { This. root =NULL; This. Insert =Insert; This. Find =find; This. Insert =Insert; This. Update =Update; } functionInsert (data) {varn =NewNode (data,NULL,NULL); if( This. root = =NULL) { This. root =N; } Else { varCurrent = This. Root; varparent; while(true) {Parent=Current ; if(Data <current.data) { current=Current.left; if(Current = =NULL) {Parent.left=N; Break; } } Else{ Current=Current.right; if(Current = =NULL) {Parent.right=N; Break; } } } } } functionFind (data) {varCurrent = This. Root; while(Current! =NULL) { if(Current.data = =data) { returnCurrent ; } Else if(Data <current.data) { current=Current.left; } Else{ Current=Current.right; } } return NULL; } functionUpdate (data) {varGrade = This. Find (data); Grade.count++; returngrade; } functionPrarray (arr) {//10 elements per line display elements in an array for(vari = 0; i < arr.length; ++i) {document.write (Arr[i]+ " "); if((i + 1)% 10 = = 0) {document.write ("<br/>"); } } } functionGenarray (length) {//An array that produces the specified number of elements vararr = []; for(vari = 0; i < length; ++i) {Arr[i]= Math.floor (Math.random () * 101); } returnarr; } vargrades = Genarray (100); Prarray (grades); varGradedistro =NewBST (); for(vari = 0; i < grades.length; ++i) {//add elements from an array to a two-fork tree varg =Grades[i]; varGrade =Gradedistro.find (g); if(Grade = =NULL) {Gradedistro.insert (g); } Else{gradedistro.update (g); }} document.write ("***************" + "<br/>"); varG = 45; varAgrade =Gradedistro.find (g); if(Agrade = =NULL) {document.write ("No occurrences of" +g); } Else{document.write ("Occurrences of" + G + ":" +agrade.count); }
Using a binary tree to achieve a count of student scores (randomly generating 100 student scores)