• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DayyanSisson · Mar 03, 2013 at 07:44 PM · sortswap

Need Some Help with Binary Heaps

So I've been reading this article on binary heaps and trying to incorporate it in my pathfinding code but have no luck. I was hoping to get some feedback on my attempt to see what went wrong:

 void SortedAdd (Node nextNode) {

     if(open.Count < 1){
         open.Add(new Node());
         return;
     }

     open.Add(nextNode);

     int index = open.IndexOf(nextNode);
     int parentIndex = Mathf.RoundToInt(index/2);
     float fCost = nextNode.f;

     while(fCost > open[parentIndex].f){
         if(fCost > open[parentIndex].f){
             Swap(nextNode, open[parentIndex]);
             int opIndex = parentIndex;
             parentIndex = Mathf.RoundToInt(opIndex/2);
         }else{
             break;
         }
     }
 }

 void Swap (Node n1, Node n2) {

     int n1ind = open.IndexOf(n1);
     int n2ind = open.IndexOf(n2);
     open[n1ind] = n2;
     open[n2ind] = n1;
 }

When I checked the open list, the nodes were not in order therefore the path was all over the place. Any idea where I went wrong?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Mar 04, 2013 at 12:29 PM

This code definitely can't do what you want. Suppose for instance that open currently has four values - 1,4,5 and 7, for instance - and you're going to add value 3: the new element is created at index 4 and parentIndex becomes 2; since the new element is lower than open[2], the value 3 is left at index 4, which is obviously wrong.

If you want to implement a binary insertion algorithm yourself, take a look at this article to get the basics - or post a link to the article you've mentioned in the question.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Illustrating Sorting Process of Balls In Unity Using Sorting Algorithms 0 Answers

Swapping lightmaps during runtime? 1 Answer

Swap Object Positions 3 Answers

Swapping between Diffuse and Transparent/Diffuse Shaders 0 Answers

hi, Im new in unity and I'm searching a way to swap two different 2d gameobjects on the same position when its clicked the object 1 to obeject 2.? ,hi guys, 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges