Binary tree  and avl tree

hi iam not able to get the binary tree and avl tree in swings please any one help me
thanks in advance

Hi Rastabot.
If you look closely, it's not that different after all.
In each step of your recursion you need to do two things: decide whether to keep recursing, and decide what to return to the previous recursive call.
If you are trying to traverse your entire tree, then your logic to determine when to recurse and when not to is exactly the same as your example.
What you want to return each time is the number of operators in and below the node in the tree. One useful property of such an expression tree is that all the operands become leaves of the tree, and all other tree nodes are operators. Therefore, if the current node is a leaf node (your subTree == null case in your example), then that node is not an operator and has no operators below it in the tree, therefore you can return zero as the number of operators in or below that node. On the other hand, if the node is not a leaf node (your else case) then it must be an operator, and therefore you would return 1 (for that operator) plus the number of operators in each subtree below that node.
This is very similar to what you have already posted - mostly, where you have strings you need to replace with integer equivalents.
I can help with this if you need.
Mac.
Edited by: Mac.Coombe on Apr 30, 2009 11:00 PM - made explanation a little clearer

Similar Messages

  • Macking button to expand the tree and collapse tree and another to add node

    macking button to expand the tree and collapse tree and another to add node and saving the changes in the database ( this is problem)
    and finally delete node from database
    so what is proper code for those buttons
    thanks my mail is :
    [email protected]

    Hello,
    Use the ftree package's functions
    code to expand all nodes:
    PROCEDURE explose_tree IS
    node    ftree.node;
    htree   ITEM;
    state   varchar2(30);
    BEGIN
       -- search the tree ID --
       htree := Find_Item('BL_TREE.TREE_1');
        -- search the root --
           node  := Ftree.Find_Tree_Node(htree, '');
        -- expand all nodes --
       WHILE NOT Ftree.ID_NULL(node) LOOP
          state := Ftree.Get_Tree_Node_Property(htree, node, Ftree.NODE_STATE);
          IF state = Ftree.COLLAPSED_NODE THEN
            Ftree.Set_Tree_Node_Property(htree, node, Ftree.NODE_STATE, Ftree.EXPANDED_NODE);
          END IF;
          node := Ftree.Find_Tree_Node(htree, '', ftree.find_NEXT,Ftree.NODE_LABEL,'', node);
       END LOOP;
    END;and to collapse all nodes:
    PROCEDURE Implose_tree IS
       node   ftree.node;
       htree  ITEM;
       state  varchar2(30);
    BEGIN
       -- search the root ID --
       htree := Find_Item('BL_TREE.TREE_1'); 
       -- search the root --
       node  := Ftree.Find_Tree_Node(htree, '');  
       -- Collapse all nodes --
       WHILE NOT ftree.ID_NULL(node) LOOP
         state := Ftree.Get_Tree_Node_Property(htree, node, Ftree.NODE_STATE);
         IF state = Ftree.EXPANDED_NODE THEN
          Ftree.Set_Tree_Node_Property(htree, node, Ftree.NODE_STATE, Ftree.COLLAPSED_NODE);
         END IF;
        node := Ftree.Find_Tree_Node(htree, '', Ftree.FIND_NEXT,Ftree.NODE_LABEL, '', node);
       END LOOP;
    END; Francois

  • Workshop tree and Field tree have become undocked

    Hi All ,
    I have 11.5 and have somehow undocked my Workshop tree
    and Field tree in the Formula Editor.  Now all my reports are that way !
    Is there a reset button or something ?
    I couldn't find anything in the Crystal environment
    for something as simple as redocking a window.
    Help 
    Thanks,
    JImmy

    Hi Jimmy,
    There is a registry key that can reset the toolbars and dock them back.
    Before you edit the registry, please back it up and make sure u2018Allow Dockingu2019 is selected for the particular tree that is not docked. Just right-click the blank area of the floating tree and check if u2018Allow Dockingu2019 is selected.
    Go to start > run > regedit.
    Browse through HKEY_CURRENT_USER > Software > Business Objects > Suite 11.5 > Crystal Reports > Formula Workshop.
    Under this, the keys ToolBar-Bar1 to ToolBar-Bar3 are for each of the three trees. Depending on which one of them is undocked, click on the correct ToolBar-Bar1/2/3 keys. If you click on one of these keys, there should be a value on the right called u2018WindowNameu2019; this will show what tree the current key belongs to.
    Once you select the right ToolBar key, on the right side look for u2018DockingStyleu2019 and set it to u2018f000u2019. Also look for u2018MRUFloatStyleu2019 and set it to u20180u2019.
    Restart CR and open the Formula Workshop, the tree would still be undocked. To Dock it, just double-click the tree.
    Hope this helps!
    -Abhilash

  • AVL tree

    Hi;
    when I try to reblance a tree to AVL tree, But I find i can get two different result, but they all satisfy the difinition of AVL, do you think they are ok? Thanks

    You don't give many details in which sense the two results are different, do you?
    A definition of an AVL tree: it is a binary search tree where
    a) each vertex is a leaf, or
    b) each vertex has only one descendant, which is a leaf, or
    c) each vertex has two descendants; the depths of the subtrees, whose roots are the descendats, differ maximum by 1
    The point c) provides two options: either depth of the left subtree is greater or equal than the depth of right subtree, or vice versa. You could have two special cases where depth of all the left subtrees is always greater or equal than the depth of the right ones or the other way around. Quite apparently, you can have many more results, if you mix the right/left preference. This, however, is not important - the only important thing is the the depth of subtrees. If you think why, it is because it secures you that in a tree of n vertices your search will always be O(log n).

  • How to next consecutive node in binary tree and insert in sql server

    i need to find next node in this tree and insert in sql server ...
    dilip kumar

    It depends you data structure. How do you store this tree structure in the database? What's the database design? BTW, you can see this article which include a related sample :
    Custom Sort in Acyclic Digraph
    T-SQL e-book by TechNet Wiki Community
    My Blog
    My Articles

  • AVL Trees

    hey guys. I have the following insert method that i made for a BST but now i need to convert my inser and delete methods to work with AVL trees. I have created an extra variable in my node class called "balance" to keep track of every nodes balance. I have also created the method names getBalance and setBalance to get and set new balances.
    this is my inser method
         //Insert val into the BSt - does nothing if val is already in the tree
         public void insert(String val)
              root = rInsert(root, val);
         private StringAVLNode rInsert(StringAVLNode n, String str)
              if(n!=null && n.getItem().compareTo(str)==0)//if its a duplicate
              {//do nothing
              else if(n==null) //if insertion point is found
                   //create a new node in the insertion point that is found
                   StringAVLNode nd = new StringAVLNode(str, null, null);
                   n = nd;
              else if(str.compareTo(n.getItem())<0)//if insertion point is to the left
                   n.setLeft(rInsert(n.getLeft(), str));
              else // insertion point is to the right
                   n.setRight(rInsert(n.getRight(), str));
              return n; //return the new inserted node
         }i need to convert it into an AVL insert so i have to check the old balance before inserting and then check the new balance after inserting and make the proper rotation to balance the tree if its out of balance
    a node balance value of 1 means the subtree is a little heave and a balance of -1 means the left subtree is a litle heavy. 0 means the node is completely balanced. if the value is greater than 1 on the righ side then the right side is out of balance so it has to be rotated left to balance itself and if the value is less than -1, then the left side is out of balance and needs to be rotated to the right.
    i am not exactly sure where i would update the balance for the nodes during my insert method. I have done the following so far but i am completly stuck right now and clueless as to what to do.
         private StringAVLNode rInsert(StringAVLNode n, String str)
              if(n!=null && n.getItem().compareTo(str)==0)//if its a duplicate
              {//do nothing
              else if(n==null) //if insertion point is found
                   //create a new node in the insertion point that is found
                   StringAVLNode nd = new StringAVLNode(str, null, null);
                   n = nd;
              else if(str.compareTo(n.getItem())<0)//if insertion point is to the left
                   int oldBalance = n.getLeft().getBalance();
                   n.setLeft(rInsert(n.getLeft(), str));
                   int newBalance = n.getLeft().getBalance();
              else // insertion point is to the right
                   int oldBalance = n.getRight().getBalance();
                   n.setRight(rInsert(n.getRight(), str));
                   int newBalance = n.getRight().getBalance();
              return n; //return the new inserted node
         }i also have to consider double rotations. I know how they work on paper but can't seem to put it in code.
    any help would be nice.

    AVL trees are always balanced because they guarantee O(logn) retrieval. A balanced binary search tree will also be O(logn) retrieval. Thus, you could state that as long as a binary search tree is balanced it will have the same performance as an AVL tree.

  • Problem with trees and memory handling.

    Hi there, I just want to know if, when I create a large binary tree, and I re-"pointed" the root pointer to another node somewhere below in that same tree (say, a terminal node), will the "upper" parts of the tree (starting from the new root node) be removed/deleted from memory? Please explain your answers.
    Thank you.

    f I changed the root to B, will A and its children be
    deleted from memory?If you do root = B, AND if nothing else is referring to A or C or anything else beneath them, then A, C, and all of C's children will become eligible for garbage collection.
    Whether the memory actually gets cleaned up is something you can't really predict or control, but it doesn't really matter. If it's needed, it will get cleaned up. If it's not needed, it may or may not get cleaned up, but your program won't care.
    So, in short, yes, for all intents and purposes, A's, C's, and C's descendants' memory is released when you re-root to B.

  • How to create input text of the node in AVL tree

    Hi experts,
    I create a simple AVL tree to display the sales order items, just two classes for one relationship. Like the left part on the screen in this program "DEMO_ABAP_OBJECTS_CONTROLS".
    I want to let the node of the second class be maintained serial no., somthing like input field behind the node or double click the node, then show up the input field on another space on the same screen.
    And there is a button on screen, I can do some check for the text of input field when pressing the button. If the input data is wrong , then show up the error message.
    I have searched BCALV* in SE38, but i don't see anything helpful.
    Plz give me some advice, thanks a lot!!
    Regards,
    Claire

    To Kunjal and Uwe,
    My AVL tree is declared like this:
    Data: tree TYPE REF TO cl_gui_simple_tree.
    The class is not  "cl_gui_alv_tree" , so I can not use the code.
    The code I create the node is the follwing:
    CALL METHOD tree->add_nodes
             EXPORTING table_structure_name = 'ABDEMONODE'
                       node_table = node_table.
    And I looked the program that Uwe wrote, the tree is declared in class cl_gui_alv_tree, too.
    I want to double click the node and appear a field that can be inputed text.
    Is there any other way to do this with the class cl_gui_simple_tree?
    Thanks for the advence help.
    Claire

  • AVL tree in shared memory

    Can anyone help, I am not being deliverately stupid? I can create an avl tree, and I can create a shared memory area, but how do I put an AVL tree into the shared memory area?

    You are probably creating the nodes of the tree using malloc.
    Malloc is just handing you an area of memory. You already
    have a block of memory, which is the shared memory block.
    If you go to the part of the AVL program that you already have
    and replace the malloc calls with calls to a routine that you
    will write to give you blocks of your shared memory then you
    are golden.
    However, note that you may also have other details to take
    care of such as making sure that the programs with which you
    are sharing the memory block does not try to read or write the
    block at the same time as someone else is using the block.
    You can use a mutex, semaphore, or readers/writer lock for this
    purpose.

  • Help AVL Tree in JAVA code (remove) question-answer

    Hi,
    everyone. I have question.
    I want codes in Java for AVL Tree.
    I but must have remove methods or choose elements.
    It`s main for me.
    If you know, please write in code JAVA or contact e-mail.
    or link to page or if you have self code.
    thanks,
    regards
    [email protected]

    I have a better idea.
    Why don't you try to do it yourself, and when you get stuck, post your code and details of what difficulty you're having.
    When you post code, please use[code] and [/code] tags as described in Formatting tips on the message entry page. It makes it much easier to read.

  • Create tree and store it's value in an array

    I have to create a tree as shown below:
    2--->7-->6-->5-->3
    ---->4
    ---->8
    Here the numbers are the nodes of the tree and --->denotes the connection between them.
    2
    7 8
    6
    5 4
    3
    Another representation
    2 is connected to 7and 8, 7 is connected to 6 and 4, 6 is connected to 5 and then 5 to 3.
    Currently I am able to display the graph as shown in the first diagram but while storing the value, I am facing a problem.
    I have declared a double dimensional array to store the array index.This gives me 2 as a[0][0],7 as[0][1],6 as [0][2],5 as [0][3], 3 as [0][4] and 8 as [0][1] and lower(2nd) 5 as [0][2].
    Now since the timestamps are same for 7 and 8 and that for 6 and 5 so I saved them in the same array location.Instead I want 7 as [0][1][0] and 8 as[0][1][1].
    I am using recursion to built and I knw I cant add a new [] everytime.
    Can any1 of u suggest me how to store my array?
    Thanks
    The display I am trying to show instead coming proper in this windows so ------- means the connection between parent and children.
    Edited by: abheyb on 25 Mar, 2008 5:45 PM

    Hi,
    Here you have to use the pure object oriented concept rather than using procedure oriented(like c).
    Design a user defined class according to your problem that extends with any Collection. For Example. TableCell
    Declare a attribute TableCell tableCell in your class.
    Write the method called addCell(TableCell tableCell)
    and printTablecell()
    In this method you need to follow the recursion concept.
    Design as u need...! If you are designing for binary tree, then you need to declare two attribute called tableCell1 and tableCell2
    Bye.....!

  • AVL Tree Removal Problem

    I am implementing an AVL tree for school assignment.
    Everything has been smooth except for remove()method.
    There are many different ways to implement this method, and the it is difficult to debug.
    I have 5 hours left till the assignment is due, so I hope if anyone has any good examples on the remove() of AVL tree, please let me know. I have tried most links on Google and Google Code Search, but none of them suite my needs. Many of them use an extra datstructue such as queue or bitmap to do it, and some of them are too long and redundant.
    My code is in reference to Weiss' Data Structure and Algorithm in Java book.
    So basically I am looking for a hint on solving the problem or easy-to-read pseduoCode if possible.
    Thank you and please reply as soon as you can because my time is running out

    Hi, I'm using DefaultMutableTreeNode. On detecting a condition, I'm trying to remove a node under my root along with its children using removeNodeFromParent(); My appln just freezes and it doesn't refresh. The same code used to work with jvm_1.3.1 and all I did waz just upgrade it to 1.4.1_03; This is happening on a RedHat 9.0 Linux platform (if this matters). I tried catching Exception & Error - nothin' gets printed out. This is the snippet.
    DefaultTreeModel model= (DefaultTreeModel) gethardwareTree().getModel();
    HardwareTreeNode node= (HardwareTreeNode) model.getRoot();
    Enumeration enumChildren = node.children();
    ArrayList nodeList = new ArrayList();
    // This is done to avoid using Enumeration
    // Enumeration is not elegant in handling removal from its collection
    while (enumChildren.hasMoreElements())
    nodeList.add(enumChildren.nextElement());
    Iterator rootChildren = nodeList.iterator();
    while (rootChildren.hasNext())
    // HardwareTreeNode is extended from DefaultMutableTreeNode
    HardwareTreeNode hostNode = (HardwareTreeNode) rootChildren.next();
    HardwareTreeNodeInfo currentInfo= hostNode.getTreeNode();
    try {
    // The following sys.out does get printed     System.out.println("About to Remove: "+hostNode);
         model.removeNodeFromParent(hostNode);
    // The following sys.out doesn't get printed and it hangs
         System.out.println("Removed: "+hostNode);
    catch (Exception ex)//ArrayIndexOutOfBounds
    System.out.println(ex);
    Code once - use it anywhere - unfortunately didn't work??
    Pls. post Ur Xperienced thoughts.

  • How to use GUI to display AVL tree.-Urgent

    Hi,
    I implemented the insertion of the AVL Tree, it worked fine. but i do not know how to use the GUI to display the tree befor and after the insertion.
    Could you please help me,
    thanks,

    Have you considered using JTree?

  • AVL tree implementation

    Hi all,
    I am interested in an AVL tree implementation in Java. I prefer it to be Open Source. Efficiency is my main concern, although a readable code would be appreciated.
    The question is if it exist a standard implementation or an implementation you know it is stable and efficient.
    thank you in advance,

    prometheuzz wrote:
    The TreeSet and TreeMap implementations from the java.util package are backed up by a red-black tree. So those classes have the same characteristics as an AVL tree: they're self balancing, and insertions and lookups are O(log(n)) where n is the total number of nodes in your tree.
    So, as far as I see, you can use those classes. If not, please explain why you need an AVL tree instead of a proven/reliable class already present in the standard API.Thank you for the answer.
    to be honest, in the past, I rejected the idea of using TreeMap or TreeSet, but I fail to understand (or remember) why.
    The only reason I see at the moment is that I would like the tree notion of child nodes etc, although I can live without that given that I have an efficient implementation in zero-time. :)
    The use of AVL trees is not needed in the implementation, it is just an offspring of the theoretical complexity analysis of what I am about to implement.

  • Spanning Tree and Admin mac address issues srw2048

    Ok, I have a somewhat complex problem and hopefully someone may shed some light or have an idea as to whats wrong.
    First the scenario:
    I have two Cisco Cat 6509's etherchanneled to each other via two fiber cables.  One of these is the STP/RSTP root.  I have two SRW2048's.. one trunked to each of these 6509 switches.  There is also a trunk between the SRW2048's.  All this is to create a redundant topology so that if one of the switches fail's the others can still forward packets to each other.  Of course the scenario described is in fact a loop that should be handled by STP/RSTP.  I have RSTP enabled on all the switches in the scenario (PV RSTP on the cisco switches as they only do Cisco's brand of per vlan spanning tree).  There are 3 vlan's configured on each of the srw2048's (2,55,96).  There are corresponding vlan's also on the 6509's.  I have put the srw2048's management interface into vlan 2.
    The problem:
    I need to forward packets between the srw2048's primarily and only use the 6509 that is not the root when a failure happens.  I have configured the non-root 6509's spanning tree cost on the etherchannel to be higher then the alternate path through the srw's to the root.  I can hook everything up and view the spanning tree and see that the srw2048's interface that goes to the non-root 6509 is blocked, and all other interfaces on the other switches are forwarding.  I can in fact ping and get to the admin interface on all the switches.  Then for some strange reason the admin interface of the srw2048 plugged into the non-root 6509 stops responding.  If I disable either the interface its plugged into on the 6509 or the other srw2048 everything starts working again.  Sometimes it responds after many failures for no apparent reason.  I looked into the mac-address table on the 6509's and they are conflicting, pointing to each other for the mac-address of the broken srw2048.  When I clear the mac-table the admin port comes back for about 5 seconds then again goes dark.  When reviewing mac-table on the 6509's they are back to pointing to each other.  The odd thing (although I haven't confirmed this completely) is that hosts placed into vlan 2 on that same srw2048 seem to work fine.  If there was an STP loop or something misconfigured, I would expect it to effect any host in vlan 2 or the other vlan's for that matter on the srw2048 that stops responding.  Alas, I am stuck because I need to manage this switch remotely.  My only thought is that for some reason even when the STP status is blocked the broken srw2048 is still sending out arp's of its admin interface and bypassing the STP protocol.  I have no way to confirm this, but maybe someone has an idea as to what I'm doing wrong, or otherwise offer a solution.  For now, I simply removed vlan 2 from the 6509 that the broken srw2048 is plugged into and everything seems fine.
    My apologies for such a long post, but this is somewhat complicated.  Thanks in advance for any info.
    -Geoff
    Message Edited by gmyers on 08-19-2008 10:35 PM

    To follow up, I had a ticket open with Linksys about this for about 3 months with no resolution.  I submitted packet captures, stp outputs, etc and no luck.  I gave up and basically had to revert to a manual failover for redundancy.  It's no perfect or fast, but it works every time.
    Unless linksys issues a firmware upgrade with this as a fix, I doubt we will be able to ever resolve this on our own.

Maybe you are looking for

  • Posting limit restriction ( UpTO 10000) for T Code FB60 - for specific GL

    Dear All, Posting limit restriction ( UpTO 10000) for T Code FB60 - for specific GL Is it Possibal ? Any other way to stop Posting Amt more than Rs 10000 ( Ten Thousand) In specific T Code FB60 Required urgent Help, Your early reply / solution is exp

  • Mac Pro EFI Firmware 1.5 NOT installing (Yes, I RTFM)

    Starting a new topic as the other one was marked 'SOLVED'. MacPro5,1 2.8 Quad. Firmware 1.5 does install before shutdown, but the progress bar doesn't appear when holding the power button. After the loud firmware beep, the chime follows immediately a

  • OEM GRID CONTROL REMOTE INSTALLATION

    Hello Good People, I am trying to install OEM GRID CONTROL. I have a two RAC node (AIX 5L). I don't have acces to the server so my only option is remote installation. I was able to ftp the file into a directory on the server but it will not unzip, (e

  • Item does not come to delivery screen. VL01N

    Dear all, After I create customer order, I invoice it then I create Delivery. But some materials does not come to delivery screen(VL01N). Some of the materials ok.. To fix this What should I do?

  • Watermark in LR3

    I made a watermark and saved it, now I want to add it to a batch of pictures, where do I find it?