Any good algorithm to balanced a strict binary tree for a ordered set

Hi,
I have created a syntax tree of a binary operator that is associative but not commutative. The resultant is a unbalanced strict binary tree.
I have tried using AVL tree as well as a heap. But they will occasionally destruct the non-commutative property of the syntax tree.
Is there any efficient algorithm that I can used to balance this tree?
Thank you.

Is linear time good enough?
1. Traverse your tree to build a list.
2. Recursively turn the list into a tree. Pseudocode for this step: fun listToTree( [a] ) = Leaf(a)
  | listToTree( xs ) = Node( listToTree(firstHalf(xs)), listToTree(secondHalf(xs)) );

Similar Messages

  • Binary tree for parsing algebraic expressions

    hi all
    i have a project to do.. i need to read in an algebraic expression, parse it and form a binary tree. later use that tree to evaluate the expressions when the actual value for those variables are passed..
    can anyone tell me how to go about doing this.
    im a beginner in java..
    tks

    first of, your going to need three stacks. The implementation of a stack in JAVA is something that's easy to find, do a forum search. Your going to need a Binary Node as well. One stack will hold the operator elements of your algebraic expression, another will hold the numeric values, and the final will hold your binary nodes:
    expression: 4 + 6 - (6 * 9)
    now there are some rules that should be followed. Whenever a closing parenthesis is encountered ")", pop an operator, pop two numbers, and create the corresponding binary node.
    order of operations:
    push 4 onto numberStack
    push + onto operatorStack
    push 6 onto numberStack
    push - onto operatorStack
    push ( onto operator Stack
    push 6 onto numberStack
    push * onto operatorStack
    push 9 onto numberStack
    ")" encountered
    Now, your stacks look something like this:
    operatorStack: [ +, -, (, *, ) ]
    numberStack: [ 4, 6, 6, 9 ]
    pop 9 and 6 from numberStack, pop * from operatorStack
    build a binaryNode using these three elements
    push this node onto the binaryNodeStack
    I.E. using the expression above:
    / \ is the resulting binary node once the ")" is received.
    6 9
    etc.. there are some rules regarding when to pop and build binary nodes, it's been a while since i did this assignment and frankly my brain hurts, hope this gets you started, good luck.

  • Any Good FREE FCP Video Tutorials Like LiveTypeCentral Do For LT ?

    Recently my attention was drawn to some very good FREE video tutorials provided by LiveTypeCentral:-
    http://www.livetypecentral.com/cgi-bin/livetypecentral.cgi/tutorials.html
    I was wondering if there are any similar free ones covering FCE/FCP or the other FCS apps?
    I don't need them of course ....... they are just for friends! (If you believe that you'll believe anything).
    Incidentally I have done a search and had a look at the Creative Cow ones, but they tend to be on somewhat random esoteric subjects.
    I am looking for something that starts with the basics of the apps.
    Message was edited by: Ian R. Brown

    Sorry, I didn't make it clear.
    I mainly wanted freely available tutorials that I could direct others to on the FCE and other forums. Frequently newcomers don't know where to start, and I don't have time to write a new manual, so it is very efficacious to be able to point them to a site with free tutorials.
    That LiveType source I quoted is excellent. So now, basically all I am looking for is a similar one for Soundtrack and FCE/FCP.
    The first one you linked is probably fine except for the download time which would probably have taken several hours per tutorial, which is going to put off many newcomers looking for a quick-fix.
    I must admit I also like looking at tutorials (I never used to!) because every one of them has a different angle and it's surprising what you can pick up, that you have missed in others.

  • Any good place to get native extensions? (looking for an ok/cancel modal dialog myself)

    Hi all,
    I imagined someone would have made an extension that just pops up a native dialog and return the answer but I'm finding no real centralized place for extensions.
    I see the examples on the adobe site such as the NetworkInfo and Gyroscope and I may have to turn to attempting to wrap my head around creating one. Before I do that, does anyone know if there already is a native extension to pop up a simple dialog?
    I need to just adjust the title, have the two "Ok" and "Cancel" button setup and get the response from the user. Dimming the screen (modal) so it requires an answer would be great.
    Any hidden treasure troves of premade extensions?

    Thanks very much, this is right up the alley I wanted! I will try this and report back!
    edit:
    Geez you can't even get Xcode 4.2 without Lion. I had to update my system just to get Xcode 4.2 so unfortunately testing will need to wait until tomorrow..

  • Do you now of any good places that will make my itouch white for me? i currently have the most current one in black and i would like to make it white

    Please? i would really like a white itouch and do not want to buy another one, i think places like cellaris makes them white but could you give me other reccomendations? I have the 4th generation Ipod Touch in black currently.

    @Sarchet: See this user tip by Karsten Schlüter for all the options of making a video DVD - you might give the free "Burrn" a try:
    How to create a video-DVD? (Macs without iDVD)
    Regards
    Léonie

  • Looking for good algorithm to calculate a list of figures

    Hello,
    Given a list of figures:
    1899.09
    345.55
    300.00
    130.05
    90023.00
    45.90
    120.50
    8831781.56
    505.00
    45.55
    Then the program need to find all the possible addition of figures that will able to produce a answer, for example, 550.55
    Then the possible additions are:
    300.00 + 120.50 + 130.05 = 550.55
    505.00 + 45.55 = 550.55
    any good algorithm or sample code?
    Thanks for any help.

    A general knapsack problem solver is overkill here if one just wants to find a combination that fits 'perfectly'. If the list of numbers is small (< 100), a simple exhaustive search will do fine (read: it's fast). Have a look at the following sample class that solves this particular problem. Note that the set of numbers is a set of integer numbers, because floating point numbers don't add up well; in particular large and small numbers added will result in very inaccurate sums.
    kind regards,
    public class Comb {
         private int   u; // index of used numbers sofar
         private int   s; // the sum to be found
         private int[] c; // the candidate numbers
            // given a set of numbers and a sum, instantiate a solver
         public Comb(int[] c, int s) {
              this.c= c;
              this.s= s;
              this.u= c.length;
            // yes, swap two elements in array c
         private void swap(int i, int j) {
              int t= c;
              c[i] = c[j];
              c[j] = t;
    // use the number at index i as a try
         private void use(int i) {
              swap(i, --u);
    // unuse the number at index i
         private void unuse(int i) {
              swap(i, u++);
    // proccess (e.g. print) a current solution
    // note that this method could throw an exception if
    // just one solution is needed.
         private void solution() {
              for (int i= u; i < c.length; ++i) {
                   System.out.print(c[i]);
                   if (i < c.length-1)
                        System.out.print("+");
              System.out.println("="+s);
    // find all solutions to this problem
         public void find(int t) {
              if (t == 0) // ready?
                   solution();
              else if (t > 0) // still more to do?
                   for (int i= 0; i < u; ++i) {
                        use(i);
                        find(t-c);
                        unuse(i);
    // solve the example problem ... == 11
         public static void main(String[] args) {
              int c[]= { 5, 3, 6, 8 };
              int s = 11;
              new Comb(c, s).find(s);

  • Are these any good TP-Link TL-WR1043ND

    Hi,
    Just looking to replace the HH3 as I find my speed slows right down until rebooted. Once I have done this it works great for a few days and then slows right down again. I am only rebooting the HH3 and the modem stays on so makes me think it is the unit. Just wondering what experiences other people have out there and wheather the above box would be any good to replace the HH3 with.
    Thanks for any help.

    walkerx wrote:
    Nice link that you can't view unless you connected via BT Internet
    I'm browsing from another network at moment and you get blocked from view it
    Sorry - You must be connected to BT Broadband or the BT network to use this website
    My website is running on a private server and is restricted to BT Broadband and those connected to the BT network, as its intended as a help site for forum members.
    If you access via BT Wifi, you should be able to view it as well, although sometimes my reverse DNS lookup fails on some of these subnets, but I am keeping a watch to see which ones are failing.
    Unfortunately, opening up the website to the whole world, results in an exponential increase in bandwidth due to linking by search engines, attacks by hackers and other issues as well.
    There are some useful help pages here, for BT Broadband customers only, on my personal website.
    BT Broadband customers - help with broadband, WiFi, networking, e-mail and phones.

  • Any good dot matrix printers for macbook pro w/ osx tiger

    anybody got any good advice on a compatible dot matrix printer for a macbook pro running osx 10.4 tiger. want to print about 30-2 part time cards per week with excel or filemaker pro 9.0 and having a hard time finding current info on compatibility. want a reasonably priced printer which is useful without a whole lot of technical wizardry involved!

    Check out the Okidatas too...
    https://www.thenerds.net/index.php?page=esearch&action=Query&keyword=&m=1314&c=1 0030052400600&vam=1&limit=20&sort=0

  • VMware Fusion - Any Good?

    Hi,
    I'm thinking about getting the VMware Fusion to run windows on my mac today, but just wanted to know does anyone have/had it on their laptop and was it indeed any good? I tried searching on the website for it but can't find it to see how much it is in UK Pounds only in USA dollars.
    Thanks

    Just to add my two cents as it were, I've been using VMWare Fusion for at least a year now and the only problems that I've had is it not running some of my more graphics heavy games.
    For using Office and Messenger though it work perfectly. There's no slowdown when using it. You can create a virtual machine for pretty much any OS as long as you have the install files.
    Also, if you have a Boot Camp partition you can load that up as a virtual machine so you don't have to install a separate one. Although, I have a Boot Camp partition set up and I've also got a VM of the same Windows that is only running Office and a few other programs so that I can easily check compatibility.
    THe other thing you can do with the virtual machines (not the Boot Camp machine) is suspend the operation of it, which essentially puts the VM into sleep mode, but you can then close down Fusion or even you Mac. When you load up Fusion again, there's no need to wait for Windows to boot up, just click resume and it's there just as you left it.
    I've tried Parallels before but to me it just didn't feel as polished or easy to use as Fusion.
    Thats just my opinion though.
    Hope this helps
    Chris

  • Help loading a binary tree from file

    Hi all,
    So I am working on making a 20 question game using a binary tree. I have set it up so each time the user plays, if the computer cannot guess what they are thinking of, they tell the computer and it saves it. I am saving the tree in a text file recursively, in a format that looks something like this:
    Do you use it outside?
    a shovel
    [null]
    [null]
    is it round?
    a ball
    [null]
    [null]
    a TV
    [null]
    [null]
    The problem is now, I am having a very hard time rebuilding the tree when the user restarts the game. As you can see, the "{" indicates the beginning of a path and the "}" is the end. Can someone suggest a nice way to load this back up into my data structure? Thanks!!

    Cross-post here: [http://www.java-forums.org/java-2d/14237-help-loading-binary-tree-file.html]
    Recommended looking into XML

  • How to extend  breadth first Search for Binary Tree to any kind of Tree??

    Dear Friends,
    I am thinking a problem, How to extend breadth first Search for Binary Tree to any kind of Tree?? ie each node has more than 2 leaves such as 1, 2,3,4 or any,
    I have following code to successfully apply for breadth first Search in Binary Tree as follows,
    package a.border;
    import java.util.ArrayList;
    import java.util.LinkedList;
    public class Tree
        int root;
        Tree left;
        Tree right;
        static ArrayList<Integer> list = new ArrayList<Integer>();
        static ArrayList<Tree> treeList = new ArrayList<Tree>();
        private static LinkedList<Tree> queue = new LinkedList<Tree>();
         * @param root root value
         * @param left left node
         * @param right right node
        public Tree(int root, Tree left, Tree right)
            this.root = root;
            this.left = left;
            this.right = right;
        /** Creates a new instance of Tree
         * You really should know what this does...
         * @param root
        public Tree(int root)
            this.root = root;
            this.left = null;
            this.right = null;
         * Simply runs a basic left then right traversal.
        public void basicTraversal()
            //Check if we can go left
            if (left != null)
                left.basicTraversal();
            //Add the root
            list.add(root);
            //Check if we can go right
            if (right != null)
                right.basicTraversal();
        public ArrayList<Integer> getBreadthTraversal(ArrayList<Integer> list)
            //Add the root to the arraylist, we know it is always the first entry.
            list.add(root);
            //Basically we add the first set of nodes into the queue for
            //traversing.
            //Query if left exists
            if (left != null)
                //Then add the node into the tree for traversing later
                queue.add(left);
            //Same for right
            if (right != null)
                queue.add(right);
            //Then we call the traverse method to do the rest of the work
            return traverse(list);
        private ArrayList<Integer> traverse(ArrayList<Integer> list)
            //Keep traversing until we run out of people
            while (!queue.isEmpty())
                Tree p = queue.remove();
                //Check if it has any subnodes
                if (p.left != null)
                    //Add the subnode to the back of the queue
                    queue.add(p.left);
                //Same for left
                if (p.right != null)
                    //Same here, no queue jumping!
                    queue.add(p.right);
                //Append to the ArrayList
                list.add(p.root);
            //And return
            return list;
         * Makes a tree and runs some operations
         * @param args
        public static void main(String[] args)
             *                             4
             *          t =           2       6
             *                      1   3    5   7
            Tree leaf6 = new Tree(1);
            Tree leaf7 = new Tree(3);
            Tree leaf8 = new Tree(5);
            Tree leaf9 = new Tree(7);
            Tree t4 = new Tree(2, leaf6, leaf7);
            Tree t5 = new Tree(6, leaf8, leaf9);
            Tree t = new Tree(4, t4, t5);
            t.basicTraversal();
            System.out.println("Here is basicTraversal ="+list.toString());
            list.clear();
            t.getBreadthTraversal(list);
            System.out.println("getBreadthTraversal= " +list.toString());
            list.clear();
        }Can Guru help how to update to any kind of tree??
    here this code is for the tree like:
             *                             4
             *          t =           2       6
             *                      1   3    5   7
             */But i hope the new code can handle tree like:
             *                             4
             *                           /   | \
             *                          /     |   \
             *          t =            2     8   6
             *                        / |  \    |    /| \
             *                      1 11  3 9   5 10  7
             */Thanks

    sunnymanman wrote:
    Dear Friends,
    I am thinking a problem, How to extend breadth first Search for Binary Tree to any kind of Tree?? ...The answer is interfaces.
    What do all trees have in common? And what do all nodes in trees have in common?
    At least these things:
    interface Tree<T> {
        Node<T> getRoot();
    interface Node<T> {
        T getData();
        List<Node<T>> getChildren();
    }Now write concrete classes implementing these interfaces. Let's start with a binary tree (nodes should have comparable items) and an n-tree:
    class BinaryTree<T extends Comparable<T>> implements Tree<T> {
        protected BTNode<T> root;
        public Node<T> getRoot() {
            return root;
    class BTNode<T> implements Node<T> {
        private T data;
        private Node<T> left, right;
        public List<Node<T>> getChildren() {
            List<Node<T>> children = new ArrayList<Node<T>>();
            children.add(left);
            children.add(right);
            return children;
        public T getData() {
            return data;
    class NTree<T> implements Tree<T> {
        private NTNode<T> root;
        public Node<T> getRoot() {
            return root;
    class NTNode<T> implements Node<T> {
        private T data;
        private List<Node<T>> children;
        public List<Node<T>> getChildren() {
            return children;
        public T getData() {
            return data;
    }Now with these classes, you can wite a more generic traversal class. Of course, every traversal class (breath first, depth first) will also have something in common: they return a "path" of nodes (if the 'goal' node/data is found). So, you can write an interface like this:
    interface Traverser<T> {
        List<Node<T>> traverse(T goal, Tree<T> tree);
    }And finally write an implementation for it:
    class BreathFirst<T> implements Traverser<T> {
        public List<Node<T>> traverse(T goal, Tree<T> tree) {
            Node<T> start = tree.getRoot();
            List<Node<T>> children = start.getChildren();
            // your algorithm here
            return null; // return your traversal
    }... which can be used to traverse any tree! Here's a small demo of how to use it:
    public class Test {
        public static void main(String[] args) {
            Tree<Integer> binTree = new BinaryTree<Integer>();
            // populate your binTree
            Tree<Integer> nTree = new NTree<Integer>();
            // populate your nTree
            Traverser<Integer> bfTraverser = new BreathFirst<Integer>();
            // Look for integer 6 in binTree
            System.out.println("bTree bfTraversal -> "+bfTraverser.traverse(6, binTree));
            // Look for integer 6 in nTree
            System.out.println("bTree bfTraversal -> "+bfTraverser.traverse(6, nTree));
    }Good luck!

  • Mac OS X's built-in fax - any good to monitor fax status or re-fax out?

    Hi Folks,
    Just testing out my new Intel Mac Mini's faxing capabilities. It seems that the faxing status details when I view the fax monitor isn't quite as accurate as I need. For example, after I mistakenly tried to send a fax out via my printer/fax machine, I cancelled it before it finished dialing, but still the status of it showed that it was "Finished".
    Because I absolutely need to know whether my faxes got sent properly or not, should I continue to use the very convienient built-in fax software, or opt for another fax program?
    Does the built-in fax software also attempt to re-dial the same number to re-fax out if the first try failed? And can you also configure it to attempt X number of times before quitting?
    BTW, are there any good free Mac faxing programs available? Thanks.
    Intel Mac Mini   Mac OS X (10.4.5)  

    There are two excellent shareware faxing programs available: Pagesender and FaxCenter. Of the two I prefer and use Pagesender, and it's a universal binary. FaxCenter is cheaper, but hasn't been updated for a while.

  • Is the i phone any good for those with essential tremour

    is the i phone any good for those with essential tremour

    Hi auchenshugglegranny,
    Welcome to the Support Communities!
    The iPhone has a number of accessibility features that may be helpful to you, including Siri, which will accept voice commands. The article below will provide an overview for you.
    Apple - Accessibility
    http://www.apple.com/accessibility/
    Below is a list of features included in the iPhone User Guide:
    Accessibility features - iPhone
    http://help.apple.com/iphone/7/#/iph3e2e4367
    VoiceOver
    Support for braille displays
    Siri
    Zoom
    Invert Colors
    Speak Selection
    Speak Auto-text
    Large, bold, and high-contrast text
    Reduce onscreen motion
    On/off switch labels
    Hearing aids
    Subtitles and closed captions
    LED Flash for Alerts
    Mono audio and balance
    Call audio routing
    Assignable ringtones and vibrations
    Phone noise cancellation
    Guided Access
    Switch Control
    AssistiveTouch
    Widescreen keyboards
    Turn on accessibility features. Go to Settings > General > Accessibility, or use the Accessibility Shortcut. See Accessibility Shortcut.
    Use iTunes to configure accessibility. You can choose some accessibility options in iTunes on your computer. Connect iPhone to your computer and select iPhone in the iTunes device list. Click Summary, then click Configure Accessibility at the bottom of the Summary screen.
    Hope this information helps ...
    Happy Holidays!
    Judy

  • Know of a Good Algorithm to arrange a Tree (Graph)?

    Do you know of a good algorithm to arrange a graph? The JDK comes with a demo example of a graph relaxer but I was wondering if there were any others out there.
    thanks in advance,
    Anil

    Here are some sites I have been directed to:
    http://graphdrawing.org/
    Graph Drawing: Algorithms for the Visualization of Graphs (Paperback)
    by Ioannis G. Tollis, Giuseppe Di Battista, Peter Eades, Roberto Tamassia

  • Any good faq sites for ppl new to solaris?

    Hi, just want to know if there are any good sites that teach
    the basics on solaris and samba

    Hi,
    You try this URL:
    http://docs.sun.com
    Thanks.
    revert back.
    regards,
    Senthilkumar
    SUN - DTS

Maybe you are looking for

  • HT1925 I am getting a fatal error message when I try to un install itunes from Control Panel on my PC, so now I cannot un install it at all, any suggestions?

    I need to un install itunes on my PC. I have windows XP. when I go into my control panel and select to un install it goes through the proper steps then gives me a fatal error message and therefore cannot remove it. Any suggestions?

  • I cannot get my HDTV to read shared files from my iMac

    I have just brought a HDTV live and am having trouble getting it to connect to my mic and read files. I am pretty sure I have all my file sharing settings ok. I read that there are issues with this system and mountain lion is that correct! I really n

  • A verry weird problem!!

    could someone please have a look here... the problem is, the contact us page... it works fine by its self, even when its loaded into a movieClipLoader... but whe it opens from the site it doesnt display the Imput Text... I thought it was because of t

  • HT6030 update fails

    the problems are not solved after this update, got even worse; now I can only read my mail when I actually open in instead of seeing the preview. Combined with my other problems (iMac freezes, moving or deleting mail doesn't work (well), sending and

  • Kerberos Authentication on an NT machine.

    I have this config file: MyAuth { com.sun.security.auth.module.Krb5LoginModule required useTicketCache=true doNotPrompt=true; I want to authenticate a user using an already existing Kerberos Cache. This works fine on a sun box, but fails on an NT. I