473,386 Members | 1,823 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,386 software developers and data experts.

a function to verifies if the values are correctly ordered in the tree

momotaro
357 100+
am trying to implimente a function to check weither a function is bst or not this is my code PLZ HELP:

Expand|Select|Wrap|Line Numbers
  1. int CheckOrder(TreeNode *root){
  2. if (!root)
  3.    return TRUE;
  4. if (CheckOrder(root->left->entry) < root->entry &&
  5.     CheckOrder(root->right->entry) > root->entry)
  6.     CheckOrder(root->left);
  7.     CheckOrder(root->right);
  8.     return TRUE;
  9. else
  10.      return FALSE;
  11. }
May 6 '07 #1
3 1170
momotaro
357 100+
sorry this is the right one:

Expand|Select|Wrap|Line Numbers
  1. int CheckOrder(TreeNode *root){
  2. if (!root)
  3.    return TRUE;
  4. if (CheckOrder(root->left->entry) < root->entry &&
  5.     CheckOrder(root->right->entry) > root->entry){
  6.     CheckOrder(root->left);
  7.     CheckOrder(root->right);
  8.     return TRUE;
  9. }
  10. else
  11.      return FALSE;
  12. }
[/quote]
May 6 '07 #2
Not to split hairs, but aren't the code snippets identical? :)
The problem probably is that you're comparing the bool returned by CheckOrder with the value of root->entry. So: (i haven't tested this)

Expand|Select|Wrap|Line Numbers
  1. int CheckOrder(TreeNode *root){
  2. if (!root)
  3.    return TRUE;
  4. // in here you have to check for the existence of the left & right..
  5. //no simple and elegant way around it.
  6. if ( (root->left !=NULL && root->left->entry < root->entry) &&
  7.     (root->right!=NULL && root->right->entry) > root->entry) ) {
  8.     CheckOrder(root->left);
  9.     CheckOrder(root->right);
  10.     return TRUE;
  11. }
  12. else
  13.      return FALSE;
  14. }
May 7 '07 #3
JosAH
11,448 Expert 8TB
Not to split hairs, but aren't the code snippets identical? :)
The problem probably is that you're comparing the bool returned by CheckOrder with the value of root->entry. So: (i haven't tested this)

Expand|Select|Wrap|Line Numbers
  1. int CheckOrder(TreeNode *root){
  2. if (!root)
  3.    return TRUE;
  4. // in here you have to check for the existence of the left & right..
  5. //no simple and elegant way around it.
  6. if ( (root->left !=NULL && root->left->entry < root->entry) &&
  7.     (root->right!=NULL && root->right->entry) > root->entry) ) {
  8.     CheckOrder(root->left);
  9.     CheckOrder(root->right);
  10.     return TRUE;
  11. }
  12. else
  13.      return FALSE;
  14. }
The intention is right but the implementation is wrong ;-) Suppose a tree just
has a right sub-tree: that if-clause causes a wrong conclusion then. Even worse:
it ignores the return values of the recursive calls.

Better split the tests up like this:
Expand|Select|Wrap|Line Numbers
  1. if (!root || !root->left && !root->right) return TRUE; // empty of a leaf
  2.  
  3. if (root->left)
  4.    if (root->left->data <= root->data)
  5.       return Checkorder(root->left);
  6.    else 
  7.       return FALSE;
  8. else if (root->right->data >= root->data)
  9.    return Checkorder(root->right);
  10. else
  11.    return FALSE;
kind regards,

Jos
May 7 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jean-Christophe Michel | last post by:
Hi, In a complex merging of two (non ordered) xml files i need to keep track of the elements of the second tree that were already merged with first tree, to copy only unused elements at the end....
13
by: Dark Rayden | last post by:
Hi! I recently got a strange problem and I have no idea on the solution. I try to do a ORDER BY statement with a fixed order of values, because my client want's it this way. My approach is...
3
by: ian.proffer | last post by:
We have a 10 digit primary key value in this format: M000123456. The order for this key is first determined by positions 3 and 4 in this example, then positions 1 and 2. So a brief sample of...
16
by: mdh | last post by:
May I ask the group the following: (Again, alas , from K&R) This is part of a function: while ( ( array1 = array2 ) != '\0' ); /* etc etc */ Is this the order that this is evaluated? ...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
6
by: lawpoop | last post by:
Hello! I am working on a map of a rather large php project that I've been working on. I hope to create a map of the files of the project that would look like the output of the unix 'tree'...
2
by: bagerbeiger | last post by:
I am relatively new to programming (3-5 months) and have to write a program that prompts users for input (number of items ordered at a restaurant), performs some monetary calculations, and then...
6
by: meetharry19 | last post by:
i m trying to make a function which will calculate size of each node of an ordered statistic tree . here size refers to the number of nodes in left sub tree and right sub tree plus one... but...
0
by: MRAB | last post by:
On Sep 11, 4:48 pm, Manuel Ebert <maeb...@uos.dewrote: I don't know what the "len(a)" is, but the end position defaults to the end:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.