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
381
> 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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top