473,803 Members | 3,619 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 2747
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
10558
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
3969
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
1850
by: Ioannis Hadjichambis | last post by:
Anyone knows where to get hash table and linked list implementation as a source code?
3
1664
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
1530
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
8443
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
6067
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
3857
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
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9566
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
10555
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
10300
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
9127
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5503
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...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
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.