Discussion AVL BST and generic AI

Nestharus

o-o
Reaction score
84
So, here's some c# code I whipped up today for an AVL BST. Jesus4Lyf, you can implement AVL easily into your BST now >.<, you can virtually copy my code here ;o.
Code:
/* Author: Me
 * 
 * namespace System.Collections.Generic
 * 
 * public class BST
 *      private Node root;
 *      public Node Root
 *      
 *      public void Add(object obj)
 *      public void Clear()
 *      
 *      private class Node
 *          private object obj;
 *          private Node left;
 *          private Node right;
 *          private int offset
 *          
 *          internal Node(object obj)
 *          
 *          public Node Left
 *          public Node Right
 *          public int Height
 *          public object Obj
 *          
 *          private void UpdateHeight()
 *          private Node RotateRight()
 *          private Node RotateLeft()
 *          private Node Balance()
 *          private Node Insert(object obj)
 *          internal Node Add(object obj)
 */
using System;
using System.Linq;
using System.Text;
 
namespace System.Collections.Generic
{
    public class BST
    {//simple self balancing BST without a search and with prevention of equal values
        private Node root;
 
        public Node Root
        {
            get
            {
                return root;
            }//get
        }//property Root
 
        public void Add(object obj)
        {
            switch (root == null)
            {
                case true:
                    root = new Node(obj);
                    break;
                case false:
                    root = root.Add(obj);
                    break;
            }//switch
        }//method insert
 
        public void Clear()
        {
            root = null;
        }//method Clear
 
        //because I can
        public class Node
        {//all work beautifully for range and single operations
         //recursive methods as BST is expected to be small, so simplicity + speed adv
            private object obj; //payload
            private Node left;
            private Node right;
            private Node root;
            private int height;
 
            internal Node(object obj)
            {//new node
                this.obj = obj;
            }//constructor Node(object obj)
 
            public object Obj
            {
                get
                {
                    return obj;
                }//get
            }//property Obj
            public Node Left
            {
                get
                {
                    return left;
                }//get
            }//property Left
            public Node Right
            {
                get
                {
                    return right;
                }//get
            }//property Right
            public int Height
            {
                get
                {
                    return height;
                }//get
            }//property Height
 
            //Adder Methods
            //////////////////////////////////////////////////////////
            internal Node Add(object obj)
            {//Add does inserting and balancing, it acts as the traversal portion
                //completely recursive
                //on calls inwards (=) it will attempt to insert
                //on calls outwards (return) it will balance
                //*crazy* memory hog but very efficient
                //only use on small BSTs unless you want to die
 
                //compare this.obj to obj (<, >, ==)
                int comparison = ((IComparable)this.obj).CompareTo(obj);
                Node node = this;
                if (comparison > 0)
                {//<
                    left = Insert(left, obj); //insert delving in
                    node = Balance(left); //balance winding out
                    UpdateHeight(); //height has to be updated *after setting leaves
                }//if
                else if (comparison < 0)
                {//>
                    right = Insert(right, obj); //insert delving in
                    node = Balance(right); //balance winding out
                    UpdateHeight();
                }//else if
 
                return node;
            }//method Insert
 
            private Node Insert(Node leaf, object obj)
            {//insertion portion
                if (leaf == null) //leaf empty?
                {
                    leaf = new Node(obj);
                    leaf.root = this;
                    int level = 0; //for updating levels
                    for (Node node = this; node != null; node = node.root)
                        if (node.height < ++level) node.height = level;
                    return leaf;
                }//if
                return leaf.Add(obj); //continue delving in
            }//method Insert
 
            //Balancing Methods
            //////////////////////////////////////////////////////////
            private void UpdateHeight()
            {//recursively update height of entire tree
                if (left == null && right == null)
                    height = 0;
                else if (left == null) height = right.height + 1;
                else if (right == null) height = left.height + 1;
                else
                    if (left.height > right.height) height = left.height + 1;
                    else height = right.height + 1;
                if (root != null) root.UpdateHeight();
            }//method UpdateHeight
 
            private Node RotateRight() //returns pivot
            {//decrease left side by 1
                left.root = root; //update left's root
                root = left; //update this root
                if (root.right != null)
                {//if pivot has right leaf, make this point to it
                    left = root.right; //root's old left
                    left.root = this; //make left point to this
                }//if
                else left = null;
                root.right = this;
 
                return root;
            }//method RotateRight
 
            private Node RotateLeft() //returns pivot
            {//decrease right side by 1
                right.root = root; //update right's root
                root = right; //update this root
                if (root.left != null)
                {//if pivot has left leaf, make this point to it
                    right = root.left; //root's old left
                    right.root = this; //make right point to this
                }//if
                else right = null;
                root.left = this;
 
                return root;
            }//method RotateLeft
 
            private Node Balance(Node leaf) //returns root
            {//balancing portion
                if (leaf != null)
                {//only do something if there is a new leaf
                    if (right != null && left != null)
                    {
                        if (left.height - right.height > 1) return RotateRight();
                        if (right.height - left.height > 1) return RotateLeft();
                    }//if
                    else
                    {
                        if (right == null && left.height >= 1) return RotateRight();
                        if (left == null && right.height >= 1) return RotateLeft();
                    }//else
                }//if
                return this;
            }//method Balance
        }//class Node
    }//class BST
}//namespace System.Collections.Generic

So... should I translate into vJASS to be used? : p.

Oh, and I'm not really sure on the practical applications of this, heh. Really, you can have maxed array sized trees without a three crash due to the interface idea of compare, but if you converted that into a plain function call, you could run into a op limit very quickly ;o.

As for practical uses... AI =D, but would need a standard BST and an AVL BST, one for wide scope of decisions and the other for dynamic decision making, or something ;o.

Anyways, you'd be making a seriously advanced AI if you were using this, lol... actually an interesting project could be a generic AI that could learn the game by analyzing information given about the game ;o. That'd be really cool =). Also making AI for specific heroes or spells ;o, like an AI module to run a spell and would work inside of an AI framework. Could use an AVL BST to add your stuff to the AI's decision making =).

Actually, I might start up this AI framework now for OO shared design. That's kinda what I'm doing with my Spawn framework atm, people build modules and put them inside of a framework that runs those modules regardless of what they are. The framework just provides a standard API so everything can interact =).

Yum, AI framework sounds like a good idea : O.
 

Narks

Vastly intelligent whale-like being from the stars
Reaction score
90
less abbreviation and more explaining what this is
 

Narks

Vastly intelligent whale-like being from the stars
Reaction score
90
lol he's still going on about that
 

Azlier

Old World Ghost
Reaction score
461
You must spread some Reputation around before giving it to Jesus4Lyf again.

...So, what's this now? Some sort of AI? Another data structure that virtually nobody needs? I can't tell because it's written in cJass.
 

Nestharus

o-o
Reaction score
84
> I can't tell because it's written in cJass.

>So, here's some c# code I whipped up today for an AVL BST.

ownt

also it does say what this is in the title ; )

There is such a thing as google if you don't know the terms I'm using ; P
 

Hatebreeder

So many apples
Reaction score
380
> I can't tell because it's written in cJass.

>So, here's some c# code I whipped up today for an AVL BST.

ownt

also it does say what this is in the title ; )

There is such a thing as google if you don't know the terms I'm using ; P

You don't have to be cocky, IMO.

Though, you *could* comment your Code.
 

Nestharus

o-o
Reaction score
84
Wasn't being cocky and code happens to be commented... : |

But here's how it works if you must know.

The tree winds in as it searches for a free space
The tree winds out as it balances (1 rotation per)

Now, there is a bug in it that I realized this morning, but that can be fixed ; D (balance towards op side of cur side in cave scenario)
 

Azlier

Old World Ghost
Reaction score
461
>ownt

Sorry that I tend to look in the code itself for documentation, of which there appears to be none that is coherent. And there's the fact that I never read cJass because it's not worth the trouble and therefore I don't know exactly what the syntax is.

And this IS the Jass forum after all.

More importantly where would this ever be useful in a WC3 mapping scenario?
 

Nestharus

o-o
Reaction score
84
>Sorry that I tend to look in the code itself for documentation, of which there appears to be none that is coherent. And there's the fact that I never read cJass because it's not worth the trouble and therefore I don't know exactly what the syntax is.

I said what the language was on very first line of post.. : |

>And this IS the Jass forum after all.
Posted to possibly translate to JASS

>More importantly where would this ever be useful in a WC3 mapping scenario?
Already said in first post

But autonomous AI units would be rather cool methinks ; )
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top