473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

how to traverse a array represented binary tree using only a father field

How to traverse a binary tree whose nodes only contain a father field. e.g.

struct NodeType {
int data;
int father;
};

A left son's father field has a negative value and right son's father field has +ve value. Absolute value of the father field points to the father of the node.
Such tree can be traversed only in an upward direction but i cannot reach all nodes starting at a leaf. Once all nodes reachable from a leaf are traversed, i get stuck. What i can make out is that while constructing the tree, i will need to keep track of more than just one leaf so that i can traverse all nodes of the binary tree

Want to know an algorthm for this

Am tryng to solve a data-structure exercise in aarom m. tenebaum --5.2.4
Jul 24 '10 #1
6 4055
iohos
45
dis mite be a lengthy solution....... do u need d whole algorithm or just d remedial soln
Jul 25 '10 #2
iohos
45
struct TreeNode
{
struct TreeNode *lgame;
int data;
struct TreeNode *rgame;
};
typedef struct TreeNode TreeNode;
Jul 25 '10 #3
@iohos
Dynamic representation is not to be used.

struct NodeType {
int data;
int father;
};

How to traverse a binary tree wherein each node is as shown above

Initial problem specification is to convert a binary tree represented using nodeType as i have shown to a binary tree using nodeType as you have shown.
Jul 25 '10 #4
iohos
45
ok, wrking on it. will post it soon
Jul 25 '10 #5
jkmyoung
2,057 Expert 2GB
  1. Create a node array of same length as your initial array.
  2. For each array element, create a node. Set the values to be the same, but initially set all children to null.
  3. Go back through the array.
  4. Set each node's children accordingly. Eg, if the first array with your NodeType is A, and the second array with the other node is A2:
foreach node in A
{
get parent index
determine if we are left or right child by comparing values.
set A2[parent_index] corresponding child accordingly.
}
Jul 29 '10 #6
hi jkm,
thank u for the solution. its simple & good.

sorry abt the delayed response

Regards
Aug 22 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tuhin Kumar | last post by:
Hi, I would like to know how to perform the bulk insertion of binary data using the OCI. Thanks, Tuhin
7
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
0
by: Reed | last post by:
Can someone stear me in the right direction to convert a binary tree from a Linked List to a Dynamic Array... Dynamic arrays aren't something im strong with. //********bintree.h******** #ifndef...
5
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
10
by: Aris | last post by:
Hello! This is my problem. I'm trying to make an inorder traversal algorithm for a binary tree, not a recursive one, but using a stack. Till now I got this: void traverse(tree * p) { l:...
4
by: Anthony Liu | last post by:
There are many ways to represent a binary tree on an ascii screen. 1 / \ 2 3 / \ / \ 4 5 6 7 or
4
by: hankssong | last post by:
Hi everyone, I'm wondering whether it's possible to construct a binary-tree using python. Since python don't have pointer, it can't dynamically allocate memory like C language. But some important...
1
by: anarghya | last post by:
dear friends please tell me the construction of binary tree using linked list??
4
by: whitehatmiracle | last post by:
Hello I have written a program for a binary tree. On compiling one has to first chose option 1 and then delete or search. Im having some trouble with the balancing function. It seems to be going...
8
by: perdoname | last post by:
Hello! If i want to place the elements of an unsorted array of integers and add the elements to a binary tree then do i have to make use of heapsort?? and another thing how can i print all the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.