Quick Search for:  in language:    
JavaScript,slow,more,speed,better,Ever,array,
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Java/ Javascript Stats

 Code: 228,322. lines
 Jobs: 100. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for Java/ Javascript.
Vending Machine Example, GUI Version.
By JJG on 1/16


Vending Machine Console Example
By JJG on 1/16


Linear Fit From X/Y Coordinates
By JJG on 1/16


Click here to see a screenshot of this code!PV iTime 1.0 J2ME
By Pavol Valentin on 1/16

(Screen Shot)

Falcon Fighter V_2_0
By Tarokahn on 1/15


Fibonacci Sequnce
By Danny Baruh Kaplun on 1/15


Prime Calculator
By Danny Baruh Kaplun on 1/15


Click here to see a screenshot of this code!EasyFTP 1.0
By Marcelo Valle Franco on 1/15

(Screen Shot)

Click here to see a screenshot of this code!A basic Client Server Application III
By Ronald Holland on 1/13

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

ordered binary tree

Print
Email
 
VB icon
Submitted on: 10/16/2002 2:58:23 PM
By: Eric Repec InetSolution  
Level: Advanced
User Rating: By 1 Users
Compatibility:JavaScript

Users have accessed this code 2070 times.
 

(About the author)
 
     JavaScript is slow and the more speed you can get out of it the better. Ever have an array, which you needed to sort or have a list of items you wanted to sort quickly. Bubble sorts do the job but do you find yourself waiting for the sort to happen. This script may help you speed up your sorts. Ordered Binary trees are said to be one of the fastest way to sort data. Here is a simple implementation of one for you. You can use it as is or improve on it. Have fun! Eric Repec
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

//**************************************
//     
// Name: ordered binary tree
// Description:JavaScript is slow and th
//     e more speed you can get out of it the b
//     etter. Ever have an array, which you nee
//     ded to sort or have a list of items you 
//     wanted to sort quickly. Bubble sorts do 
//     the job but do you find yourself waiting
//     for the sort to happen. This script may 
//     help you speed up your sorts. Ordered Bi
//     nary trees are said to be one of the fas
//     test way to sort data. Here is a simple 
//     implementation of one for you. You can u
//     se it as is or improve on it. Have fun!
Eric Repec
// By: Eric Repec InetSolution
//
// Inputs:Currently this implementation 
//     only supports the input of strings. Howe
//     ver, you can place any type of data into
//     this structure with a bit of coding. Ple
//     ase see my incode comments for help.
//
// Returns:binary tree.
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.Planet-Source-Code.com/vb/scripts/Sh
//     owCode.asp?txtCodeId=3240&lngWId;=2//for details.//**************************************
//     

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<SCRIPT LANGUAGE=JAVASCRIPT>
var oCurr; // temp object to hold our current pointer.
var t = new tree("t"); // Lets get started by creating our tree with one value.
// manually add a few nodes.
t.addSorted("a");
t.addSorted("y");
t.addSorted("z");
t.addSorted("g");
t.addSorted("q");
t.addSorted("r");
t.addSorted("s");
// show the user what our sorted list lo
//     oks like
alert(t.sortedList());
// another way to iterate through the da
//     ta..
oCurr = t.getFirst();
alert(oCurr.value);
oCurr = oCurr.getNextSorted();
alert(oCurr.value);
oCurr = oCurr.getNextSorted();
alert(oCurr.value);
oCurr = oCurr.getNextSorted();
alert(oCurr.value);
oCurr = oCurr.getNextSorted();
alert(oCurr.value);
oCurr = oCurr.getNextSorted();
alert(oCurr.value);
oCurr = oCurr.getNextSorted();
alert(oCurr.value);
// OK now lets play with arrays.
var a = new Array("r","g","f","q","h","y","z","a","b","e","u","w","b","m","x","p","k");
// create our tree and make the first no
//     de equal to the first item in the array.
//     
var t2 = new tree(a[0])
// loop through all the data in the arra
//     y and add them to the tree.
for(var i = 1;i < a.length;i++)
	t2.addSorted(a[i]);
// OK now we can pull the array back out
//     and it is sorted. 
a = t2.sortedArray();
// lets build the array into a string so
//     we can see it.
var string = "";
for(var i=0;i < a.length;i++)
	string += a[i];
alert("array dump = " + string);
function tree(value)
    {
    	this.value = value;
    	this.parent;
    	this.left;
    	this.right;
    	// this will traverse the tree and find a sutable spot for the next node. It will evaluate the value being passed. 
    	// if you wish to use something other then strings you will have to change the if statement below to properly evaluate 
    	// your type of data.
    	this.addSorted = function(value)
        	{
        		// this is where we are making the determination of where to add this node to the tree.
        		// you can change this evaluation to match your data type.
        		if(value < this.value)
            		{
            			if(this.left == null)
                			{
                				// add the value to the left of the current node
                				return this.addLeft(value);
                			}else
                    			{
                    				// recursively call addSort on the left node
                    				return this.left.addSorted(value);
                    			}
                    		}else
                        		{
                        			if(this.right == null)
                            			{
                            				// add the value to the right of the current node
                            				return this.addRight(value);
                            			}else
                                			{
                                				// recursively call addSort on the right node
                                				return this.right.addSorted(value);
                                			}
                                		}
                                	}
                                	// helper function to add a left child node.
                                	this.addLeft = function(value)
                                    	{
                                    		this.left = new tree(value);
                                    		this.left.parent = this;
                                    		return this.left;
                                    	}
                                    	// helper function to add a right child node.
                                    	this.addRight = function(value)
                                        	{
                                        		this.right = new tree(value);
                                        		this.right.parent = this;
                                        		return this.right;
                                        	}
                                        	// output a string of sorted values. this function can be changed to support your type of object.
                                        	this.sortedList = function()
                                            	{
                                            		var string = "";
                                            		string = this.value;
                                            		if(this.left != null)
                                            			string = this.left.sortedList() + "," + string;
                                            		if(this.right != null)
                                            			string += "," + this.right.sortedList();
                                            		return string;
                                            	}
                                            	// this will return the first sorted node in the tree node.
                                            	this.getFirst = function getFirst()
                                                	{
                                                		var curr = this;
                                                		while(curr.left != null)
                                                			curr = curr.left;
                                                		return curr;
                                                	}
                                                	// this will return the next node in a sorted list by using just the tree object.
                                                	// this is a bit messy because I choose not to use recursion to implement this function.
                                                	this.getNextSorted = function()
                                                    	{
                                                    		// check to see if we have a right node
                                                    		if(this.right != null)
                                                    			return this.right.getFirst();
                                                    		// check to see if we are the left node of a parent
                                                    		if(this.parent != null)
                                                    			if(this.parent.left == this)
                                                    				return this.parent;
                                                    			else
                                                        			{
                                                        				var curr = this;
                                                        				while(curr.parent.right == curr)
                                                            				{
                                                            					curr = curr.parent;
                                                            					if(curr.parent == null)
                                                            						return null;
                                                            				}
                                                            				return curr.parent;
                                                            			}
                                                            	}
                                                            	// this function will use the getNextSorted() function to build an array of sorted items
                                                            	this.sortedArray = function()
                                                                	{
                                                                		var a = new Array();
                                                                		var curr = this.getFirst();
                                                                		var i = 0;
                                                                		while(curr != null)
                                                                    		{
                                                                    			a[i++] = curr.value;
                                                                    			curr = curr.getNextSorted();	
                                                                    		}
                                                                    		return a;
                                                                    	}
                                                                }

</SCRIPT> <TITLE></TITLE> </HEAD> <BODY> <P> </P> </BODY> </HTML>


Other 1 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments

 There are no comments on this submission.
 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Java/ Javascript Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.