473,795 Members | 3,041 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary Search Tree Help Needed

hi all;

I have an array and want to insert all the elements from this array to
a binary search tree.That array is an object of the class of a
stringtype which includes overloaded "< > = ==" functions and every
other thing it needs.

void InsertElementFr omArray(StringT ype Array,int first element,int
last element);

that member function takes the array,first element of the array,last
element of the array as parameters.Arra y is sorted array.this
application is going to be used as spellchecker.if you supply any info
I will be glad.

Aug 5 '05 #1
3 2853
tsunami wrote:
hi all;

I have an array and want to insert all the elements from this array to
a binary search tree.That array is an object of the class of a
stringtype which includes overloaded "< > = ==" functions and every
other thing it needs.

void InsertElementFr omArray(StringT ype Array,int first element,int
last element);

that member function takes the array,first element of the array,last
element of the array as parameters.Arra y is sorted array.this
application is going to be used as spellchecker.if you supply any info
I will be glad.


I'm not entirely clear what you're asking for. You can use std::set (or
any of its cousins map, multiset, or multimap) as a sorted container
which supports O(log n) searches, inserts, removals, and more.
Typically these are implemented with binary search trees (every
implementation I've seen uses red-black trees though this is not
mandatory).

Of course if you already have a sorted array and don't intend to add or
remove elements from it, it's pretty easy to perform O(log n) searches
on that directly.

But if you're making a spell-checker a BST may not be ideal. If you
only want to check if a word is in a fixed dictionary then a hash table
is probably better. If you need to suggest lexically similar words then
a hash table is no good, but a simple sorted structure probably won't
perform very well at this task either.
Aug 6 '05 #2
"tsunami" writes:
I have an array and want to insert all the elements from this array to
a binary search tree.That array is an object of the class of a
stringtype which includes overloaded "< > = ==" functions and every
other thing it needs.

void InsertElementFr omArray(StringT ype Array,int first element,int
last element);

that member function takes the array,first element of the array,last
element of the array as parameters.Arra y is sorted array.this
application is going to be used as spellchecker.if you supply any info
I will be glad.


The only problem I see is that the array is sorted; putting a sorted array
into a tree will give the worst possible tree. I assume you are not
supposed to change the input array. If the array contains n elements I
would create an auxiliary array with elements numbered 0..n-1. Randomize
this array ( see the shuffle algorithm as used in a card game) and use the
auxiliary array to access the original array. The tree will see the
elements arriving in random order.
Aug 6 '05 #3
osmium wrote:
"tsunami" writes:

I have an array and want to insert all the elements from this array to
a binary search tree.That array is an object of the class of a
stringtype which includes overloaded "< > = ==" functions and every
other thing it needs.

void InsertElementFr omArray(StringT ype Array,int first element,int
last element);

that member function takes the array,first element of the array,last
element of the array as parameters.Arra y is sorted array.this
application is going to be used as spellchecker.if you supply any info
I will be glad.

The only problem I see is that the array is sorted; putting a sorted array
into a tree will give the worst possible tree. I assume you are not
supposed to change the input array. If the array contains n elements I
would create an auxiliary array with elements numbered 0..n-1. Randomize
this array ( see the shuffle algorithm as used in a card game) and use the
auxiliary array to access the original array. The tree will see the
elements arriving in random order.


Why throw away the sort information and waste time randomizing when you
can build a balanced binary tree very easily from a sorted array? There
are a variety of ways to do this, but they all follow the general
strategy of breaking the list in half and recursively building a tree
from each half.

Really this isn't even necessary, except perhaps for conceptual
understanding, since searching in a sorted array is already as easy as
searching in a balanced binary tree.
Aug 6 '05 #4

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

Similar topics

11
2533
by: jova | last post by:
Is there a difference between a Binary Tree and a Binary Search Tree? If so can someone please explain. Thank You.
0
4256
by: j | last post by:
Hi, Anyone out there with binary search tree experience. Working on a project due tomorrow and really stuck. We need a function that splits a binary tree into a bigger one and smaller one(for a random binary search tree. We've tried everything but are in the land of pointer hell. If someone could help it would be a huge help. our code follows. We've tried 2 diff methods the split() and splitter() functions #include <iostream> #include...
4
9021
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working, even the removal, I just don't know how to keep track of the parent, so that I can set its child to the child of the node to be removed. IE - if I had C / \ B D
15
5101
by: Foodbank | last post by:
Hi all, I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked lists. I'm totally new to binary searches by the way. Can anyone help me with the commented sections below? Much of the code such as functions and printfs has already been completed. Any help is greatly appreciated. Thanks,
1
2952
by: hn.ft.pris | last post by:
I have the following code: Tree.h defines a simple binary search tree node structure ########## FILE Tree.h ################ #ifndef TREE_H #define TREE_H //using namespace std; template <typename Tclass Tree{ private: Tree<T*left;
4
11014
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 into an infinite loop, i think im messing up with the pointers. Heres the code. //press 1, first, Do not press it a second time!!!
11
1191
by: Defected | last post by:
Hi, How i can create a Binary Search Tree with a class ? thanks
2
2602
by: Defected | last post by:
Hi, How i can implement a main function with this Binary Search Tree. thanks for help. is this code corrected ? #include<iostream>
7
3875
by: Vinodh | last post by:
Started reading about Binary Trees and got the following questions in mind. Please help. Definition of a Binary Tree from "Data Structures using C and C++ by Tanenbaum" goes like this, "A binary tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets. The first subset contains a single element called the 'Root' of the tree. The other two subsets are themselves binary trees, called the 'Left'...
0
9672
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
9519
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,...
1
10164
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
10001
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...
1
7540
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
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
5437
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
4113
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
3
2920
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.