• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Perform a swap of all children of nodes at a given depth

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to write a function that performs a swap on all the children of nodes at a given depth.

Say we have a tree like this:



And an array containing the depth of nodes I want to perform a swap on.

If the array contains 1 then we swap all the childs of nodes at that depth. The tree would then look like this:



My function looks like this:



When I run this method on the example tree, and perform a inorder traversal of the tree, it gives me
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just wondering the requirement in this case. Inorder generally performed on binary search tree. After performing swap, tree is no more abide the
pre-condition(left child is less than root and right child is greater than the root).
 
Thomas Nilsen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:Just wondering the requirement in this case. Inorder generally performed on binary search tree. After performing swap, tree is no more abide the
pre-condition(left child is less than root and right child is greater than the root).




I just did inorder to get a look at the output, to see if my function worked.
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic