Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-23-2008, 02:09 AM
outlaw525 outlaw525 is offline
Newbie
 
Join Date: Jun 2008
Posts: 1
Rep Power: 0
outlaw525 is on a distinguished road
Question Question: "Binary Tree" only with parent pointer

Hi,

The fuction should work in C.

I need to create a struct for tree with pointer to parent only, without pointers to left & right childs.

The function got two pointers for two childs and printer the shared parent of them.

The function will print the info of the shared parent.

I attached a doc file that show what I want, there is two blue childs that the red parent is the one that the function need to print.

I alse need to know the struct elements.

Sorry for my english.

Thanks alot
Attached Files
File Type: doc example.doc (27.5 KB, 4 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 06-23-2008, 12:25 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,421
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default Re: Question: "Binary Tree" only with parent pointer

Instead of thinking of this as a Binary Tree, flip the image upside down and think of it as a linked list with multiple roots that eventually merge.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-24-2008, 09:27 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 793
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default Re: Question: "Binary Tree" only with parent pointer

Since both children have the same parent (eventually), just compare the parents recursively, like so:

C Code:
  1. struct Node
  2. {
  3.     Node  *parent;
  4.     int  value;
  5. };
  6.  
  7. Node *getParent(Node *childLeft, Node *childRight)
  8. {
  9.     Node *currLeft = childLeft;
  10.     Node *currRight = childRight;
  11.  
  12.     while(true)
  13.     {
  14.         //check for nulls
  15.         if((currLeft == NULL)||(currRight = NULL))
  16.             return NULL;
  17.         //check to see if the parents are the same
  18.         //if so, return the parent.
  19.         if(currLeft->parent == currRight->parent)
  20.             return currLeft->parent;
  21.         //parents aren't the same, continue up the tree
  22.         currLeft = currLeft->parent;
  23.         currRight = currRight->parent;
  24.     }
  25. }

Last edited by dargueta; 06-24-2008 at 09:27 PM. Reason: Fixed formatting
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows form question (select a value from childform and populate in parent form) yasinirshad Visual Basic Programming 1 11-19-2007 12:42 PM


All times are GMT -5. The time now is 11:40 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads