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

  • Thread starter Thread starter trickae
  • Start date Start date
  • Tags Tags
    Trees
AI Thread Summary
The discussion focuses on modifying a binary search tree (BST) into a juggle tree, emphasizing the need for recursive functions to handle operations like adding, deleting, and traversing nodes. A juggle tree is defined similarly to a binary tree, with nodes having left and right children and a parent. The process involves placing nodes based on comparisons with the root, where values less than the root go left and those greater go right.The key operation, "juggle," rearranges the tree by maintaining the left subtree of a specified node while adjusting parent-child relationships. The discussion includes a code snippet for a function that finds nodes and another that performs the juggle operation, highlighting the need for further theoretical assistance on implementing these functions in C. The term "juggle tree" is confirmed as correct within the context of the assignment, despite some initial confusion regarding its terminology. The poster expresses a willingness to share their current implementation for additional feedback.
trickae
Messages
82
Reaction score
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
write it on paper. and is the term "juggle" tree correct I thought thta was a normal shuffle or a binary tree?
 
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: 493
Last edited:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
3
Views
4K
Replies
1
Views
2K
Replies
7
Views
75K
Replies
4
Views
1K
Replies
1
Views
3K
Back
Top