C++ 二叉搜索树未将插入值加载到树中
我的问题是试图让函数C++ 二叉搜索树未将插入值加载到树中,c++,algorithm,binary-tree,binary-search-tree,C++,Algorithm,Binary Tree,Binary Search Tree,我的问题是试图让函数treeleavesunt()和treeNodeCount()返回树中叶子和节点的值,但是,我的问题是在使用insert()函数通过while循环提供值之后,树似乎保持为空,我使用isEmpty()知道这一点将值插入树之前和之后的函数 main.cpp #include <iostream> #include "binaryTreeType.h" #include "bSearchTreeType.h" using namespace std; int ma
treeleavesunt()
和treeNodeCount()
返回树中叶子和节点的值,但是,我的问题是在使用insert()
函数通过while循环提供值之后,树似乎保持为空,我使用isEmpty()知道这一点
将值插入树之前和之后的函数
main.cpp
#include <iostream>
#include "binaryTreeType.h"
#include "bSearchTreeType.h"
using namespace std;
int main()
{
int num;
bSearchTreeType<int> *myTree= new bSearchTreeType<int>();
//test if tree is empty
if(myTree->isEmpty())
cout << "yes" << endl;
else
cout << "no" << endl;
cout << "Line 10: Enter numbers ending with -999"<< endl;
cin >> num;
while (num != -999)
{
myTree->insert(num);
cin >> num;
}
myTree->inorderTraversal();
int p;
p=myTree->treeNodeCount();
cout << p << endl;
p=myTree->treeLeavesCount();
cout << p << endl;
//test if tree is empty after data is inserted
if(myTree->isEmpty())
cout << "yes" << endl;
else
cout << "no" << endl;
delete myTree;
return 0;
}
#包括
#包括“binaryTreeType.h”
#包括“bSearchTreeType.h”
使用名称空间std;
int main()
{
int-num;
bSearchTreeType*myTree=新的bSearchTreeType();
//测试树是否为空
如果(myTree->isEmpty())
cout inorderTraversal();
INTP;
p=myTree->treeNodeCount();
cout=y)
返回x;
其他的
返回y;
}
模板
void binaryTreeType::copyTree(binaryTreeNode*&copiedTreeRoot,binaryTreeNode*其他treeroot)
{
if(otherTreeRoot==NULL)
copiedTreeRoot=NULL;
其他的
{
copiedTreeRoot=新的二元树节点;
copiedTreeRoot->info=otherTreeRoot->info;
copyTree(copiedTreeRoot->llink,otherTreeRoot->llink);
copyTree(copiedTreeRoot->rlink,otherTreeRoot->rlink);
}
}//结束复制树
模板
void binaryTreeType::destroy(binaryTreeNode*&p)
{
如果(p!=NULL)
{
破坏(p->llink);
销毁(p->rlink);
删除p;
p=零;
}
}
模板
void binaryTreeType::destroyTree()
{
消灭(根);
}
模板
binaryTreeType::binaryTreeType(const binaryTreeType和otherTree)
{
if(otherTree.root==NULL)//otherTree为空
root=NULL;
其他的
copyTree(root,otherTree.root);
}
模板
binaryTreeType::~binaryTreeType()
{
消灭(根);
}
模板
const-binaryTreeType&binaryTreeType::operator=(const-binaryTreeType&otherTree)
{
if(this!=&otherTree)//避免自复制
{
if(root!=NULL)//如果二叉树不是空的,
//破坏二叉树
消灭(根);
if(otherTree.root==NULL)//otherTree为空
root=NULL;
其他的
copyTree(root,otherTree.root);
}//结束其他
归还*这个;
}
模板
int binaryTreeType::leaveScont(binaryTreeNode*p)常量
{
if(p==NULL)
返回0;
如果(p->llink==NULL&&p->rlink==NULL)
返回1;
其他的
返回LeaveScont(p->llink)+LeaveScont(p->rlink);
}
模板
int binaryTreeType::nodeCount(binaryTreeNode*p)常量
{
整数计数=1;
if(p==NULL){
返回0;
}否则{
计数+=节点计数(p->llink);
计数+=节点计数(p->rlink);
}
返回计数;
}
#endif//BINARYTREETYPE_H
bSearchTreeType.h
#ifndef BINARYTREETYPE_H
#define BINARYTREETYPE_H
#include <queue>
template <class elemType>
struct binaryTreeNode
{
elemType info;
binaryTreeNode<elemType> *llink;
binaryTreeNode<elemType> *rlink;
};
template <class elemType>
class binaryTreeType
{
public:
const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&);
//Overload the assignment operator.
bool isEmpty() const;
//Returns true if the binary tree is empty;
//otherwise, returns false.
void inorderTraversal() const;
//Function to do an inorder traversal of the binary tree.
void preorderTraversal() const;
//Function to do a preorder traversal of the binary tree.
void postorderTraversal() const;
//Function to do a postorder traversal of the binary tree.
int treeHeight() const;
//Returns the height of the binary tree.
int treeNodeCount() const;
//Returns the number of nodes in the binary tree.
int treeLeavesCount() const;
//Returns the number of leaves in the binary tree.
void destroyTree();
//Deallocates the memory space occupied by the binary tree.
//Postcondition: root = NULL;
binaryTreeType(const binaryTreeType<elemType>& otherTree);
//copy constructor
binaryTreeType();
//default constructor
~binaryTreeType();
//destructor
protected:
binaryTreeNode<elemType> *root;
private:
void copyTree(binaryTreeNode<elemType>* &copiedTreeRoot,binaryTreeNode<elemType>* otherTreeRoot);
//Makes a copy of the binary tree to which
//otherTreeRoot points. The pointer copiedTreeRoot
//points to the root of the copied binary tree.
void destroy(binaryTreeNode<elemType>* &p);
//Function to destroy the binary tree to which p points.
//Postcondition: p = NULL
void inorder(binaryTreeNode<elemType> *p) const;
//Function to do an inorder traversal of the binary
//tree to which p points.
void preorder(binaryTreeNode<elemType> *p) const;
//Function to do a preorder traversal of the binary
//tree to which p points.
void postorder(binaryTreeNode<elemType> *p) const;
//Function to do a postorder traversal of the binary
//tree to which p points.
int height(binaryTreeNode<elemType> *p) const;
//Function to return the height of the binary tree
//to which p points.
int max(int x, int y) const;
//Returns the larger of x and y.
int nodeCount(binaryTreeNode<elemType> *p) const;
//Function to return the number of nodes in the binary
//tree to which p points
int leavesCount(binaryTreeNode<elemType> *p) const;
//Function to return the number of leaves in the binary
//tree to which p points
};
template <class elemType>
bool binaryTreeType<elemType>::isEmpty() const
{
return (root == NULL);
};
template <class elemType>
binaryTreeType<elemType>::binaryTreeType()
{
root = NULL;
}
template <class elemType>
void binaryTreeType<elemType>::inorderTraversal() const
{
inorder(root);
}
template <class elemType>
void binaryTreeType<elemType>::preorderTraversal() const
{
preorder(root);
}
template <class elemType>
void binaryTreeType<elemType>::postorderTraversal() const
{
postorder(root);
}
template <class elemType>
int binaryTreeType<elemType>::treeHeight() const
{
return height(root);
}
template <class elemType>
int binaryTreeType<elemType>::treeNodeCount() const
{
return nodeCount(root);
}
template <class elemType>
int binaryTreeType<elemType>::treeLeavesCount() const
{
return leavesCount(root);
}
template <class elemType>
void binaryTreeType<elemType>::inorder(binaryTreeNode<elemType> *p) const
{
if (p != NULL)
{
inorder(p->llink);
std::cout << p->info << " ";
inorder(p->rlink);
}
}
template <class elemType>
void binaryTreeType<elemType>::preorder(binaryTreeNode<elemType> *p) const
{
if (p != NULL)
{
std::cout << p->info << " ";
preorder(p->llink);
preorder(p->rlink);
}
}
template <class elemType>
void binaryTreeType<elemType>::postorder(binaryTreeNode<elemType> *p) const
{
if (p != NULL)
{
postorder(p->llink);
postorder(p->rlink);
std::cout << p->info << " ";
}
}
template <class elemType>
int binaryTreeType<elemType>::height(binaryTreeNode<elemType> *p) const
{
if (p == NULL)
return 0;
else
return 1 + max(height(p->llink), height(p->rlink));
}
template <class elemType>
int binaryTreeType<elemType>::max(int x, int y) const
{
if (x >= y)
return x;
else
return y;
}
template <class elemType>
void binaryTreeType<elemType>::copyTree(binaryTreeNode<elemType>* &copiedTreeRoot,binaryTreeNode<elemType>* otherTreeRoot)
{
if (otherTreeRoot == NULL)
copiedTreeRoot = NULL;
else
{
copiedTreeRoot = new binaryTreeNode<elemType>;
copiedTreeRoot->info = otherTreeRoot->info;
copyTree(copiedTreeRoot->llink, otherTreeRoot->llink);
copyTree(copiedTreeRoot->rlink, otherTreeRoot->rlink);
}
} //end copyTree
template <class elemType>
void binaryTreeType<elemType>::destroy(binaryTreeNode<elemType>* &p)
{
if (p != NULL)
{
destroy(p->llink);
destroy(p->rlink);
delete p;
p = NULL;
}
}
template <class elemType>
void binaryTreeType<elemType>::destroyTree()
{
destroy(root);
}
template <class elemType>
binaryTreeType<elemType>::binaryTreeType(const binaryTreeType<elemType>& otherTree)
{
if (otherTree.root == NULL) //otherTree is empty
root = NULL;
else
copyTree(root, otherTree.root);
}
template <class elemType>
binaryTreeType<elemType>::~binaryTreeType()
{
destroy(root);
}
template <class elemType>
const binaryTreeType<elemType>& binaryTreeType<elemType>::operator=(const binaryTreeType<elemType>& otherTree)
{
if (this != &otherTree) //avoid self-copy
{
if (root != NULL) //if the binary tree is not empty,
//destroy the binary tree
destroy(root);
if (otherTree.root == NULL) //otherTree is empty
root = NULL;
else
copyTree(root, otherTree.root);
}//end else
return *this;
}
template <class elemType>
int binaryTreeType<elemType>::leavesCount(binaryTreeNode<elemType> *p) const
{
if(p == NULL)
return 0;
if(p->llink == NULL && p->rlink==NULL)
return 1;
else
return leavesCount(p->llink) + leavesCount(p->rlink);
}
template <class elemType>
int binaryTreeType<elemType> ::nodeCount(binaryTreeNode<elemType> *p) const
{
int count = 1;
if ( p == NULL ){
return 0;
}else{
count += nodeCount(p->llink);
count += nodeCount(p->rlink);
}
return count;
}
#endif // BINARYTREETYPE_H
#ifndef BSEARCHTREETYPE_H
#define BSEARCHTREETYPE_H
#include "binaryTreeType.h"
#include <iostream>
#include <cassert>
template <class elemType>
class bSearchTreeType : public binaryTreeType<elemType>
{
public:
bool search(const elemType& searchItem) const;
//Function to determine if searchItem is in the binary
//search tree.
//Postcondition: Returns true if searchItem is found in the
// binary search tree; otherwise, returns false.
void insert(const elemType& insertItem);
//Function to insert insertItem in the binary search tree.
//Postcondition: If no node in the binary search tree has the
// same info as insertItem, a node with the info insertItem
// is created and inserted in the binary search tree.
void deleteNode(const elemType& deleteItem);
//Function to delete deleteItem from the binary search tree.
//Postcondition: If a node with the same info as deleteItem
// is found, it is deleted from the binary search tree.
protected:
binaryTreeNode<elemType> *root;
private:
void deleteFromTree(binaryTreeNode<elemType>* &p);
//Function to delete the node to which p points is deleted
//from the binary search tree.
//Postcondition: The node to which p points is deleted from
// the binary search tree.
};
using namespace std;
template <class elemType>
bool bSearchTreeType<elemType>::search(const elemType& searchItem) const
{
binaryTreeNode<elemType> *current;
bool found = false;
if (root == NULL)
cerr << "Cannot search the empty tree." << endl;
else
{
current = root;
while (current != NULL && !found)
{
if (current->info == searchItem)
found = true;
else if (current->info > searchItem)
current = current->llink;
else
current = current->rlink;
}//end while
}//end else
return found;
}//end search
template <class elemType>
void bSearchTreeType<elemType>::insert(const elemType& insertItem)
{
binaryTreeNode<elemType> *current; //pointer to traverse the tree
binaryTreeNode<elemType> *trailCurrent; //pointer behind current
binaryTreeNode<elemType> *newNode; //pointer to create the node
newNode = new binaryTreeNode<elemType>;
assert(newNode != NULL);
newNode->info = insertItem;
newNode->llink = NULL;
newNode->rlink = NULL;
if (root == NULL)
root = newNode;
else
{
current = root;
while (current != NULL)
{
trailCurrent = current;
if (current->info == insertItem)
{
std::cerr << "The insert item is already in the list-";
std::cerr << "duplicates are not allowed." << insertItem << std::endl;
return;
}
else if (current->info > insertItem)
current = current->llink;
else
current = current->rlink;
}//end while
if (trailCurrent->info > insertItem)
trailCurrent->llink = newNode;
else
trailCurrent->rlink = newNode;
}
}//end insert
template <class elemType>
void bSearchTreeType<elemType>::deleteFromTree(binaryTreeNode<elemType>* &p)
{
binaryTreeNode<elemType> *current;//pointer to traverse the tree
binaryTreeNode<elemType> *trailCurrent; //pointer behind current
binaryTreeNode<elemType> *temp; //pointer to delete the node
if (p == NULL)
cerr << "Error: The node to be deleted is NULL." << endl;
else if(p->llink == NULL && p->rlink == NULL)
{
temp = p;
p = NULL;
delete temp;
}
else if(p->llink == NULL)
{
temp = p;
p = temp->rlink;
delete temp;
}
else if(p->rlink == NULL)
{
temp = p;
p = temp->llink;
delete temp;
}
else
{
current = p->llink;
trailCurrent = NULL;
while (current->rlink != NULL)
{
trailCurrent = current;
current = current->rlink;
}//end while
p->info = current->info;
if (trailCurrent == NULL) //current did not move;
//current == p->llink; adjust p
p->llink = current->llink;
else
trailCurrent->rlink = current->llink;
delete current;
}//end else
}//end deleteFromTree
template <class elemType>
void bSearchTreeType<elemType>::deleteNode(const elemType& deleteItem)
{
binaryTreeNode<elemType> *current; //pointer to traverse the tree
binaryTreeNode<elemType> *trailCurrent; //pointer behind current
bool found = false;
if (root == NULL)
std::cout << "Cannot delete from the empty tree." << endl;
else
{
current = root;
trailCurrent = root;
while (current != NULL && !found)
{
if (current->info == deleteItem)
found = true;
else
{
trailCurrent = current;
if (current->info > deleteItem)
current = current->llink;
else
current = current->rlink;
}
}//end while
if (current == NULL)
std::cout << "The delete item is not in the tree." << endl;
else if (found)
{
if (current == root)
deleteFromTree(root);
else if (trailCurrent->info > deleteItem)
deleteFromTree(trailCurrent->llink);
else
deleteFromTree(trailCurrent->rlink);
}//end if
}
}//end deleteNode
#endif // BSEARCHTREETYPE_H
\ifndef BSEARCHTREETYPE\u H
#定义BSEARCHTREETYPE\u H
#包括“binaryTreeType.h”
#包括
#包括
模板
类bSearchTreeType:公共二进制树类型
{
公众:
布尔搜索(常量元素类型和搜索项)常量;
//函数确定searchItem是否在二进制文件中
//搜索树。
//Postcondition:如果在中找到searchItem,则返回true
//二叉搜索树;否则返回false。
无效插入(常量元素类型和插入项);
//函数在二进制搜索树中插入插入项。
//后置条件:如果二叉搜索树中没有节点具有
//信息与insertItem相同,是一个具有信息insertItem的节点
//创建并插入到二进制搜索树中。
void deleteNode(const elemType&deleteItem);
//函数从二进制搜索树中删除deleteItem。
//后置条件:如果节点具有与deleteItem相同的信息
//如果找到,则从二进制搜索树中删除。
受保护的:
二叉树烯*根;
私人:
void deleteFromTree(二进制树节点*&p);
//函数删除要删除p点的节点
//从二进制搜索树。
//后置条件:从中删除p点的节点
//二叉搜索树。
};
使用名称空间std;
模板
bool bSearchTreeType::search(const elemType&searchItem)const
{
二元树节点*电流;
bool-found=false;
if(root==NULL)
cerr信息>搜索项目)
当前=当前->llink;
其他的
当前=当前->rlink;
}//结束时
}//结束其他
发现退货;
}//结束搜索
模板
void bSearchTreeType::insert(常量elemType和insertItem)
{
binaryTreeNode*current;//遍历树的指针
binaryTreeNode*trailCurrent;//当前后面的指针
binaryTreeNode*newNode;//创建节点的指针
新节点=新的二元树节点;
断言(newNode!=NULL);
新建节点->信息=插入项;
newNode->llink=NULL;
newNode->rlink=NULL;
if(root==NULL)
根=新节点;
其他的
{
电流=根;
while(当前!=NULL)
{
牵引电流=电流;
如果(当前->信息==插入项)
{
标准:cerr-rlink;
}//结束时
如果(跟踪当前->信息>插入项)
trailCurrent->llink=newNode;
其他的
trailCurrent->rlink=newNode;
}
}//端部嵌件
模板
void bSearchTreeType::deleteFromTree(二进制TreeNode*&p)
{
binaryTreeNode*current;//遍历树的指针
binaryTreeNode*trailCurrent;//当前后面的指针
binaryTreeNode*temp;//删除节点的指针
if(p==NULL)
cerr rlink==NULL)
{
温度=p;
p=零;
删除临时文件;
}
else if(p->llink==NULL)
{
温度=p;
p=温度->rlink;
删除临时文件;
}
else if(p->rlink==NULL)
{
温度=p;
p=温度->最低温度;
删除临时文件;
}
其他的
{
电流=p->llink;
trailCurrent=NULL;
while(当前->链接!=NULL)
{
牵引电流=电流;
当前=当前->rlink;
}//结束时
p->info
template <class elemType>
class binaryTreeType
{
public:
bool isEmpty() const;
protected:
binaryTreeNode<elemType> *root;
};
template <class elemType>
bool binaryTreeType<elemType>::isEmpty() const
{
return (root == NULL);
};
template <class elemType>
class bSearchTreeType : public binaryTreeType<elemType>
{
void insert(const elemType& insertItem);
protected:
binaryTreeNode<elemType> *root;
};
template <class elemType>
void bSearchTreeType<elemType>::insert(const elemType& insertItem)
{
// ...
if (root == NULL)
root = newNode;
else
{
current = root;
//...
}
}//end insert