Thread: Tree Search

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    112

    Tree Search

    I am working on A tree search funtion. Currently I am just trying to see whether its working or not. Like I have added data in root node and now i am comparing it with the input. But the If condition of program higlighted below doesnt work.
    Code:
    void treeSearch(char a[24])
    	{
    		Node* temp;
    		temp = root;
    		bool flag = false;
    		char pointer[24];
    		strcpy(pointer,a);
    		for (int i = 0; i < 24; i++)
    		cout<<pointer[i];
    
    		//check to check the root
    		for (int i = 0; i < 24; i++ )
    		{
    			if (strcmp(temp->data,pointer) == 0)			{
    				cout<<"Found";
    				flag = true;
    				return;
    			}
    		}
    
    		if (flag == true)
    			cout<<"FOUND";
    		return;

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The first thing I would do is ponder on whether you want to write C or C++ code. This is all C code with the additional of cout which is C++. The only C++ part of the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Trying adding this line:
    Code:
    		for (int i = 0; i < 24; i++ )
    		{
    cout << "-->" << temp->data << "<--\t-->" << pointer << "<--\n";
    			if (strcmp(temp->data,pointer) == 0)			{
    				cout<<"Found";
    				flag = true;
    				return;
    			}
    		}
    I usually leave temporary debugging stuff like that unindented so it does not get forgotten.

    Guess: one of these has a \n on and the other one doesn't. Altho an odd thing about this loop is it compares the EXACT SAME TWO things 24 times.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What benefit is there to performing the same strcmp 24 times on the root node?
    Oh drat someone already mentioned that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    2
    Why is this block needed anyway?
    Code:
    if (flag == true)
    			cout<<"FOUND";
    		return;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM