answersLogoWhite

0

//Start file TreeNode.h

#ifndef TREE_NODE

#define TREE_NODE

#include <iostream>

using namespace std;

template <class T>

class TreeNode

{

private:

//variables

TreeNode<T> *leftNode, *rightNode;

T value;

public:

//function prototypes

TreeNode(T Value);

T getValue();

void setValue(T newValue);

friend ostream& operator<< <>(ostream& output, const TreeNode<T>& tN);

template <class T> friend class BinaryTree;

};

template <class T>

TreeNode<T>::TreeNode(T Value)

{

value = Value;

leftNode = NULL;

rightNode = NULL;

}

template <class T>

T TreeNode<T>::getValue()

{

return value;

}

template <class T>

void TreeNode<T>::setValue(T newValue)

{

value = newValue;

}

template <class T>

ostream& operator<<(ostream& output, const TreeNode<T>& tN)

{

if (tN.leftNode != NULL)

output << *(tN.leftNode);

output << tN.value << endl;

if (tN.rightNode != NULL)

output << *(tN.rightNode);

return output;

}

#endif

----------------------------------------------------------------

//Start file BinaryTree.h

#ifndef BINARY_TREE

#define BINARY_TREE

#include "TreeNode.h"

#include <iostream>

using namespace std;

template <class T>

class BinaryTree

{

private:

TreeNode<T> *firstNode;

public:

BinaryTree();

friend ostream& operator<< <>(ostream& output, const BinaryTree<T>& bT);

void add(T value);

void remove(T value);

TreeNode<T>* findNode(T value);

};

template <class T>

ostream& operator<<(ostream& output, const BinaryTree<T>& bT)

{

if (bT.firstNode NULL) //the smallest node has no children

*pointerToSmallestNode = NULL; //set the pointer that pointed to the smallest node that was just deleted to NULL

else //the smallest node has a child on the right

*pointerToSmallestNode = smallestNode->rightNode; //replace the smallest node with its right hand child

delete smallestNode; //delete the smallest node since its value was moved and its reference pointer was changed

}

}

else if (value < p->getValue())

{

pointerToNode = &(p->leftNode);

p = p->leftNode;

}

else //value is greater than p->getValue()

{

pointerToNode = &(p->rightNode);

p = p->rightNode;

}

}

}

#endif

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

How do you print all data in a Binary Search Tree?

By using Depth First Search or Breadth First search Tree traversal algorithm we can print data in Binary search tree.


Give you the algorithm of creating a new binary search tree using c?

i want to know how to give the algorithm password in a computer ?


Does binary tree and binary search tree same?

no they are not same


How can you merge two binary search trees into a single binary search tree?

To merge two binary search trees into a single binary search tree, you can perform an in-order traversal on each tree to extract their elements, combine the elements into a single sorted list, and then construct a new binary search tree from the sorted list. This process ensures that the resulting tree maintains the binary search tree property.


What is complexity of binary search tree?

The complexity of binary search tree : Search , Insertion and Deletion is O(h) . and the Height can be of O(n) ( if the tree is a skew tree). For Balanced Binary Trees , the Order is O(log n).


Complexity of an algorithm in data structure?

* search array =&gt; O(1) linked list=&gt; O(n) binary tree=&gt; O(log n) hash=&gt;O(1) * search array =&gt; O(1) linked list=&gt; O(n) binary tree=&gt; O(log n) hash=&gt;O(1)


Is an AVL tree a binary search tree (BST)?

Yes, an AVL tree is a type of binary search tree (BST) that is balanced to ensure efficient searching and insertion operations.


What is the different between a binary search tree and a binary tree?

self depend friend"s............


How do you calculate the height of a binary tree?

To calculate the height of a binary tree, you can use a recursive algorithm that traverses the tree and keeps track of the height at each level. The height of a binary tree is the maximum depth of the tree, which is the longest path from the root to a leaf node.


Is sorting a binary search tree simple?

A binary search tree is already ordered. An in order traversal will give you a sorted list of nodes.


What is the time complexity of searching a binary search tree?

The time complexity of searching a binary search tree is O(log n), where n is the number of nodes in the tree.


What is the process of traversing a binary tree in a depth-first manner using the depth-first search algorithm?

Traversing a binary tree in a depth-first manner using the depth-first search algorithm involves visiting each node's children before moving on to the next level. This is done by starting at the root node, then recursively visiting the left child, then the right child, and continuing this pattern until all nodes have been visited.