重载操作符‘左左’

重载操作符简例

#include<stdio.h>

//使out类实现一部分类似cout的功能

class out
{
public:
	out& operator <<(const int value)
	{
		printf("%d", value);
		return *this;
	}
	out& operator <<(const short value)
	{
		printf("%d", value);
		return *this;
	}
	out& operator <<(const double value)
	{
		printf("%f", value);
		return *this;
	}
	out& operator <<(const float value)
	{
		printf("%f", value);
		return *this;
	}
	out& operator <<(const char *value)
	{
		printf("%s", value);
		return *this;
	}
	out& operator <<(const char value)
	{
		printf("%c", value);
		return *this;
	}

};

int main()
{
	out zout;
	zout<<1<<"hahahaha"<<'\n';
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
平衡二叉树的删除操作是一个比较复杂的过程,需要考虑到平衡性的维护。下面是一个简单的C语言代码示例,展示了平衡二叉树的删除操作: ```c #include <stdio.h> #include <stdlib.h> // 定义平衡二叉树节点结构 struct AVLNode { int data; struct AVLNode* left; struct AVLNode* right; int height; }; // 获取节点的高度 int getHeight(struct AVLNode* node) { if (node == NULL) { return 0; } return node->height; } // 获取两个数中较大的数 int max(int a, int b) { return (a > b) ? a : b; } // 创建一个新的节点 struct AVLNode* createNode(int data) { struct AVLNode* newNode = (struct AVLNode*)malloc(sizeof(struct AVLNode)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; newNode->height = 1; return newNode; } // 右旋操作 struct AVLNode* rightRotate(struct AVLNode* y) { struct AVLNode* x = y->left; struct AVLNode* T2 = x->right; // 执行旋转 x->right = y; y->left = T2; // 更新高度 y->height = max(getHeight(y->left), getHeight(y->right)) + 1; x->height = max(getHeight(x->left), getHeight(x->right)) + 1; return x; } // 旋操作 struct AVLNode* leftRotate(struct AVLNode* x) { struct AVLNode* y = x->right; struct AVLNode* T2 = y->left; // 执行旋转 y->left = x; x->right = T2; // 更新高度 x->height = max(getHeight(x->left), getHeight(x->right)) + 1; y->height = max(getHeight(y->left), getHeight(y->right)) + 1; return y; } // 获取平衡因子 int getBalanceFactor(struct AVLNode* node) { if (node == NULL) { return 0; } return getHeight(node->left) - getHeight(node->right); } // 插入节点 struct AVLNode* insertNode(struct AVLNode* node, int data) { // 执行普通的BST插入操作 if (node == NULL) { return createNode(data); } if (data < node->data) { node->left = insertNode(node->left, data); } else if (data > node->data) { node->right = insertNode(node->right, data); } else { // 不允许插入相同的节点 return node; } // 更新节点的高度 node->height = 1 + max(getHeight(node->left), getHeight(node->right)); // 获取平衡因子 int balanceFactor = getBalanceFactor(node); // 平衡维护 // 情况,需要进行右旋操作 if (balanceFactor > 1 && data < node->left->data) { return rightRotate(node); } // 右右情况,需要进行旋操作 if (balanceFactor < -1 && data > node->right->data) { return leftRotate(node); } // 右情况,先进行旋再进行右旋 if (balanceFactor > 1 && data > node->left->data) { node->left = leftRotate(node->left); return rightRotate(node); } // 右情况,先进行右旋再进行旋 if (balanceFactor < -1 && data < node->right->data) { node->right = rightRotate(node->right); return leftRotate(node); } return node; } // 查找最小值节点 struct AVLNode* findMinNode(struct AVLNode* node) { struct AVLNode* current = node; while (current->left != NULL) { current = current->left; } return current; } // 删除节点 struct AVLNode* deleteNode(struct AVLNode* root, int data) { // 执行BST的删除操作 if (root == NULL) { return root; } if (data < root->data) { root->left = deleteNode(root->left, data); } else if (data > root->data) { root->right = deleteNode(root->right, data); } else { // 找到要删除的节点 // 节点只有一个子节点或没有子节点的情况 if ((root->left == NULL) || (root->right == NULL)) { struct AVLNode* temp = root->left ? root->left : root->right; // 没有子节点的情况 if (temp == NULL) { temp = root; root = NULL; } else { // 有一个子节点的情况 *root = *temp; } free(temp); } else { // 节点有两个子节点的情况,找到右子树中的最小值节点进行替换 struct AVLNode* temp = findMinNode(root->right); root->data = temp->data; root->right = deleteNode(root->right, temp->data); } } // 如果树中只有一个节点,则返回 if (root == NULL) { return root; } // 更新节点的高度 root->height = 1 + max(getHeight(root->left), getHeight(root->right)); // 获取平衡因子 int balanceFactor = getBalanceFactor(root); // 平衡维护 // 情况,需要进行右旋操作 if (balanceFactor > 1 && getBalanceFactor(root->left) >= 0) { return rightRotate(root); } // 右右情况,需要进行旋操作 if (balanceFactor < -1 && getBalanceFactor(root->right) <= 0) { return leftRotate(root); } // 右情况,先进行旋再进行右旋 if (balanceFactor > 1 && getBalanceFactor(root->left) < 0) { root->left = leftRotate(root->left); return rightRotate(root); } // 右情况,先进行右旋再进行旋 if (balanceFactor < -1 && getBalanceFactor(root->right) > 0) { root->right = rightRotate(root->right); return leftRotate(root); } return root; } // 中序遍历二叉树 void inorderTraversal(struct AVLNode* root) { if (root != NULL) { inorderTraversal(root->left); printf("%d ", root->data); inorderTraversal(root->right); } } int main() { struct AVLNode* root = NULL; // 插入节点 root = insertNode(root, 9); root = insertNode(root, 5); root = insertNode(root, 10); root = insertNode(root, 0); root = insertNode(root, 6); root = insertNode(root, 11); root = insertNode(root, -1); root = insertNode(root, 1); root = insertNode(root, 2); printf("平衡二叉树中序遍历结果:"); inorderTraversal(root); printf("\n"); // 删除节点 root = deleteNode(root, 10); printf("删除节点后的平衡二叉树中序遍历结果:"); inorderTraversal(root); printf("\n"); return 0; } ``` 这段代码实现了平衡二叉树的删除操作,包括节点的插入、右旋、旋、获取高度、获取平衡因子等功能。你可以根据需要进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值