answersLogoWhite

0


Best Answer

#include<iostream>

template<typename T>

class bin_tree

{

class node

{

T data;

node* left {nullptr};

node* right {nullptr};

public:

node (const T&);

node (const node&);

node (node&&);

node& operator= (const node&);

node& operator= (node&&);

~node ();

void insert (const T&);

bool exists (const T&) const;

std::ostream& depth_first_print (std::ostream&) const;

};

node* root {nullptr};

public:

bin_tree ();

bin_tree (const T[], const size_t);

bin_tree (const bin_tree&);

bin_tree (bin_tree&&);

bin_tree& operator= (const bin_tree&);

bin_tree& operator= (bin_tree&&);

~bin_tree ();

void clear ();

void insert (const T&);

bool exists (const T&) const;

std::ostream& depth_first_print (std::ostream&) const;

};

// node output stream insertion operator (depth-first traversal)

template<typename T>

std::ostream& operator<< (std::ostream& os, typename const bin_tree<T>& t)

{

return t.depth_first_print (os);

}

// node default/conversion constructor (construct a node from a type T)

template<typename T>

bin_tree<T>::node::node (const T& value)

: data {value}

{}

// node copy constructor

template<typename T>

bin_tree<T>::node::node (typename const bin_tree<T>::node& rhs)

: data {rhs.data}

, left {rhs.left ? new node {*rhs.left} : nullptr}

, right {rhs.right ? new node {*rhs.right} : nullptr}

{}

// node move constructor

template<typename T>

bin_tree<T>::node::node (typename bin_tree<T>::node&& rhs)

: data {std::move(rhs.data)}

, left {std::move(rhs.left)}

, right {std::move(rhs.right)}

{

rhs.left = nullptr;

rhs.right = nullptr;

}

// node copy assignement operator overload

template<typename T>

typename bin_tree<T>::node& bin_tree<T>::node::operator= (typename const bin_tree<T>::node& rhs)

{

delete left;

delete right;

data = rhs.data;

left = rhs.left ? new node (*rhs.left) : nullptr;

right = rhs.right ? new node (*rhs.right) : nullptr;

}

// node move assignment operator overload

template<typename T>

typename bin_tree<T>::node& bin_tree<T>::node::operator= (typename bin_tree<T>::node&& rhs)

{

delete left;

delete right;

data = std::move (rhs.data);

left = rhs.left;

right = rhs.right;

rhs.left = nullptr;

rhs.right = nullptr;

}

// node destructor

template<typename T>

bin_tree<T>::node::~node ()

{

delete left;

delete right;

}

// node print method (depth first traversal)

template<typename T>

std::ostream& bin_tree<T>::node::depth_first_print(std::ostream& os) const

{

if (left) left->depth_first_print (os);

os << data << ' ';

if (right) right->depth_first_print (os);

return os;

}

// node exists method

template<typename T>

bool bin_tree<T>::node::exists (const T& value) const

{

return value == data ? true :

value < data && left ? left->exists (value) :

value > data && right ? right->exists (value) :

false;

}

// node insertion method

template<typename T>

void bin_tree<T>::node::insert (const T& value)

{

if (value < data)

{

if (left)

left->insert (value);

else

left = new node {value};

}

else

{

if (right)

right->insert (value);

else

right = new node {value};

}

}

// tree default constructor (construct an empty tree)

template<typename T>

bin_tree<T>::bin_tree ()

{}

// tree overloaded constructor (construct a tree from an array)

template<typename T>

bin_tree<T>::bin_tree (const T arr[], const size_t size)

{

for (size_t index=0; index<size; ++index)

insert (arr[index]);

}

// tree copy constructor

template<typename T>

bin_tree<T>::bin_tree (const bin_tree<T>& rhs)

{

*this = rhs;

}

// tree move constructor

template<typename T>

bin_tree<T>::bin_tree (bin_tree&& rhs): root{rhs.root}

{

rhs.root=nullptr;

}

// tree copy assignment operator overload

template<typename T>

bin_tree<T>& bin_tree<T>::operator= (const bin_tree<T>& rhs)

{

if (root)

delete root;

if (rhs.root)

{

root = new node {*rhs.root};

}

return *this;

}

// tree move assignment operator overload

template<typename T>

bin_tree<T>& bin_tree<T>::operator= (bin_tree&& rhs)

{

if (root)

delete root;

root=rhs.root;

rhs.root=nullptr;

return *this;

}

// tree destructor

template<typename T>

bin_tree<T>::~bin_tree ()

{

clear();

}

// tree print method (depth first traversal)

template<typename T>

std::ostream& bin_tree<T>::depth_first_print(std::ostream& os) const

{

return root ? root->depth_first_print (os) : os << "{empty}";

}

// tree clear method

template<typename T>

void bin_tree<T>::clear ()

{

delete root;

root = nullptr;

}

// tree insertion method

template<typename T>

void bin_tree<T>::insert (const T& value)

{

if (!root)

root = new node {value};

else

root->insert (value);

}

// tree exists method

template<typename T>

bool bin_tree<T>::exists (const T& value) const

{

return root ? root->exists (value) : false;

}

int main()

{

int arr[10] = {42, 56, 30, 5, 12, 60, 40, 8, 1, 99};

std::cout << "Test default constructor:\n";

bin_tree<int> a {}; // {} is implied if omitted

std::cout << "a: " << a << std::endl;

std::cout << "\nTest overloaded constructor:\n";

bin_tree<int> b {arr, 10};

std::cout << "b: " << b << std::endl;

std::cout << "\nTest tree copy constructor (c{b}):\n";

bin_tree<int> c {b};

std::cout << "b: " << b << std::endl;

std::cout << "c: " << c << std::endl;

std::cout << "\nTest tree move constructor (d{move(b)}):\n";

bin_tree<int> d {std::move(b)};

std::cout << "b: " << b << std::endl;

std::cout << "c: " << c << std::endl;

std::cout << "d: " << d << std::endl;

std::cout << "\nTest insertion into c:\n";

c.insert (41);

std::cout << "b: " << b << std::endl;

std::cout << "c: " << c << std::endl;

std::cout << "d: " << d << std::endl;

std::cout << "\nTest search:\n";

if (c.exists (41))

std::cout << "Value 41 exists in c" << std::endl;

else

std::cout << "Value 41 does not exist in c" << std::endl;

if (d.exists (41))

std::cout << "Value 41 exists in d" << std::endl;

else

std::cout << "Value 41 does not exist in d" << std::endl;

std::cout << "\nTest copy assignment (c=d):\n";

c = d;

std::cout << "b: " << b << std::endl;

std::cout << "c: " << c << std::endl;

std::cout << "d: " << d << std::endl;

std::cout << "\nTest move assignment (d=move(c)):\n";

d = std::move (c);

std::cout << "b: " << b << std::endl;

std::cout << "c: " << c << std::endl;

std::cout << "d: " << d << std::endl;

std::cout << "\nTest clear method (d.clear()):\n";

d.clear();

std::cout << "b: " << b << std::endl;

std::cout << "c: " << c << std::endl;

std::cout << "d: " << d << std::endl;

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

#include<iostream>

#include<time.h>

template<typename T>

class BinTree {

private:

template<typename T>

struct Node {

Node* m_left;

Node* m_right;

T m_data;

Node (const T& data): m_left (nullptr), m_right (nullptr), m_data (data) {}

~Node() { delete m_right; delete m_left; }

void print () const {

if (m_left)

m_left->print();

std::cout << m_data << std::endl;

if (m_right)

m_right->print();

}

Node* search (const T& data) {

if (data==m_data)

return this;

if (data<m_data) {

if (m_left)

return m_left->search(data);

} else {

if (m_right)

return m_right->search(data);

}

return nullptr;

}

Node* insert (const T& data) {

if (data<m_data) {

if (m_left)

return m_left->insert(data);

return m_left = new Node<T>(data);

} else {

if (m_right)

return m_right->insert(data);

return m_right = new Node<T>(data);

}

return nullptr;

}

};

Node<T>* m_root;

size_t m_size; // count of nodes

public:

BinTree (): m_root (nullptr) {}

~BinTree () { delete m_root; }

void print () { if (m_root) m_root->print(); }

Node<T>* search (const T& data) {

return m_root ? m_root->search(data):nullptr;

}

Node<T>* insert (const T& data) {

return m_root?m_root->insert(data):(m_root=new Node<T>(data));

}

};

int main()

{

srand((unsigned)time(nullptr));

BinTree<int> int_tree;

int last = 0;

for (size_t i=0; i<16; ++i)

{

int val = rand();

while (int_tree.search(val)!=nullptr)

val = rand();

int_tree.insert (val);

last = val;

}

int_tree.print();

for (size_t i=0; i<16; ++i)

{

int val = rand();

if (int_tree.search(val) != nullptr)

std::cout << "The value " << val << " exists!\n";

else

std::cout << "The value " << val << " does not exist!\n";

}

if (int_tree.search(last) != nullptr)

std::cout << "The value " << last << " exists!\n";

else

std::cout << "The value " << last << " does not exist!\n";

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The following program makes use of a binary search tree to store integers. The program generates 10 random numbers which are then inserted into the tree. it then generates random numbers until it locates three which are in the tree, and three which are not in the tree.

#include

#include

class tree

{

private:

struct node

{

node(int num): data(num), left(NULL), right(NULL) {}

~node(){ delete(left); delete(right); }

int data;

node* left;

node* right;

void insert(int num)

{

if( num

{

if( !left )

left = new node(num);

else

left->insert(num);

}

else

{

if( !right )

right = new node(num);

else

right->insert(num);

}

}

void print()

{

if( left )

left->print();

std::cout<

if( right )

right->print();

}

bool find(int num)

{

if( num == data )

return( true );

if( num < data && left )

return( left->find( num ));

if( right )

return( right->find( num ));

return( false );

}

};

node* m_root;

public:

tree(): m_root(NULL){}

~tree(){ delete( m_root ); }

void insert(int num)

{

node* new_node = new node(num);

if( !m_root )

m_root = new_node;

else

m_root->insert(num);

}

void print()

{

if( m_root )

{

std::cout<<"The tree has the following nodes"<

m_root->print();

}

else

std::cout<<"The tree is empty!\n"<

}

bool find(int num)

{

if( m_root )

return( m_root->find( num ));

return( false );

}

};

int main()

{

srand((unsigned) time(NULL));

// instantiate the tree

tree t;

// show the tree

t.print();

// insert 10 random numbers

for(int i=0; i<10; ++i)

t.insert( rand() );

// show the tree

t.print();

std::cout<<"\nSearching for three numbers in the tree..."<

int count = 0;

while( count<3 )

{

int num = rand();

if( t.find( num ))

{

std::cout<<"The number "<

++count;

}

}

std::cout<<"\nSearching for three numbers not in the tree..."<

count = 0;

while( count<3 )

{

int num = rand();

if( !t.find( num ))

{

std::cout<<"The number "<

++count;

}

}

}

Example Output

The tree is empty!

The tree has the following nodes

3735

3759

6220

7922

14522

17096

19727

21238

24131

25922

Searching for three numbers in the tree...

The number 3735 was located!

The number 24131 was located!

The number 14522 was located!

Searching for three numbers not in the tree...

The number 31172 was not located!

The number 20853 was not located!

The number 30182 was not located!

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Consider a binary tree with node structure as follows:typedef struct node{

int data;

node *left, *right;//where the left and right refer to the left and right child of the tree

}node;


There are three types of binary tree traversal. Their order of traversal for a tree with the above node structure would be:

1. Inorder traversal - left child, node, right child


The code for this would be like:


void inorder( node *temp){

if( temp != NULL){

inorder(temp->left);

printf("%d\t", temp->data);

inorder(temp->right);

}

}


2. Preorder Traversal - node, left child, right child


void preorder( node *temp){

if( temp != NULL){

printf("%d\t", temp->data);

preorder(temp->left);

preorder(temp->right);

}

}


3. Postorder Traversal - left child, right child, node


void postorder( node *temp){

if( temp != NULL){

postorder(temp->left);

postorder(temp->right);

printf("%d\t", temp->data);

}

}



The functions are initially called by passing the root.


For other node structure, the basic traversal remains the same, only change takes place in the printing of the data. You can use printf and scanf in c++. Just remember to include cstdio library. The above codes will thus work both in c and c++.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The best way to imagine a balanced binary search tree is with a sorted array. When searching for a value, you start in the middle of the array. If the middle element is equal to the value you seek, you're done. If the value is less than the middle element, you start a new search using the left portion of the array, otherwise you use the right portion of the array, starting with the middle element. Thus every time you fail to find the value, you reduce the number of remaining elements you need to search by half. If you end up searching an array of less than 2 elements, the value does not exist. This is known as a depth first search and is the fastest way to search a sorted array.

A balanced binary search tree is similar to a normal binary search tree except that nodes are added according to depth-first search. Thus the middle element forms the root, while its left node is the middle element of the lower elements, and the right node is the middle element of the upper elements. Repeating this process for each node gradually builds the balanced tree.

Balanced trees are identified by the fact that all the leaf nodes reside on no more than two levels of the tree (the ideal is to have all leafs on one level). This ensures the depth of search to the left and right of any node is as close to equal as possible, thus ensuring optimal search times.

Building a balanced search tree from a normal binary search tree is a complex process, thus you will typically build the entire tree first and then recursively rotate the nodes to create a balanced tree. However, if new nodes are subsequently inserted into the balanced search tree, you must re-rotate all the nodes upon each insertion, which naturally increases insertion time. A simpler approach is to use a self-balancing tree, such as a red/black tree. Nodes must still be rotated upon each insertion but the algorithm is greatly simplified.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is balanced binary search tree in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Which is faster binary tree or binary search tree?

A tree doesn't do anything so it has no speed...


What is the difference between extended binary tree and a binary search tree?

A strictly binary tree is one where every node other than the leaves has exactly 2 child nodes. Such trees are also known as 2-trees or full binary trees. An extended binary tree is a tree that has been transformed into a full binary tree. This transformation is achieved by inserting special "external" nodes such that every "internal" node has exactly two children.


IS AVL-Tree IS binary tree?

An AVL tree is another balanced binary search tree. Named after their inventors, Adelson-Velskii and Landis, they were the first dynamically balanced trees to be proposed. Like red-black trees, they are not perfectly balanced, but pairs of sub-trees differ in height by at most 1, maintaining an O(logn) search time. Addition and deletion operations also take O(logn) time.Definition of an AVL treeAn AVL tree is a binary search tree which has the following properties: The sub-trees of every node differ in height by at most one.Every sub-tree is an AVL tree.


What is balanced binary search tree?

A balanced tree is a tree which is balanced - it has roughly the same height on each of its sub-nodes. A balanced tree will have the lowest possible overall height.For example, a balanced binary search tree will have equal heights (plus or minus one) on the left and right sub-trees of each node. This ensures that operations on the tree always are guaranteed to have O(lg n) time, rather than the O(n) time that they might have in an unbalanced tree.Certain tree algorithms are designed for ensuring that the tree stays balanced at all times, while maintaining the O(lg n) time for all operations. Such algorithms, such as red-black trees, AVL trees, and others, are generally used in standard library implementation of binary search trees.


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.

Related questions

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).


Which is faster binary tree or binary search tree?

A tree doesn't do anything so it has no speed...


What is the difference between extended binary tree and a binary search tree?

A strictly binary tree is one where every node other than the leaves has exactly 2 child nodes. Such trees are also known as 2-trees or full binary trees. An extended binary tree is a tree that has been transformed into a full binary tree. This transformation is achieved by inserting special "external" nodes such that every "internal" node has exactly two children.


Does binary tree and binary search tree same?

no they are not same


IS AVL-Tree IS binary tree?

An AVL tree is another balanced binary search tree. Named after their inventors, Adelson-Velskii and Landis, they were the first dynamically balanced trees to be proposed. Like red-black trees, they are not perfectly balanced, but pairs of sub-trees differ in height by at most 1, maintaining an O(logn) search time. Addition and deletion operations also take O(logn) time.Definition of an AVL treeAn AVL tree is a binary search tree which has the following properties: The sub-trees of every node differ in height by at most one.Every sub-tree is an AVL tree.


Where to insert an element in binary tree if two nodes are equal in size?

Same as if two nodes are NOT equal in size. Size of nodes has nothing to do where to insert a new element. The insertion should be applying the search algorithm of that binary tree (so the new inserted element maybe found later). For balanced (in size) binary tree, the above still applied, because 50% of the time the tree is unbalanced (a binary tree with even number of elements is not balanced). Plus, those 2 nodes, may not be the "right" and "left" nodes of a given one, so 2 nodes equals in size has nothing to do with the way the elements being inserted into a binary tree.


What is balanced binary search tree?

A balanced tree is a tree which is balanced - it has roughly the same height on each of its sub-nodes. A balanced tree will have the lowest possible overall height.For example, a balanced binary search tree will have equal heights (plus or minus one) on the left and right sub-trees of each node. This ensures that operations on the tree always are guaranteed to have O(lg n) time, rather than the O(n) time that they might have in an unbalanced tree.Certain tree algorithms are designed for ensuring that the tree stays balanced at all times, while maintaining the O(lg n) time for all operations. Such algorithms, such as red-black trees, AVL trees, and others, are generally used in standard library implementation of binary search trees.


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.


What is difference between full binary tree and balanced binary tree?

A binary tree is considered to be balanced if all of the leaves of the tree are on the same level or at least within one level of each other.A binary tree is considered to be full if all of the leaves of the tree are at the same level and every non leaf node has exactly 2 children.


What is Efficiency of Binary Search tree operations?

If it is an unbalanced binary tree, O( ln( n ) / ln( 2 ) ) is best-case. Worst case is O( n ). If it is balanced, worst case is O( ln( n ) / ln( 2 ) ).


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

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


What is the full form of avl tree?

Adelson-Velskii and Landis (balanced binary tree)