Solved

Trees on the brain

Posted on 1998-02-11
3
231 Views
Last Modified: 2010-04-16
I need to write a procedure to build a binary tree using arrays where the data item is string[10]. I also need a procedure to build a binary tree  using pointers where the data item is string[10]. Any suggestions / algorithms?
0
Comment
Question by:chadd082197
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Expert Comment

by:inter
ID: 1217276
I do not understand the bintree with arrays but with pointers here is the idea:

type
  PNode = ^TNode;
  TNode = record
   data : string[10];
   L,R  : PNode; // left and right childs of node
  end;

var
  Root : TNode; //the root node for our tree

Now, please list what methods you want for this tree. e.g. adding item, removing item, balancing the tree etc...

Igor
0
 
LVL 2

Expert Comment

by:mitchell042997
ID: 1217277
You can use an array if you plan on having a balanced tree.  For example, if you want the tree:

....1
.../.\
..2...3
./\.../\
4.5..6..7

Then you can have an array like this:

1 | 2 | 3 | 4 | 5 | 6 | 7

To find any node's left child, multiply it by 2.  To find it's right child, multiply it by two and add 1.  If you have an unchanging, balaced array that you need to perform a lot of searches on, this way is quicker than using pointers.


0
 
LVL 1

Accepted Solution

by:
santhosh022698 earned 100 total points
ID: 1217278
binary tree using pointers :
type node = ^item ; item = record  data : string[10]; l,r : node; end;
the function for insertion is
function insert (root : node, toinsert : string):node ;
begin  if root=nil then begin root^.data := toinsert ; root^.l := nil; root^.r := nil; end
else if root^.data > toinsert then root^.left := insert(root^.left, toinsert)
else root^.right := insert (root^.right, toinsert);
insert := root ; end ;
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft is moving in-place eDiscovery & hold from ECP to EOP console under Content Search in Search and Investigation Options.  In this post, I will be showing you how to export emails to a PST file using the Content Search Options.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question