二叉树,给x结点赋值

#include <iostream>
#include <sstream>
#include <vector>
#include <string>
 
using namespace std;
 
template<class T>
struct BinaryTreeNode
{
    T data;
    BinaryTreeNode<T>* LChild;
    BinaryTreeNode<T>* RChild;
 
    BinaryTreeNode(T item = T(), BinaryTreeNode<T>* L = nullptr, BinaryTreeNode<T>* R = nullptr)
        : data(item), LChild(L), RChild(R) {}
};
 
template<class T>
class BinaryTree
{
private:
    BinaryTreeNode<T>* root;
 
    void destroy(BinaryTreeNode<T>*& node)
    {
        if (node != nullptr)
        {
            destroy(node->LChild);
            destroy(node->RChild);
            delete node;
            node = nullptr;
        }
    }
 
    void preorder(BinaryTreeNode<T>* node, vector<T>& result)
    {
        if (node != nullptr)
        {
            result.push_back(node->data);
            preorder(node->LChild, result);
            preorder(node->RChild, result);
        }
    }
 
    void inorder(BinaryTreeNode<T>* node, vector<T>& result)
    {
        if (node != nullptr)
        {
            inorder(node->LChild, result);
            result.push_back(node->data);
            inorder(node->RChild, result);
        }
    }
 
    void postorder(BinaryTreeNode<T>* node, vector<T>& result)
    {
        if (node != nullptr)
        {
            postorder(node->LChild, result);
            postorder(node->RChild, result);
            result.push_back(node->data);
        }
    }
 
public:
    BinaryTreeNode<T>* getroot()
    {
        return root;
    }
    BinaryTree() : root(nullptr) {}   ~BinaryTree()
    {
        destroy(root);
    }

    void printResult(const vector<T>& result)
    {
        if (!result.empty())
        {
            cout << result[0];
            for (size_t i = 1; i < result.size(); ++i)
            {
                cout << ',' << result[i];
            }
        }
        cout << endl;
    }
 
    void createFromPreorder(vector<T> elements, T empty)
    {
        auto it = elements.begin();
        root = create(it, elements.end(), empty);
    }
 
    BinaryTreeNode<T>* create(typename vector<T>::iterator& it, typename vector<T>::iterator end, T empty)
    {
        if (it == end || *it == empty)
        {
            return nullptr;
        }
 
        BinaryTreeNode<T>* node = new BinaryTreeNode<T>(*it);
        ++it;
        node->LChild = create(it, end, empty);
        ++it;
        node->RChild = create(it, end, empty);
        return node;
    }
 
    void printPreorder()
    {
        vector<T> result;
        preorder(root, result);
        printResult(result);
    }
 
    void printInorder()
    {
        vector<T> result;
        inorder(root, result);
        printResult(result);
    }
 
    void printPostorder()
    {
        vector<T> result;
        postorder(root, result);
        printResult(result);
    }
    void ssign (BinaryTreeNode<T> *node, T x,T value,int &flag)
    {
        if(node!=nullptr)
        {
            if(node->data==x)
            {
                node->data=value;
                flag=1;
            }
        ssign(node->LChild,x,value,flag);
        ssign(node->RChild,x,value,flag);        
        
        }
    }




};
template <class T>
bool Ssign(BinaryTree<T> &t, T &x, T &value)
{
    int flag=0;
    t.ssign (t.getroot(), x, value,flag);

    if(flag==1)return true;
    return false;
}
 
int main()
{
    string nullSymbol;
    string preorderInput;
 
    getline(cin, nullSymbol);
    getline(cin, preorderInput);
 
    stringstream input(preorderInput);
    string item;
    vector<string> elements;
 
    while (input >> item)
    {
        elements.push_back(item);
    }
    string x,value;
    cin>>x>>value; 
    BinaryTree<string> tree;
    tree.createFromPreorder(elements, nullSymbol);
 
    tree.printPreorder();
    tree.printInorder();
    tree.printPostorder();
    cout<<endl;
    if(Ssign(tree,x,value))
    {
        cout<<"success"<<endl;
    tree.printPreorder();
    tree.printInorder();
    tree.printPostorder();
    }
        
    else
        cout<<"false";
 
    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个示例代码,其中给结点赋值的函数为setValue,遍历二叉树的函数为traverse,其他操作包括插入结点、查找结点、删除结点、计算树高等。 ``` #include <stdio.h> #include <stdlib.h> /* 二叉树结点定义 */ typedef struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; } TreeNode; /* 创建二叉树 */ TreeNode* createTree() { int val; scanf("%d", &val); if (val == -1) { /* 输入-1表示空结点 */ return NULL; } TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode)); root->val = val; root->left = createTree(); root->right = createTree(); return root; } /* 给结点赋值 */ void setValue(TreeNode* node, int val) { if (node == NULL) { return; } node->val = val; } /* 遍历二叉树 */ void traverse(TreeNode* root) { if (root == NULL) { return; } printf("%d ", root->val); traverse(root->left); traverse(root->right); } /* 插入结点 */ void insertNode(TreeNode* root, int val) { if (root == NULL) { return; } if (val < root->val) { if (root->left == NULL) { TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)); node->val = val; node->left = NULL; node->right = NULL; root->left = node; } else { insertNode(root->left, val); } } else { if (root->right == NULL) { TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)); node->val = val; node->left = NULL; node->right = NULL; root->right = node; } else { insertNode(root->right, val); } } } /* 查找结点 */ TreeNode* findNode(TreeNode* root, int val) { if (root == NULL) { return NULL; } if (root->val == val) { return root; } else if (val < root->val) { return findNode(root->left, val); } else { return findNode(root->right, val); } } /* 删除结点 */ TreeNode* deleteNode(TreeNode* root, int val) { if (root == NULL) { return NULL; } if (val < root->val) { root->left = deleteNode(root->left, val); } else if (val > root->val) { root->right = deleteNode(root->right, val); } else { if (root->left == NULL && root->right == NULL) { /* 叶子结点 */ free(root); root = NULL; } else if (root->left == NULL) { /* 只有右子树 */ TreeNode* temp = root; root = root->right; free(temp); } else if (root->right == NULL) { /* 只有左子树 */ TreeNode* temp = root; root = root->left; free(temp); } else { /* 左右子树都有 */ TreeNode* temp = root->right; while (temp->left != NULL) { temp = temp->left; } root->val = temp->val; root->right = deleteNode(root->right, temp->val); } } return root; } /* 计算树高 */ int getHeight(TreeNode* root) { if (root == NULL) { return 0; } int leftHeight = getHeight(root->left); int rightHeight = getHeight(root->right); return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1; } int main() { printf("请输入二叉树结点,-1表示空结点:"); TreeNode* root = createTree(); printf("遍历二叉树结果为:"); traverse(root); printf("\n"); printf("给结点赋值为10\n"); setValue(root, 10); printf("插入结点5\n"); insertNode(root, 5); printf("查找结点7\n"); TreeNode* node = findNode(root, 7); if (node == NULL) { printf("未找到结点7\n"); } else { printf("找到结点7,值为%d\n", node->val); } printf("删除结点10\n"); root = deleteNode(root, 10); printf("计算树高为%d\n", getHeight(root)); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值