Preview

Binary Search Tree

Better Essays
Open Document
Open Document
1292 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Binary Search Tree
//Program – Binary Search Tree

#include<iostream> using namespace std; class node
{
public: int data; node *left, *right; node() { left=right=NULL; } node(int val) { left=right=NULL; data=val; }
};

class bst
{
private: node *root; void insertNode(node *&rootptr, node *pnew); void deleteNode(node *&root, int delval); int least(node *rootptr); int max(node *rootptr); void pre(node *rootptr); void post(node *rootptr); void in(node *rootptr); int countinternal(node *rootptr, int &count); void printTree(node *p, int level); int search(node *rootptr, int data);

public: bst(); void callsearch(int data); void insertBST(int datain); void deleteBST(int data); void displayin(); void displaypost(); void displaypre(); void count(); void print(); void leastele(); void maxele();
};

bst::bst()
{
root=NULL;
}

void bst::print()
{
printTree(root, 0);
};

void bst::callsearch(int data)
{
search(root, data);
}

int bst::search(node *rootptr, int data)
{
if(rootptr==NULL) { cout<<"Data not found"; return 0; } else if(rootptr->data==data) cout<<"Element found"<<endl; else if(data<rootptr->data) search(rootptr->left, data); else search(rootptr->right, data);
}

void bst::count()
{
int c=0; cout<<"The number of internal nodes is: "<<countinternal(root, c)<<endl;
}

int bst::countinternal(node *rootptr,int &count)
{
if(rootptr!=NULL) { countinternal(rootptr->left, count); if(rootptr->right!=NULL || rootptr->left!=NULL) count++; countinternal(rootptr->right, count); return count; }
}
void bst::printTree(node *p, int level)
{
if(p!=NULL) { printTree(p->right, level+1); for(int i=0;i<level;i++) cout<<" "; cout<<level<<":"<<p->data<<endl; printTree(p->left, level+1); }
};

You May Also Find These Documents Helpful

  • Good Essays

    ECET 370 Week 5 Lab 5

    • 650 Words
    • 3 Pages

    Exercise 1: Review of the Lecture Content Create a project using the ArrayList class and the Main class provided in DocSharing. The ArrayList class contains implementations of the first three search methods explained in this week's lecture: sequential, sorted, and binary search. The Main class uses these three methods. These programs test the code discussed in the lecture. Compile the project, run it, and review the code that is given carefully.…

    • 650 Words
    • 3 Pages
    Good Essays
  • Good Essays

    Nt1310 Unit 1 Test Paper

    • 381 Words
    • 2 Pages

    4. Create a delete function that searches the value in the tree, if it is present it deletes that value and return true else return false.…

    • 381 Words
    • 2 Pages
    Good Essays
  • Better Essays

    printf(" %d \t\t %d\n\n",stfor->wt,stfor->tt); ttl_wt += stfor->wt; ttl_tt += stfor->tt; temp = np = stfor; del_node(np,1) ; ++i; } else { stfor->bt -= qt; stfor->tt = temp->tt + qt; printf(" %d \t\t %d\n\n",stfor->wt,stfor->tt); np = temp = stfor; del_node(np,2); ins_node(temp); } }while(stfor !=…

    • 401 Words
    • 2 Pages
    Better Essays
  • Better Essays

    Families of the Forest

    • 2739 Words
    • 11 Pages

    This paper will be discussing the ethnography by Allen Johnson titled Families of the forest. The ethnography describes the Matsigenka people of Shimaa that live in the Peruvian Amazon. The paper will examine the Matsigenka culture, the needs and resources of the culture, and proposed projects to meet the needs of the culture.…

    • 2739 Words
    • 11 Pages
    Better Essays
  • Good Essays

    The Web Search market has been dominated by Google, forcing Microsoft and Yahoo to form an alliance in July 2009 to get a larger slice of the huge web search pie. The new Wolfram/Alpha Search engine, however, tries to compete with Google and other similar search engines by “researching” rather than just “searching” data. Stephen Wolfram, the new search engine creator gives an example; for a question like "How many Nobel Prize winners were born under a full moon?" Google would find the answer only if someone had previously gone through the whole list of Nobel Prize winners, matched the birthplace of each laureate with a table of lunar phases, and posted the results. Wolfram says his engine would have no problem doing this on the fly. "Alpha makes it easy for the typical person to answer anything quantitatively".…

    • 335 Words
    • 2 Pages
    Good Essays
  • Good Essays

    Kanarek, R. B. (2011). Artificial food dyes and attention deficit hyperactivity disorder. Nutrition Reviews, 69(7), 385-391. doi:10.1111/j.1753-4887.2011.00385.x…

    • 1293 Words
    • 4 Pages
    Good Essays
  • Good Essays

    Binary Code

    • 597 Words
    • 3 Pages

    Did you know that all of the information that travels through your computer is based on two commands? It's true. The only data that a computer can understand is on and off. But, the millions of combinations of those two commands given in series are what make a computer work.…

    • 597 Words
    • 3 Pages
    Good Essays
  • Good Essays

    By evaluating the duality of illustrations as both mechanisms for pictorial representation and the act of narrative clarification, the images in Shel Silverstein’s picture book The Giving Tree (1964) subsequently solidify as the dominant mode for communicating the text’s overall themes to the reader. In presenting the pattern of action of personification, and the recurring images of isolation and sacrifice, Silverstein’s illustrations actively portray one such theme of the destabilizing psychological and physiological effects of rape on the female body. As such, these trends ultimately elevate the overall meaning of the work by inherently presenting readers with a socio-political consideration of the destructive consequences of male desire…

    • 123 Words
    • 1 Page
    Good Essays
  • Good Essays

    Decision Tree

    • 404 Words
    • 2 Pages

    Everyone is eligible and there are sliding scale fees for those individuals with financial difficulties. Outpatient treatment requires detox to have happened before entering the program.…

    • 404 Words
    • 2 Pages
    Good Essays
  • Good Essays

    Database Search

    • 433 Words
    • 2 Pages

    The reason of this assignment is to explore and explain approaches in navigating databases, talk about the finding of the search process, closing with four relevant sources that related to the research questions, “What competence do counselors believe they have to incorporate spirituality into hospice care, and What course(s) are need for counselors to feel comfortable integrating spirituality in a therapeutic setting?”…

    • 433 Words
    • 2 Pages
    Good Essays
  • Powerful Essays

    Greedy In The Giving Tree

    • 585 Words
    • 3 Pages

    Most people take giving a brand new definition. You don't have to like what people give you just Think about why they gave it to you. In the book The Giving Tree the tree is being really kind to the boy and letting him take her stuff. The giving tree is all about giving and being greedy is not all about giving.…

    • 585 Words
    • 3 Pages
    Powerful Essays
  • Satisfactory Essays

    CS 220 – Programming w/ Data Structures: You have missed one assignment and one quiz. Your instructor has extended your assignment due date to this Sunday, April 10. Your instructor has also let you to take your Quiz # 2 during his office hours during this week. Let me know if you need additional support to study for this quiz. Your grade to date in this class is 30.2/37 81.62% B.…

    • 354 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    isearch

    • 999 Words
    • 3 Pages

    The other person I e-mailed was Grant Leum. I knew Brock Leum’s uncle was an engineer, but I didn’t realize was that Grant owns his own business. Leum Engineering makes ramps, risers, rail boards and engineered dock products. I also went online to an online discussion site and found some very interesting reviews from Jeff and Christopher on Cappex.…

    • 999 Words
    • 3 Pages
    Good Essays
  • Satisfactory Essays

    The Giving Tree is a children's picture book written and illustrated by Shel Silverstein. First published in 1964 by Harper & Row, it has become one of Silverstein's best known titles and has been translated into numerous languages.…

    • 116 Words
    • 1 Page
    Satisfactory Essays
  • Good Essays

    int function1(char x) { //parameter x passed to the function, function returns an integer value…

    • 1596 Words
    • 7 Pages
    Good Essays