Binary Search tree Inorder tree display

I am using two pointers in BST root and top. top pointer moves along the tree for insertion and display. Now my Question is that in my inorder code. i move the top pointer down the tree till the time NULL is found through recursion as follows

void display() { if(top->left!=NULL) { top=top->left; display(); } cout<<"\n\nnow the address of top is "<<top; return; }

elements in the tree are for the time being 12 11 and 10; now in the last recursive call of the fuction, top pointer moves at 10 but how to move the top pointer back after the last function i mean ; in every cout the adress of top is of 10 means that the pointer remains at 10 even if the control comes back to a previous recursive function. there are two recursive functions in this code, display is called tWo times, after the second display function the top pointer doesnt move up the tree rather it remains on 10, i want to move the top pointer back after the completion of every recursive display function. help. BUT WITHOUT INVOLVING ARGUMENTS IN DISPLAY FUNCTION.


In this sample, your code appears to be stepping down the left side of each tree (but ignoring the right side).


Generally, a BST has nodes, each node has data, a ptr to left child, and a ptr to right child.

And it appears that your nodes do not yet contain a "display()" method.

In a tree, I suspect you want to be invoking

<a node> -> display();

To avoid passing pointers in your recursive 'display' calls.

Next, your names are misleading (to me). So perhaps if you rename some things, the following might be appropriate and more understandable to you.

Each node needs at least 3 parts, and the display method. For a depth-first-search, in-line display maybe the following will trigger some thoughts.

// first, display nodes of left side-of-tree
if(nullptr != left)
// else no left node available

// for in-order, cout the data at this node
std::cout << data << std::endl;

// continue the depth-first display by tracing the right side
if (nullptr != right)

To begin the the display track, you need to have an anchor of the tree. Perhaps something like:

if(tree.Top) // is tree not empty?

So ... I am suggesting a class for tree, another class for node, a display method for tree, a display method for node, etc.

Hope this helps ... Good luck.

Category:c# Time:2017-12-05 Views:1

Related post

  • Binary search through quad-tree 2011-04-03

    I have a quad tree where the leaf nodes represent pixels. There is a method that prunes the quad tree, and another method that calculates the number of leaves that would remain if the tree were to be pruned. The prune method accepts an integer tolera

  • How do you display a binary search tree? 2010-06-04

    I'm being asked to display a binary search tree in sorted order. The nodes of the tree contain strings. I'm not exactly sure what the best way is to attack this problem. Should I be traversing the tree and displaying as I go? Should I flatten the tre

  • How do I implement a display function for a binary search tree? 2010-12-18

    How do I implement a display function for a binary search tree of strings if display is passed as a function pointer to function inorder? typedef void (*FunctionType)(TreeItemType& anItem); void display(TreeItemType& anItem); void BinarySearc

  • need help understanding inorder successor in Binary Search Tree 2011-03-06

    I need help in understanding this interview question: Q: find an algorithm to find the ‘next’ node (e.g., in-order successor) of a given node in a binary search tree where each node has a link to its parent. Does parent mean the in-order predecessor

  • Binary Search Tree displaying nodes recursively 2012-04-27

    I'm getting a problem with my display functions dealing with binary search trees. The main problem I'm having is getting the count variable to increment correctly in the recursive function so that I can number the nodes in ascending order. Would maki

  • Binary Search Tree Display issue 2014-11-09

    I have a lab assignment that I have been working on for the last couple of weeks and I am stuck and desperately need help as this will be about 50% of the final project. The assignment is to create a Binary Search Tree in C++. We have to read in the

  • javascript binary search tree implementation 2009-08-25

    Anyone know of any good examples of a simple BTree implementation in Javascript? I have a bunch of "things" arriving randomly, and want to insert each one efficiently. Ultimately, each new one will get inserted into the DOM based on where it ends up

  • Two questions on Binary Search Trees 2011-04-06

    I have two questions about binary search trees - one about the code I am writing and the other more about theory. First of all, the code that I wrote below works fine except for when I try to display the case where the BST is actually empty; it gives

  • MIPS - implementing a binary search tree 2012-03-06

    As our term project, we're implementing a binary search tree. The thought behind it is as follows: Assume a bst with 3 nodes: 10 / \ / \ 8 14 Its address representation is as follows (value, left node address, right node address, root node address)t:

  • Populating a binary search tree with a text file 2012-03-12

    I'm trying to fill in a binary search tree with a text file, but i'm having alot of trouble implementing my insert function. Am i reading the input correctly or is it my code? Code for reading file: import*; import java.util.Scanner; public

  • How to balance an existing random binary search tree (BST) using an array (in Java)? 2012-03-21

    I got the following assignment - I have a certain java code for a binary search tree and I need to add methods to do the following things with it: Transform the BST into an array that's sorted by BST data keys. Create a balanced BST from an ordered i

  • need access to Class Object via Function Pointer - Binary Search Tree Class Creation Related 2008-10-15

    Creating Traversals for Binary Search Tree with Recursion. void inOrder(void (*inOrderPtr)(T&)) { if(this->left != NULL) inOrder((*inOrderPtr)(this->left)); inOrderPtr(this->data); if(this->right != NULL) inOrder((*inOrderPtr)(this-

  • How do you validate a binary search tree? 2009-02-01

    I read on here of an exercise in interviews known as validating a binary search tree. How exactly does this work? What would one be looking for in validating a binary search tree? I have written a basic search tree, but never heard of this concept. -

  • How to properly insert/delete in a binary search tree in C? 2009-03-24

    I kinda have to put my previous C questions on hold cause this one is more important now... I have already coded the insert and delete functions on my binary search tree but the delete function is incomplete. There's a couple of things I need help in

  • Java Generic Binary Search Tree type issue 2009-05-02

    I'm working on this homework that's kind of confusing me... I am provided with the following BinarySearchTree class import java.util.NoSuchElementException; /** * * @param <T> The type of data stored in the nodes of the tree, must implement Com

  • Sorting a binary search tree on different key value 2009-08-13

    Say I have a binary tree with the following definition for a node. struct node { int key1 ; int key2 ; } The binary search tree is created on the basis of key1. Now is it possible to rearrange the binary search tree on basis of key2 in O(1) space. Al

  • Binary Search Tree C++ 2009-11-19

    Im a little confused. Im wondering if an array based Binary Search tree is implemented this way? void BST::insert(item &items, const data & aData ) {//helper function. Parent++; data *new_data = new data(aData); this->insert(*new_data); }

  • Delete multiple nodes from a binary search tree 2009-12-17

    I have a binary search tree created in C. The problem is I can't find a efficient way to delete all the nodes with e.g., id>5. When I traverse the tree, if I delete a node, the recursion is getting error, because the structure is not the same. Is

  • Find kth smallest element in a binary search tree in Optimum way 2010-02-24

    I need to find the kth smallest element in the binary search tree without using any static/global variable. How to achieve it efficiently? The solution that I have in my mind is doing the operation in O(n), the worst case since I am planning to do an

  • How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space 2010-04-05

    How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space. --------------Solutions------------- You don't give much to go on, but if the requirement is what I think it is, you have a binary tree already created

Copyright (C), All Rights Reserved.

processed in 1.031 (s). 13 q(s)