C++ 二叉树插入法

C++ 二叉树插入法,c++,C++,对于类,我必须创建一个binaryTree,而我似乎无法使insert方法正常工作 预期成果: first: tree is not empty no of nodes = 15 height of tree = 5 The elements of 'first' in inorder: -11 8 -3 12 -1 -9 -5 2 16 10 6 -13 4 14 -7 The elements of 'first' in preord

对于类,我必须创建一个binaryTree,而我似乎无法使insert方法正常工作

预期成果:

first: tree is not empty
        no of nodes    = 15
        height of tree =  5

The elements of 'first' in inorder:
        -11 8 -3 12 -1 -9 -5 2 16 10 6 -13 4 14 -7
The elements of 'first' in preorder:
        2 -1 -3 8 -11 12 -5 -9 4 6 10 16 -13 -7 14
The elements of 'first' in postorder:
        -11 8 12 -3 -9 -5 -1 16 10 -13 6 14 -7 4 2

second: tree is not empty
        no of nodes    =  9
        height of tree =  4

The elements of 'second' in inorder:
        7 3.25 0.75 -7.75 -0.5 -11.5 4.5 -4 8.25
The elements of 'second' in preorder:
        -0.5 0.75 3.25 7 -7.75 -4 4.5 -11.5 8.25
The elements of 'second' in postorder:
        7 3.25 -7.75 0.75 -11.5 4.5 8.25 -4 -0.5

third: tree is not empty
        no of nodes    =  7
        height of tree =  4

The elements of 'third' in inorder:
        objects. list is string This of a
The elements of 'third' in preorder:
        This is list objects. string a of
The elements of 'third' in postorder:
        objects. list string is of a This
我的结果:

first: tree is not empty
       no of nodes    = 15
       height of tree = 4
The elements of 'first' in inorder:
-9 -5 4 16 -1 -13 10 -7 2 14 8 6 -3 -11 12
The elements of 'first' in preorder:
2 -1 4 -5 -9 16 -7 10 -13 -3 6 8 14 12 -11
The elements of 'first' in postorder:
-9 -5 16 4 -13 10 -7 -1 14 8 6 -11 12 -3 2
second: tree is not empty
       no of nodes    = 9
       height of tree = 3
The elements of 'second' in inorder:
-7.75 -4 0.75 -11.5 8.25 -0.5 7 4.5 3.25
The elements of 'second' in preorder:
-0.5 0.75 -4 -7.75 8.25 -11.5 3.25 4.5 7
The elements of 'second' in postorder:
-7.75 -4 -11.5 8.25 0.75 7 4.5 3.25 -0.5
third: tree is not empty
       no of nodes    = 7
       height of tree = 3
The elements of 'third' in inorder:
string a is This objects. of list
The elements of 'third' in preorder:
This is a string list of objects.
The elements of 'third' in postorder:
string a is objects. of list This
代码:

template <class T>
void binTree<T>::insert(binTreeNode < T >*& node, const T& data) {
        if(node == NULL) {
                root = new binTreeNode<T>(data, NULL, NULL);
                return;
        }

        binTreeNode<T>* ptr1 = node;
        binTreeNode<T>* ptr2 = node;
        bool placeRight = 0;
        while(ptr1 != NULL) {
                ptr2 = ptr1;
                if(height(ptr1->left) > height(ptr1->right)) {
                        placeRight = true;
                        ptr1 = ptr1->right;
                } else if (height(ptr1->right) > height(ptr1->left)) {
                        placeRight = false;
                        ptr1 = ptr1->left;
                } else {
                        placeRight = false;
                        ptr1 = ptr1->left;
                }
        }

        if(placeRight) {
                ptr2->right = new binTreeNode<T>(data, NULL, NULL);
        } else {
                ptr2->left = new binTreeNode<T>(data, NULL, NULL);
        }
}
模板
void binTree::insert(binTreeNode*&节点、常量和数据){
if(node==NULL){
root=新的binTreeNode(数据,NULL,NULL);
返回;
}
binTreeNode*ptr1=节点;
binTreeNode*ptr2=节点;
bool-placeRight=0;
while(ptr1!=NULL){
ptr2=ptr1;
如果(高度(ptr1->左)>高度(ptr1->右)){
placeRight=true;
ptr1=ptr1->右侧;
}否则如果(高度(ptr1->右)>高度(ptr1->左)){
placeRight=false;
ptr1=ptr1->左;
}否则{
placeRight=false;
ptr1=ptr1->左;
}
}
如果(右){
ptr2->right=新的binTreeNode(数据,NULL,NULL);
}否则{
ptr2->left=新的binTreeNode(数据,NULL,NULL);
}
}
驱动程序:

const vector<int> A { 1, -2, 3, -4, 5, -6, 7, -8, 9, -10, 11, -12, 13, -14, 15 };
const vector<float> B { 0.5, 1.75, -3, 4.25, 5.50, -6.75, 8, 9.25, -10.5 };
const vector<string> C { "This", "is", "a", "list", "of", "string", "objects." };
int main() {
        binTree<int> intTree = binTree<int>();
        binTree<float> floatTree = binTree<float>();
        binTree<string> strTree = binTree<string>();

        for (std::vector<int>::const_iterator it = A.begin() ; it != A.end(); ++it) {
                intTree.insert(*it);
        }
        intTree.preorder(increase);
        cout << "first: ";
        header(intTree);

        inorder(intTree, "first");
        preorder(intTree, "first");
        postOrder(intTree, "first");
}
常量向量A{1,-2,3,-4,5,-6,7,-8,9,-10,11,-12,13,-14,15}; 常数向量B{0.5,1.75,-3,4.25,5.50,-6.75,8,9.25,-10.5}; 常量向量C{“This”,“is”,“a”,“list”,“of”,“string”,“objects.”; int main(){ binTree intTree=binTree(); binTree floatTree=binTree(); binTree strTree=binTree(); 对于(std::vector::const_迭代器it=A.begin();it!=A.end();+it){ intTree.insert(*it); } intTree.preorder(增加); cout*节点,void(*f)(T&) { if(node==NULL){ 返回; } 顺序(节点->左,f); f(节点->数据); 顺序(节点->右侧,f); } 模板 void binTree::预排序(binTreeNode*节点,void(*f)(T&)) { if(node==NULL){ 返回; } f(节点->数据); 预订单(节点->左,f); 预订单(节点->右侧,f); } 模板 void binTree::postorder(binTreeNode*节点,void(*f)(T&)) { if(node==NULL){ 返回; } 后订单(节点->左,f); postorder(节点->右侧,f); f(节点->数据); } 模板 int binTree::高度(binTreeNode*节点)常量{ 如果(node==NULL | |((node->left==NULL)和&(node->right==NULL))){ 返回0; } int leftSide=高度(节点->左侧); int rightSide=高度(节点->右侧); 如果(左侧>右侧){ 返回左侧+1; }否则{ 返回右侧+1; } }
看起来向量的符号
A
是向后的。你有
1,-2,3,-4,
,但正确的解决方案是
-1,2,-3,4,
。同样地,你
B
是向后的

const vector<float> B { 0.5, 1.75, -3, 4.25, 5.50, -6.75, 8, 9.25, -10.5 };
这些看起来甚至不完全相同

某个地方的转录错误?

您的height()函数是什么

我认为您误解了BST的定义:

A. the value of the left child is smaller than the value of root node.

B. the value of the right child is bigger than the value of root node.

C. his left child tree and right child tree are also a BST.
但通过这里的代码:

while(ptr1 != NULL) {
            ptr2 = ptr1;
            if(height(ptr1->left) > height(ptr1->right)) {
                    placeRight = true;
                    ptr1 = ptr1->right;
            } else if (height(ptr1->right) > height(ptr1->left)) {
                    placeRight = false;
                    ptr1 = ptr1->left;
            } else {
                    placeRight = false;
                    ptr1 = ptr1->left;
            }
    }

您只需比较节点的高度,而不是比较节点的实际值。

您的错误在height方法中。如果您有一个不是null但没有子节点的节点,则返回零。您应该返回1

将高度方法中的此条件更改为:

if (node == NULL || ((node->left == NULL) && (node->right == NULL))) {
    return 0;
}
致:


正确的解决方案没有元素
-10
,但在你的情况下它确实存在。而且它似乎是你的第一个向量中的一个元素。你能评论一下吗?@Floris:你是如何这么快地阅读这段文本并找到
-10
?!:-)@Floris:这是一个非常有趣的通知。我会调查它,以确保我没有遗漏任何东西作业。@MM。-我没有全部读。我有一个讨厌的习惯,就是只看到一点文字,然后找到“a”问题…我接受你的赞扬。我已经更新了结果,因为我忘了应用递增和递减函数。但是结果的顺序仍然错误。提供了向量。我更新了输出,因为我在将向量插入二进制树时出错。你注意到我运行了“递增”和“递减”方法吗这增加了每个元素1或减少了每个元素1。OP从来没有说它是一个二叉搜索树(BST),只是一个二叉树(意味着每个节点有两个子节点,没有其他限制)。在他的“预期结果”中,元素在索引遍历中没有排序,我希望看到该树是否是BST。
while(ptr1 != NULL) {
            ptr2 = ptr1;
            if(height(ptr1->left) > height(ptr1->right)) {
                    placeRight = true;
                    ptr1 = ptr1->right;
            } else if (height(ptr1->right) > height(ptr1->left)) {
                    placeRight = false;
                    ptr1 = ptr1->left;
            } else {
                    placeRight = false;
                    ptr1 = ptr1->left;
            }
    }
if (node == NULL || ((node->left == NULL) && (node->right == NULL))) {
    return 0;
}
if (node == NULL) {
    return 0;
}