Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;BST树插入一个字符串_C++_Binary Search Tree_Avl Tree - Fatal编程技术网

C++ C++;BST树插入一个字符串

C++ C++;BST树插入一个字符串,c++,binary-search-tree,avl-tree,C++,Binary Search Tree,Avl Tree,我正在尝试创建一个数据为字符串的BST。。但是,它似乎不喜欢字符串值..如果我将数据类型更改为int,代码将正常工作。。我不知道为什么有人能帮忙吗? 这是密码 // BST.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<stdio.h> #include<stdlib.h> #include<string> #include

我正在尝试创建一个数据为字符串的BST。。但是,它似乎不喜欢字符串值..如果我将数据类型更改为int,代码将正常工作。。我不知道为什么有人能帮忙吗? 这是密码

// BST.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<iostream>

using namespace std;

// An AVL tree node
struct Node
{
    string key;
    struct Node *left;
    struct Node *right;
    int height;
    int counter;
};

// A utility function to get maximum of two integers
int max(int a, int b);

// A utility function to get height of the tree
int height(struct Node *N)
{
    if (N == NULL)
        return 0;
    return N->height;
}

// A utility function to get maximum of two integers
int max(int a, int b)
{
    return (a > b) ? a : b;
}

/* Helper function that allocates a new node with the given key and
NULL left and right pointers. */
struct Node* newNode(const string& key)
{
    struct Node* node = (struct Node*)
        malloc(sizeof(struct Node));
    node->key = key;
    node->left = nullptr;
    node->right = nullptr;
    node->counter = 0;
    node->height = 1; // new node is initially added at leaf
    return(node);
}

// A utility function to right rotate subtree rooted with y
// See the diagram given above.
struct Node *rightRotate(struct Node *y)
{
    struct Node *x = y->left;
    struct Node *T2 = x->right;

    // Perform rotation
    x->right = y;
    y->left = T2;

    // Update heights
    y->height = max(height(y->left), height(y->right)) + 1;
    x->height = max(height(x->left), height(x->right)) + 1;

    // Return new root
    return x;
}

// A utility function to left rotate subtree rooted with x
// See the diagram given above.
struct Node *leftRotate(struct Node *x)
{
    struct Node *y = x->right;
    struct Node *T2 = y->left;

    // Perform rotation
    y->left = x;
    x->right = T2;

    // Update heights
    x->height = max(height(x->left), height(x->right)) + 1;
    y->height = max(height(y->left), height(y->right)) + 1;

    // Return new root
    return y;
}

// Get Balance factor of node N
int getBalance(struct Node *N)
{
    if (N == NULL)
        return 0;
    return height(N->left) - height(N->right);
}

// Recursive function to insert key in subtree rooted
// with node and returns new root of subtree.
struct Node* insert(struct Node* node, string key)


{
    /* 1. Perform the normal BST insertion */
    if (node == NULL)
        return(newNode(key));

    if (key < node->key)
        node->left = insert(node->left, key);
    else if (key > node->key)
        node->right = insert(node->right, key);
    else // Equal keys are not allowed in BST
    {
        node->counter++;
        return node;
    }
    /* 2. Update height of this ancestor node */
    node->height = 1 + max(height(node->left),
        height(node->right));

    /* 3. Get the balance factor of this ancestor
    node to check whether this node became
    unbalanced */
    int balance = getBalance(node);

    // If this node becomes unbalanced, then
    // there are 4 cases

    // Left Left Case
    if (balance > 1 && key < node->left->key)
        return rightRotate(node);

    // Right Right Case
    if (balance < -1 && key > node->right->key)
        return leftRotate(node);

    // Left Right Case
    if (balance > 1 && key > node->left->key)
    {
        node->left = leftRotate(node->left);
        return rightRotate(node);
    }

    // Right Left Case
    if (balance < -1 && key < node->right->key)
    {
        node->right = rightRotate(node->right);
        return leftRotate(node);
    }

    /* return the (unchanged) node pointer */
    return node;
}

// A utility function to print preorder traversal
// of the tree.
// The function also prints height of every node
void preOrder(struct Node *root)
{
    if (root)
    {
        cout << root->key << endl;;
        preOrder(root->left);
        preOrder(root->right);
    }
}

/* Drier program to test above function*/
int main()
{
    struct Node *root = nullptr;

    /* Constructing tree given in the above figure */

    root = insert(root, "a");
    root = insert(root, "bc");
    root = insert(root, "DE");
    root = insert(root, "op");
    root = insert(root, "lo");
    root = insert(root, "mp");

    /*root = insert(root, 10);
    root = insert(root, 20);
    root = insert(root, 30);
    root = insert(root, 40);
    root = insert(root, 50);
    root = insert(root, 25);*/

    printf("Preorder traversal of the constructed AVL"
        " tree is \n");
    preOrder(root);

    return 0;
}
//BST.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
#包括
#包括
#包括
使用名称空间std;
//AVL树节点
结构体类型
{
字符串键;
结构节点*左;
结构节点*右;
内部高度;
整数计数器;
};
//获取两个整数最大值的实用函数
int max(int a,int b);
//获取树的高度的实用函数
整数高度(结构节点*N)
{
如果(N==NULL)
返回0;
返回N->高度;
}
//获取两个整数最大值的实用函数
最大整数(整数a,整数b)
{
返回(a>b)?a:b;
}
/*Helper函数,它使用给定的键和
空的左指针和右指针*/
结构节点*新节点(常量字符串和键)
{
结构节点*节点=(结构节点*)
malloc(sizeof(struct Node));
节点->键=键;
节点->左=空PTR;
节点->右=nullptr;
节点->计数器=0;
节点->高度=1;//新节点最初添加在叶
返回(节点);
}
//右旋转以y为根的子树的实用函数
//见上图。
结构节点*rightRotate(结构节点*y)
{
结构节点*x=y->左;
结构节点*T2=x->右侧;
//轮换
x->右=y;
y->左=T2;
//更新高度
y->高度=最大值(高度(y->左),高度(y->右))+1;
x->高度=最大值(高度(x->左),高度(x->右))+1;
//返回新根
返回x;
}
//左旋转以x为根的子树的实用函数
//见上图。
结构节点*leftRotate(结构节点*x)
{
结构节点*y=x->右侧;
结构节点*T2=y->左;
//轮换
y->左=x;
x->右=T2;
//更新高度
x->高度=最大值(高度(x->左),高度(x->右))+1;
y->高度=最大值(高度(y->左),高度(y->右))+1;
//返回新根
返回y;
}
//获取节点N的平衡因子
int getBalance(结构节点*N)
{
如果(N==NULL)
返回0;
返回高度(N->左)-高度(N->右);
}
//在子树根中插入键的递归函数
//,并返回子树的新根。
结构节点*插入(结构节点*节点,字符串键)
{
/*1.执行正常的BST插入*/
if(node==NULL)
返回(newNode(key));
如果(键<节点->键)
节点->左=插入(节点->左,键);
否则如果(键>节点->键)
节点->右侧=插入(节点->右侧,键);
else//BST中不允许使用相等的键
{
节点->计数器++;
返回节点;
}
/*2.更新此祖先节点的高度*/
节点->高度=1+最大值(高度(节点->左侧),
高度(节点->右侧);
/*3.获取该祖先的平衡因子
节点,以检查此节点是否成为
不平衡*/
int balance=getBalance(节点);
//如果此节点变得不平衡,则
//有4例
//左-左案例
如果(平衡>1&&keyleft->key)
返回右旋转(节点);
//正确的情况
如果(平衡<-1&&key>node->right->key)
返回leftRotate(节点);
//左右格
如果(平衡>1&&键>节点->左->键)
{
节点->左=左旋转(节点->左);
返回右旋转(节点);
}
//左右格
如果(余额<-1&&键<节点->右->键)
{
节点->右=右旋转(节点->右);
返回leftRotate(节点);
}
/*返回(未更改的)节点指针*/
返回节点;
}
//打印预订单遍历的实用函数
//这棵树的叶子。
//该函数还打印每个节点的高度
无效预订单(结构节点*根)
{
如果(根)
{
cout键(左);
预订单(根->右);
}
}
/*用于测试上述功能的干燥器程序*/
int main()
{
结构节点*root=nullptr;
/*构建上图中给出的树*/
根=插入(根,“a”);
根=插入(根,“bc”);
根=插入(根,“DE”);
根=插入(根,“op”);
根=插入(根,“lo”);
根=插入(根,“mp”);
/*根=插入(根,10);
根=插入(根,20);
根=插入(根,30);
根=插入(根,40);
根=插入(根,50);
根=插入(根,25)*/
printf(“构造的AVL的前序遍历”
“树是\n”);
前序(根);
返回0;
}
这里有一个问题:

struct Node*Node=(struct Node*)malloc(sizeof(struct Node))

这将无法正常工作。
节点
类将
std::string
作为成员,使用
malloc
创建动态实例将不会调用
std::string
的构造函数。
malloc
函数对
C++
构造函数或对象一无所知

C++
中,有一种称为
POD
(普通旧数据)类型,基本上是
C
兼容类型。
malloc
调用只能对
POD
类型正常工作。当您将
节点
成员从
int
更改为
std::string
时,您将
节点
从POD类型更改为非POD类型。一旦您的类型为非POD,创建实例的函数(如
malloc
)将无法按预期工作

malloc
调用只分配内存,其他什么都不分配。它不知道如何调用C++类的构造函数(例如:代码> STD::String ),因此,<<代码>节点< /C>对象有无效的、未构造的<代码> STD::String < /C>。使用它会导致出现未定义的行为

为了缓解这种情况,请使用
new
,而不是
malloc
来创建
节点的动态实例,因为
new
调用
struct Node *rightRotate(struct Node *y)
{
    struct Node *x = y->left;
    struct Node *T2 = x->right;
Node *rightRotate(Node *y)
{
    Node *x = y->left;
    Node *T2 = x->right;