473,789 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.mod erated - moderation address: cl**@plethora.n et
Nov 14 '05 #1
10 2743
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.mod erated - moderation address: cl**@plethora.n et
Nov 14 '05 #3
In article <cl************ ****@plethora.n et>,
David Méndez <da**********@h otmail.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_t ree(n*p,n*i)
{
n*in2,*t1,*t2;i nt v;if(!i)return i;
while(p->p2)p=p->p2;in2=i;
do{
t1=malloc(sizeo f*t1);t2=malloc (sizeof*t2);
if(!t1||!t2)ret urn 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");retur n 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=in 2->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.mod erated - moderation address: cl**@plethora.n et
Nov 14 '05 #4
"David Méndez" <da**********@h otmail.com> wrote in message news:<cl******* *********@pleth ora.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.mod erated - moderation address: cl**@plethora.n et
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******** *************** *******@localho st.talkaboutpro gramming.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******@csclu b.uwaterloo.ca> escribió en el mensaje
news:cl******** ********@pletho ra.net...
In article <cl************ ****@plethora.n et>,
David Méndez <da**********@h otmail.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_t ree(n*p,n*i)
{
n*in2,*t1,*t2;i nt v;if(!i)return i;
while(p->p2)p=p->p2;in2=i;
do{
t1=malloc(sizeo f*t1);t2=malloc (sizeof*t2);
if(!t1||!t2)ret urn 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");retur n 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=in 2->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.mod erated - moderation address: cl**@plethora.n et

---
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.mod erated - moderation address: cl**@plethora.n et
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_tr ee(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_tr ee(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******@csclu b.uwaterloo.ca> escribió en el mensaje
news:cl******** ********@pletho ra.net...
In article <cl************ ****@plethora.n et>,
David Méndez <da**********@h otmail.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_t ree(n*p,n*i)
{
n*in2,*t1,*t2;i nt v;if(!i)return i;
while(p->p2)p=p->p2;in2=i;
do{
t1=malloc(sizeo f*t1);t2=malloc (sizeof*t2);
if(!t1||!t2)ret urn 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");retur n 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=in 2->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.mod erated - moderation address: cl**@plethora.n et

---
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.mod erated - moderation address: cl**@plethora.n et
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******@gasca d.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

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

Similar topics

2
10557
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 indicate that it's not possible but I thought I'd throw it out to all you c++ gurus to see what you say. Is there any way to initialize a character array in the initialization list for the constructor a class? If this is not possible, can anyone...
24
3968
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 = s2 = s3 = for value in ???(s1, s2, s3):
1
1849
by: Ioannis Hadjichambis | last post by:
Anyone knows where to get hash table and linked list implementation as a source code?
3
1663
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 : IEnumerable, IEnumerable<int>, IEnumerable<string> { ... we are allowed to enumerate IEnumerable: foreach ( object item in o ) ...
10
6320
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
1527
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 room connects to. So I want to know a good way to do a SIMPLE two dimensional array. Python's lists are a little confusing when it comes to how to do this. I realized that I could do list = * N ] * N
4
8442
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 needed for this assignment. 1.Counting Words in C++ Problem: Write an elegant C++ program to count the number of times each word occurs in a file. A word is defined as a sequence of characters surrounded by whitespace. Case is not important, so...
28
6064
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 C-professional, just a hobby-programmer trying to teach it myself. I found C rather difficult without those lists (and corresponding string-functions). Slowly getting into C-strings, I thought, what if I take a C-string and
30
3856
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 the end of the app for example: class test { public: int x;
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10410
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9984
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.