Learn the Basics of Juggle Trees in C: Add, Remove, Traverse

  • Thread starter trickae
  • Start date
  • Tags
    Trees
In summary, the conversation discussed the basics of a juggle tree and how it differs from a binary search tree. The juggle function was also mentioned, which is used to modify the juggle tree by rearranging the nodes according to specific rules. The conversation also touched on implementing the juggle function in C and the correct terminology for this type of tree.
  • #1
trickae
83
0
How do we find information on how to modify a binary Search tree to that of a juggle tree. SUch as add, delete, treaverse etc. Somehow all can be done with a call to the recursive function juggle.

basics of a juggle tree

given

a binary tree will have two nodes left and right, and another to its parent
if the number is less than the root node - it will be placed to the left, if its greater than the root then it will be placed on the right. If there is already is a node on either left or right pointers then the comparison is made with the next node and it will go either left or right and so forth.

bst
-----------10-----
---------4----11
-------3---6-----12
-----2----5--8---------
----1--------7-9--------juggle(4,bst)

traverses the list, looks for '4'. then it will keep everything to the left of 4 and go up to the parent. Parent is now the right node of 4. Everything on the right of 4 is now the left node of the parent. juggle(4,bst)

------------4
---------3----10
-------2-----6---11
-----5--------8----12
-------------7-9
(edit: i appologize for the 'guitar tab' notation but the spacing kept getting messed up)
This must be done for multiple parent nodes let so if we took 2 of the original bst we'd have to repeat this process by replacing the right node by the upper parent.

* i.e we'd check the last right leaf of the current tree and put the parent * at the end of that and so forth.

any help on or further information on how this would be done in C?
 
Last edited:
Technology news on Phys.org
  • #2
write it on paper. and is the term "juggle" tree correct I thought thta was a normal shuffle or a binary tree?
 
  • #3
neurocomp2003 said:
write it on paper. and is the term "juggle" tree correct I thought thta was a normal shuffle or a binary tree?

for the redrawn diagrams see attachement


well our assingment spec called it a juggle tree and we need calls to this juggle function to insert and delete.

i just need some rough help with the theory for these two functions that's all -

if you guys want i can post my juggle function that I've wrote so far.


Code:
typedef struct node {
  key          info;
  struct node *left;
  struct node *right;
  struct node *parent;
} *JuggleTree;


JuggleTree* findNode(key data, JuggleTree *jtr)
{
    if (jtr != NULL )
    {
        if (data < jtr->info)
            jtr = findNode(data, jtr->left);
        else if (data > jtr->info)
            jtr = findNode(data, jtr->right);
        else if (data != jtr->info)
            return NULL;
    }
    return jtr;
}

void juggle (key data, JuggleTree *jtr)
{
    JuggleTree* currNode = findNode(data, jtr);
    
    if(currNode->parent != NULL){
        JuggleTree* parentNode = currNode->parent;
        if (parentNode->parent != NULL) {
            if(parentNode->right == currNode)
            {
                JuggleTree* leftNode = currNode;
                while (currNode->left != NULL) {
                    leftNode = leftNode->left;
                }
                leftNode->left = parentNode;
                parentNode->parent = leftNode;
                currNode->parent = parentNode->parent;
                currNode->parent->left = currNode;
                parentNode->right = NULL;
            }
            else{              
                parentNode->parent->left = parentNode->right;
                parentNode->right = parentNode->parent;
                parentNode->parent->parent = parentNode;
                parentNode->parent = NULL;
                jtr = parentNode;
            }
            juggle(data, jtr);
        }
        else {
            parentNode->left = currNode->right;
            parentNode->parent = currNode;
            currNode->right = parentNode;
            currNode->parent = NULL;
            jtr = currNode;
        }
    }
                        
    if (currNode->parent == NULL) {
         jtr = currNode;
    }           
}


thanx again
 

Attachments

  • bst_jst.JPG
    bst_jst.JPG
    9.6 KB · Views: 444
Last edited:

Related to Learn the Basics of Juggle Trees in C: Add, Remove, Traverse

1. What is a Juggle Tree in C?

A Juggle Tree in C is a data structure that is used to store and organize data in a hierarchical manner. It is similar to a binary tree, but instead of having two branches, it can have any number of branches (also known as children). Each node in a Juggle Tree contains a value and a pointer to its children, if any.

2. How do you add a node to a Juggle Tree in C?

To add a node to a Juggle Tree in C, you first need to create a new node with the desired value. Then, you need to find the appropriate location in the tree to add the new node. This can be done by traversing the tree using a specific algorithm, such as depth-first or breadth-first. Once you have found the location, you can add the new node as a child of that location's parent node.

3. How do you remove a node from a Juggle Tree in C?

To remove a node from a Juggle Tree in C, you first need to find the node you want to remove. Then, you need to check if the node has any children. If it does, you can either promote one of its children to take its place, or you can merge its children with its parent's other children. If the node has no children, you can simply remove it from the tree by updating its parent node's pointer to point to its sibling.

4. What is tree traversal in C?

Tree traversal in C refers to the process of visiting each node in a Juggle Tree in a specific order. There are two main methods of tree traversal: depth-first and breadth-first. Depth-first traversal visits all the nodes on a branch before moving on to the next branch, while breadth-first traversal visits all the nodes on a level before moving on to the next level. Tree traversal is often used to search for a specific node or to perform operations on all the nodes in a tree.

5. Why is it important to learn the basics of Juggle Trees in C?

Learning the basics of Juggle Trees in C is important because it is a fundamental data structure that is used in many applications. It allows for efficient organization and retrieval of data, making it a valuable tool for developers. Understanding how to add, remove, and traverse a Juggle Tree in C can help you improve your problem-solving skills and make you a more versatile programmer.

Similar threads

  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
7
Views
75K
  • Programming and Computer Science
Replies
1
Views
701
Back
Top