473,406 Members | 2,293 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,406 software developers and data experts.

Giving the preorder & inorder lists, How can be constructed the corresponding B-TREE ?

Hi,

If I have the preorder and inorder list, which algorithm does I need to
build the corresponding B-TREE? where can I find some source code?

thanks
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #1
10 2706
Is any chance that this list are _named_ in some another names. If not, can
you explain what is inorder/preorder list are? (I know that you expect
answer, but i post question !:)

Thanks,
Zaharije Pasalic

Nov 14 '05 #2
David Méndez wrote:
If I have the preorder and inorder list, which algorithm does I need
to build the corresponding B-TREE? where can I find some source code?


do your own homework.

--
Jason Whitehurst
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #3
In article <cl****************@plethora.net>,
David Méndez <da**********@hotmail.com> wrote:
Hi,

If I have the preorder and inorder list, which algorithm does I need to
build the corresponding B-TREE? where can I find some source code?


Here 'tis:
--------
#include <stdio.h>
#include <stdlib.h>

typedef struct n{struct n*p1,*p2;int v;}n;

n*reconstruct_tree(n*p,n*i)
{
n*in2,*t1,*t2;int v;if(!i)return i;
while(p->p2)p=p->p2;in2=i;
do{
t1=malloc(sizeof*t1);t2=malloc(sizeof*t2);
if(!t1||!t2)return 0;
t1->v=t2->v=in2->v;t2->p1=t2->p2=0;t1->p1=in2;t1->p2=t2;
if(t1->p1->p2)t1->p1->p2->p1=t1;if(t1->p1->p1)t1->p1->p1->p1->p2=t1;
in2=t1->p1->p2;
}while(in2);
i=t1;
while(i->p1->p1){
in2=i;v=in2->v;
while(p->v!=v&&p->p1->v!=v){
in2=in2->p1->p1;
if(!in2){puts("invalid input\n");return 0;}
v=in2->v;
}if(v==p->v){
if(p->p1){p=p->p1;free(p->p2);p->p2=0;}
if(in2->p1->p1)in2->p1->p1->p1->p2=in2->p1->p2;
if(in2->p1->p2)in2->p1->p2->p1->p1=in2->p1->p1;
else(i=in2);
t1=in2->p2;t2=in2->p1->p1;free(in2->p1);free(in2);
t2->p2->p2=t1;
}else{
t2=p->p1;
if(t2->p1)t2->p1->p2=t2->p2;t2->p2->p1=t1->p1;
free(t2);in2=in2->p1->p1;
if(in2->p1->p1)in2->p1->p1->p1->p2=in2->p1->p2;
in2->p1->p1->p1->p1=in2->p1->p1;
t1=in2->p2;t2=in2->p1->p2;free(in2->p1);free(in2);
t2->p2->p1=t1;}}
free(p);t1=i->p2;free(i->p1);free(i);
return t1;
}
--------

Be aware that I've deliberately introduced a single-character error.
Make sure you find and fix it before you hand it in.

If it's supposed to be C++ (I see you've crossposted there), there will
be more errors, but they should be easier to fix.
dave
(identify root, extract subtree traversals, recurse)

--
Dave Vandervies dj******@csclub.uwaterloo.ca
There once was a troller named Cass, Who lived in a house made of glass.
Every stone that he threw Showed how little he knew.
(Now what rhymes with "glass" and with "Cass?") --Keith Thompson in CLC
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #4
"David Méndez" <da**********@hotmail.com> wrote in message news:<cl****************@plethora.net>...
Hi,

If I have the preorder and inorder list, which algorithm does I need to
build the corresponding B-TREE? where can I find some source code?

thanks


Hint: The first element of the pre-order traversal list will be the root of
the tree. This same element will also be present somewhere in the
in-order traversal list. What can you say about the part of the
in-order list to the left of this element? What about the part to the
right?

Per
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #5
inorder/preorder list's: 2 List Data Structures where the nodes are
connected with a Binary Tree values in inorder and preorder respectively.

thank you.

"hari4063" <za**************@pmf.unsa.ba> escribió en el mensaje
news:76******************************@localhost.ta lkaboutprogramming.com...
Is any chance that this list are _named_ in some another names. If not, can you explain what is inorder/preorder list are? (I know that you expect
answer, but i post question !:)

Thanks,
Zaharije Pasalic

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.782 / Virus Database: 528 - Release Date: 2004-10-22
Nov 14 '05 #6
Thank you!!

C is ok.

David.

"Dave Vandervies" <dj******@csclub.uwaterloo.ca> escribió en el mensaje
news:cl****************@plethora.net...
In article <cl****************@plethora.net>,
David Méndez <da**********@hotmail.com> wrote:
Hi,

If I have the preorder and inorder list, which algorithm does I need to
build the corresponding B-TREE? where can I find some source code?


Here 'tis:
--------
#include <stdio.h>
#include <stdlib.h>

typedef struct n{struct n*p1,*p2;int v;}n;

n*reconstruct_tree(n*p,n*i)
{
n*in2,*t1,*t2;int v;if(!i)return i;
while(p->p2)p=p->p2;in2=i;
do{
t1=malloc(sizeof*t1);t2=malloc(sizeof*t2);
if(!t1||!t2)return 0;
t1->v=t2->v=in2->v;t2->p1=t2->p2=0;t1->p1=in2;t1->p2=t2;
if(t1->p1->p2)t1->p1->p2->p1=t1;if(t1->p1->p1)t1->p1->p1->p1->p2=t1;
in2=t1->p1->p2;
}while(in2);
i=t1;
while(i->p1->p1){
in2=i;v=in2->v;
while(p->v!=v&&p->p1->v!=v){
in2=in2->p1->p1;
if(!in2){puts("invalid input\n");return 0;}
v=in2->v;
}if(v==p->v){
if(p->p1){p=p->p1;free(p->p2);p->p2=0;}
if(in2->p1->p1)in2->p1->p1->p1->p2=in2->p1->p2;
if(in2->p1->p2)in2->p1->p2->p1->p1=in2->p1->p1;
else(i=in2);
t1=in2->p2;t2=in2->p1->p1;free(in2->p1);free(in2);
t2->p2->p2=t1;
}else{
t2=p->p1;
if(t2->p1)t2->p1->p2=t2->p2;t2->p2->p1=t1->p1;
free(t2);in2=in2->p1->p1;
if(in2->p1->p1)in2->p1->p1->p1->p2=in2->p1->p2;
in2->p1->p1->p1->p1=in2->p1->p1;
t1=in2->p2;t2=in2->p1->p2;free(in2->p1);free(in2);
t2->p2->p1=t1;}}
free(p);t1=i->p2;free(i->p1);free(i);
return t1;
}
--------

Be aware that I've deliberately introduced a single-character error.
Make sure you find and fix it before you hand it in.

If it's supposed to be C++ (I see you've crossposted there), there will
be more errors, but they should be easier to fix.
dave
(identify root, extract subtree traversals, recurse)

--
Dave Vandervies dj******@csclub.uwaterloo.ca
There once was a troller named Cass, Who lived in a house made of glass.
Every stone that he threw Showed how little he knew.
(Now what rhymes with "glass" and with "Cass?") --Keith Thompson in CLC
--
comp.lang.c.moderated - moderation address: cl**@plethora.net

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.782 / Virus Database: 528 - Release Date: 2004-10-22
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #7
well, I was expecting something like:

typedef struct tree{
int v;
struct tree *left, *right;
}BTREE, *BTREEPTR;

typedef struct list{
int v;
struct list *next
}TLIST, *TLISTPTR;

BTREE *reconstruct_tree(TLIST *p, TLIST *i)
{
...
}

I TAKE THE TIME TO IDENT THE CODE THAT YOU SEND ME, What you mean when you
use as parameters another 2 BTREE's ?:

n *reconstruct_tree(n *p, n *i)
{
n *in2, *t1, *t2;
int v;
if(!i)
return i;

while(p->p2)
p = p->p2;

in2 = i;
do
{
t1 = malloc(sizeof *t1);
t2 = malloc(sizeof *t2);

if(!t1 || !t2)
return NULL;

t1->v = t2->v = in2->v;
t2->p1 = t2->p2 = 0;
t1->p1 = in2;
t1->p2 = t2;

if(t1->p1->p2)
t1->p1->p2->p1 = t1;

if(t1->p1->p1)
t1->p1->p1->p1->p2 = t1;

in2 = t1->p1->p2;
}while(in2);

i = t1;
while(i->p1->p1)
{
in2 = i;
v = in2->v;
while(p->v != v && p->p1->v != v)
{
in2 = in2->p1->p1;
if(!in2)
{
puts("invalid input\n");
return NULL;
}
v = in2->v;
}

if(v == p->v)
{
if(p->p1)
{
p = p->p1;
free(p->p2);
p->p2 = 0;
}
if(in2->p1->p1)
in2->p1->p1->p1->p2=in2->p1->p2;

if(in2->p1->p2)
in2->p1->p2->p1->p1=in2->p1->p1;
else
(i = in2);

t1 = in2->p2;
t2 = in2->p1->p1;
free(in2->p1);
free(in2);
t2->p2->p2 = t1;
}
else
{
t2 = p->p1;

if(t2->p1)
t2->p1->p2 = t2->p2;

t2->p2->p1 = t1->p1;
free(t2);
in2 = in2->p1->p1;
if(in2->p1->p1)
in2->p1->p1->p1->p2 = in2->p1->p2;
in2->p1->p1->p1->p1 = in2->p1->p1;
t1 = in2->p2;
t2 = in2->p1->p2;
free(in2->p1);
free(in2);
t2->p2->p1 = t1;
}
}
free(p);
t1 = i->p2;
free(i->p1);
free(i);
return t1;
}

"Dave Vandervies" <dj******@csclub.uwaterloo.ca> escribió en el mensaje
news:cl****************@plethora.net...
In article <cl****************@plethora.net>,
David Méndez <da**********@hotmail.com> wrote:
Hi,

If I have the preorder and inorder list, which algorithm does I need to
build the corresponding B-TREE? where can I find some source code?


Here 'tis:
--------
#include <stdio.h>
#include <stdlib.h>

typedef struct n{struct n*p1,*p2;int v;}n;

n*reconstruct_tree(n*p,n*i)
{
n*in2,*t1,*t2;int v;if(!i)return i;
while(p->p2)p=p->p2;in2=i;
do{
t1=malloc(sizeof*t1);t2=malloc(sizeof*t2);
if(!t1||!t2)return 0;
t1->v=t2->v=in2->v;t2->p1=t2->p2=0;t1->p1=in2;t1->p2=t2;
if(t1->p1->p2)t1->p1->p2->p1=t1;if(t1->p1->p1)t1->p1->p1->p1->p2=t1;
in2=t1->p1->p2;
}while(in2);
i=t1;
while(i->p1->p1){
in2=i;v=in2->v;
while(p->v!=v&&p->p1->v!=v){
in2=in2->p1->p1;
if(!in2){puts("invalid input\n");return 0;}
v=in2->v;
}if(v==p->v){
if(p->p1){p=p->p1;free(p->p2);p->p2=0;}
if(in2->p1->p1)in2->p1->p1->p1->p2=in2->p1->p2;
if(in2->p1->p2)in2->p1->p2->p1->p1=in2->p1->p1;
else(i=in2);
t1=in2->p2;t2=in2->p1->p1;free(in2->p1);free(in2);
t2->p2->p2=t1;
}else{
t2=p->p1;
if(t2->p1)t2->p1->p2=t2->p2;t2->p2->p1=t1->p1;
free(t2);in2=in2->p1->p1;
if(in2->p1->p1)in2->p1->p1->p1->p2=in2->p1->p2;
in2->p1->p1->p1->p1=in2->p1->p1;
t1=in2->p2;t2=in2->p1->p2;free(in2->p1);free(in2);
t2->p2->p1=t1;}}
free(p);t1=i->p2;free(i->p1);free(i);
return t1;
}
--------

Be aware that I've deliberately introduced a single-character error.
Make sure you find and fix it before you hand it in.

If it's supposed to be C++ (I see you've crossposted there), there will
be more errors, but they should be easier to fix.
dave
(identify root, extract subtree traversals, recurse)

--
Dave Vandervies dj******@csclub.uwaterloo.ca
There once was a troller named Cass, Who lived in a house made of glass.
Every stone that he threw Showed how little he knew.
(Now what rhymes with "glass" and with "Cass?") --Keith Thompson in CLC
--
comp.lang.c.moderated - moderation address: cl**@plethora.net

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.782 / Virus Database: 528 - Release Date: 2004-10-22
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #8
"David Méndez" wrote:

I TAKE THE TIME TO IDENT THE CODE THAT YOU SEND ME, What you mean when you
use as parameters another 2 BTREE's ?:


Did you recognize that the code posted was a joke?
It was something that worked (actually I assume this, I
never tried the code), but was intentionally obfuscated
such that you cannot hand it in directly. It is a way
of saying: Do your own homework.
--
Karl Heinz Buchegger
kb******@gascad.at
Nov 14 '05 #9
In article <41***************@gascad.at>,
Karl Heinz Buchegger <kb******@gascad.at> wrote:
"David Méndez" wrote:

I TAKE THE TIME TO IDENT THE CODE THAT YOU SEND ME, What you mean when you
use as parameters another 2 BTREE's ?:
Did you recognize that the code posted was a joke?
It was something that worked (actually I assume this, I
never tried the code),


Well, there's two problems with it, actually.

The first problem is the deliberately-introduced single-character error I
mentioned; I figured that untangling the code enough to identify and fix
that should provide more enlightenment than coming up with the expected
solution to the exercise.

The second problem is actually an error in the algorithm that I missed
in my desk-check. (I didn't actually run the code, but did spend quite
a bit of time desk-checking it to make sure it correctly implemented the
algorithm; unfortunately I didn't notice the flaw in the algorithm until
after I had posted it.) The code that should remove the rightmost leaf
node from the preorder list in the case where that node is on the left
of its parent actually removes the parent, not the leaf. (I expect
that this will cause the code to break in obvious ways at runtime and
not just silently give the wrong answer.)

Your assignment, should you choose to accept it:
1. Identify the code with the logic error and replace it with correct
code.
2. Explain why the correct code is simpler than the incorrect code
it replaced.
3. Explain the algorithm used, and compare it with the "obvious"
top-down algorithm.

but was intentionally obfuscated
such that you cannot hand it in directly. It is a way
of saying: Do your own homework.


Yep, pretty much.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
I grant, of course, that our tasks would be much simpler if we could
order up a stock of ideal components.
--William Meyer in comp.lang.c
Nov 14 '05 #10
In article <cl****************@plethora.net>,
David Méndez <da**********@hotmail.com> wrote:
well, I was expecting something like:

typedef struct tree{
int v;
struct tree *left, *right;
}BTREE, *BTREEPTR;

typedef struct list{
int v;
struct list *next
}TLIST, *TLISTPTR;

BTREE *reconstruct_tree(TLIST *p, TLIST *i)
{
...
}

I TAKE THE TIME TO IDENT THE CODE THAT YOU SEND ME, What you mean when you
use as parameters another 2 BTREE's ?:
Read the code more carefully.
Note that where the pointers are pointing is more important than what
they're called in determining what data structure they represent.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca No, I did not look that up.

You should get out more, then.
--Garrett Wollman and Richard P. Grant in the scary devil monastery
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kris | last post by:
Hi All, I just tried to do something that I thought would be quite simple in C++ and discovered (I think) that it's not possible. I did a bunch of reading and everything that I've seen seems to...
24
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
1
by: Ioannis Hadjichambis | last post by:
Anyone knows where to get hash table and linked list implementation as a source code?
3
by: Wiktor Zychla [C# MVP] | last post by:
since generics allow us to implement several IEnumerable<T> interfaces on a single class, I think that "foreach" should somehow reflect that. suppose we have a enumerable class class C :...
10
by: Steve | last post by:
I need to build a very dynamic client and would be interested in knowing the pros and cons of using JSF and Ajax to accomplish this. Thanks. Steve
11
by: Hakusa | last post by:
Pythonic lists are everything I want in a one dimensional array . . . but I'm trying to do a text adventure and simplify the proces by creating a grid as opposed to a tedious list of rooms this...
4
by: bigbagy | last post by:
Notes The programs will be compiled and tested on the machine which runs the Linux operating system. V3.4 of the GNU C/C++ compiler (gcc ,g++) must be used. A significant amount coding is...
28
by: hlubenow | last post by:
Hello, I really like Perl and Python for their flexible lists like @a (Perl) and a (Python), where you can easily store lots of strings or even a whole text-file. Now I'm not a...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.