• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

C functions

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I've been tasked with making a Binary Tree program with particular functions. I have been able to make most of them, but am having trouble with a few. I will post what I have below, and also the required functions remaining. Any help is appreciated, thanks!



Attatched is a photo of the remaining function structures.
Capture.PNG
[Thumbnail for Capture.PNG]
 
J Hiddleston
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This shows it is posted in Java but I cant seem to change it?
 
Saloon Keeper
Posts: 22802
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

J Hiddleston wrote:This shows it is posted in Java but I cant seem to change it?



Are you referring to the code tags? That's no problem. The option on the code tag just tells us how to colorize the display and Java and C and C++ are all similar enough to use basically the same option.
 
J Hiddleston
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:

J Hiddleston wrote:This shows it is posted in Java but I cant seem to change it?



Are you referring to the code tags? That's no problem. The option on the code tag just tells us how to colorize the display and Java and C and C++ are all similar enough to use basically the same option.



Oh okay, I wasn't sure, thanks!
 
Marshal
Posts: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, he means the forum. Nil desperandum; I shall move you to our C forum in a few seconds.
 
J Hiddleston
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:No, he means the forum. Nil desperandum; I shall move you to our C forum in a few seconds.



Thanks!
 
Rancher
Posts: 259
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi J - is it that you don't understand (1) what it is you have to do, or (2) how to do it?

If it's (2) then have you got some code to show us how far you've got? That would make it a lot easier for us to help.
 
J Hiddleston
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Matthews wrote:Hi J - is it that you don't understand (1) what it is you have to do, or (2) how to do it?

If it's (2) then have you got some code to show us how far you've got? That would make it a lot easier for us to help.



Somewhat of a mix of both. I had seen an example of how to do the depth-first search online, which I will post below.



But it takes in different parameters than what is listed in my requirements, and so I am not sure if I could implement something similar to this or not.

I'm quite new to C, so some of my questions may be simple, or I may not understand all concepts so I apologize.
 
J Hiddleston
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

J Hiddleston wrote:

John Matthews wrote:Hi J - is it that you don't understand (1) what it is you have to do, or (2) how to do it?

I worked on it some more, and thought I had found a way to make it work for these two searches, but am not getting the correct values. Any ideas why?

 
Tim Holloway
Saloon Keeper
Posts: 22802
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recommend being very paranoid about the use of braces for predicate clauses. It's a practice I've learned from bloody experience.

So I'd code that as:


Yes, it takes up more room, but it's harder to accidentally short-circuit a whole raft of code when maintaining code or slipping in temporary debug statements.

Notice I dropped the "==true" from that one test. It's redundant and potentially dangerous. Also, true C geeks would eliminate "!= NULL", since in C, false, 0, and NULL are equivalent. So:


And did you mean "node->left->data" on that first "if" statement?
 
John Matthews
Rancher
Posts: 259
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure whether your (J's) code will work; not saying it won't, I just don't understand it well enough

A simpler (for me) way to do a breadth first tree traversal (to print it) is:
  • Calculate the tree height.
  • For each tree level, traverse the tree using one of the recursive methods, printing the values of nodes which are at that level.

  • Calculating the tree height is quite easy - a recursive node function that returns 0 if the node is null, otherwise returns 1 + the greater of the heights of the node's left and right sides.

    Then you need to copy/modify one of the recursive tree traversal functions such that it takes 2 additional arguments:
  • The print level - the value of the loop variable which is iterating over the tree height.
  • The current level - incremented when the traversal function recurses into a node's left or right side.

  • Informal pseudo-code:
     
    John Matthews
    Rancher
    Posts: 259
    10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And J - I did try your code, with the correction Tim mentioned, and I can confirm it doesn't work It doesn't go far enough down (up?) into the tree. But I'm not sure what you need to do to fix it.
     
    Tim Holloway
    Saloon Keeper
    Posts: 22802
    153
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    An easy way to print out a binary tree is to recursively use what I call a "filtered function".

    I have used filtered functions for many years, and some recent improvements to Java have actually codified something like them into the language itself, but in essence, my filtered functions start with code that tries to find ways not to do the main logic of the function. Only once those have been passed do I actually do the mainline code.

    Here's a pseudo extample for a binary tree print:

    I've actually coded this sort of thing many times, although usually I print an indentation rather than a level number. You can also use this basic structure for dumping a tree to XML, JSON, YAML, or whatever.

    Note that this also works for non-binary trees of arbitrary numbers of child nodes, like so:
     
    today's feeble attempt to support the empire
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic