{"id":182,"date":"2016-07-23T00:04:41","date_gmt":"2016-07-23T00:04:41","guid":{"rendered":"http:\/\/blog.qubekwest.com\/?p=182"},"modified":"2018-01-16T13:33:01","modified_gmt":"2018-01-16T13:33:01","slug":"advanced-data-structures","status":"publish","type":"post","link":"https:\/\/blog.qubekwest.com\/?p=182","title":{"rendered":"Advanced Data Structures"},"content":{"rendered":"<p>Data structures are everywhere. \u00a0There are piles of them that make up the world in QubeKwest, but those are all specialized ones. \u00a0What happens when you need an advanced data structure for holding any random thing at all? \u00a0As previously mentioned I&#8217;m working my way back up to the point where I get a block on the screen with a texture on it, and I&#8217;m finally getting closer to where I can start to write my texture packing algorithm. \u00a0The reason for the progress is that I&#8217;ve made some more data structures.<\/p>\n<p>Since I last checked in with an update on the blog, I&#8217;ve coded a few. \u00a0First, because it&#8217;s super easy, I made a doubly Linked List. \u00a0Then, once that was working the way I liked, I added some extra bits to let that linked list act as a Stack or a Queue. \u00a0Next, I moved on to coding a &#8220;Left Leaning Red Black Tree.&#8221; \u00a0That is a super fancy version of a binary search tree that specializes a Red Black Tree with some additional constraints that are supposed to make it easier to code and to understand. \u00a0Naturally, I failed to code it or even remotely understand it.<\/p>\n<p>With the Red Black Tree removed from my code base, I spent a bit of time picking over <a href=\"https:\/\/en.wikipedia.org\/wiki\/Binary_search_tree\">Wikipedia<\/a>\u00a0to find a different binary search tree that I liked better. \u00a0(And more importantly had any hope of understanding and coding.) \u00a0This led me to the AVL Tree. \u00a0Unlike a Red Black Tree which has a seemingly arbitrary collection of color rules to determine whether a node is red or black, AVL Trees simply use the height of the tree to determine whether or not some part of the tree is out of balance by too much. \u00a0In other words, still a binary search tree that self-balances, but this time with rules based on easy to understand things that are easily defined in a tree structure.<\/p>\n<p>While I expound on the logical virtues of the <a href=\"https:\/\/en.wikipedia.org\/wiki\/AVL_tree\">AVL Tree<\/a> over the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Red%E2%80%93black_tree\">Red Black Tree<\/a>, I must take a moment to clarify that neither is a simple thing to code correctly. \u00a0In my case, writing a functioning and unit test passing AVL Tree required over a week and 26 pages of densely packed notes with everything from diagrams of rotation algorithms, notes on edge cases, and tables of balance factors to examples of hand drawn and hand rotated inserts and deletes. \u00a0The core tree code is\u00a0618 lines and 19 KB and 20 unit tests to cover all the edge cases I could think of. \u00a0(That doesn&#8217;t include the code for the tree nodes, iterators, or the tests for those.)<\/p>\n<p>To make things more complicated, my tree implementation also uses nodes that hold references to their parents. \u00a0It&#8217;s not a required feature, and it definitely makes all the functions a bit more complicated, but it allows easier traversal of the tree. \u00a0Lastly, my base implementation acts as a key-value pair. \u00a0So the tree is ordered by the key, and searched through by key, but most functions return the value. \u00a0This makes it act a bit like Java&#8217;s HashMap, but without the actual hashing or bucketing or any of the other things that data structure does. \u00a0Ok, that means it&#8217;s essentially nothing like a HashMap except for the key-value part.<\/p>\n<p>Once my tree was functioning properly, I used it as the basis of my Priority Queue. \u00a0Object oriented coding is entertaining in that way, because I got everything I need out of a Priority Queue for only 70 lines of code, and almost all of those lines are either curly braces or elaborate JavaDoc comments.<\/p>\n<p>One of the primary reasons to write your own data structures as a game programmer is to give yourself a place to manage memory better. \u00a0Whether that&#8217;s through custom memory allocations, object pools, or some other creative means to dodge the garbage collector, you often can&#8217;t integrate those things without having coded something yourself. \u00a0My data structures do not yet have any of those fun memory optimizations. \u00a0For now, they just let Java handle the job with the built in garbage collection routines but if I start getting stutters in my game I&#8217;ve now provided myself a convenient place to hook in object pools. \u00a0Also, while there may well be other general purpose advanced data structures I need to write before the game is done, I figure this collection will get me well on my way.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Data structures are everywhere. \u00a0There are piles of them that make up the world in QubeKwest, but those are all specialized ones. \u00a0What happens when you need an advanced data structure for holding any random thing at all? \u00a0As previously mentioned I&#8217;m working my way back up to the point where I get a block &hellip; <a href=\"https:\/\/blog.qubekwest.com\/?p=182\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Advanced Data Structures<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-182","post","type-post","status-publish","format-standard","hentry","category-dev"],"_links":{"self":[{"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=\/wp\/v2\/posts\/182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=182"}],"version-history":[{"count":3,"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=\/wp\/v2\/posts\/182\/revisions"}],"predecessor-version":[{"id":313,"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=\/wp\/v2\/posts\/182\/revisions\/313"}],"wp:attachment":[{"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}