zoukankan      html  css  js  c++  java
  • Geeks Splay Tree Insert 树的插入操作

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载。 https://blog.csdn.net/kenden23/article/details/27665457

    Splay树的插入操作。仅仅须要处理好插入节点的孩子节点就能够了。最重要的是不要破坏了BST的基本规则。

    由于高度并非Splay树的首要因素,所以插入的时候也是使用splay操作,然后在根节点插入。

    參考:http://www.geeksforgeeks.org/splay-tree-set-2-insert-delete/


    对照一下使用插入创建的树和手工创建数的差别,先序遍历的结果:



    #pragma once
    #include<stdio.h>
    #include <stdlib.h>
    
    class SplayTree_Insertion
    {
    	struct Node
    	{
    		int key;
    		Node *left, *right;
    		Node(int k) : key(k), left(NULL), right(NULL) {}
    		~Node()
    		{
    			if (left) delete left, left = NULL;
    			if (right) delete right, right = NULL;
    		}
    	};
    
    	Node *rightRotate(Node *x)
    	{
    		Node *y = x->left;
    		x->left = y->right;
    		y->right = x;
    
    		return y;
    	}
    
    	Node *leftRotate(Node *x)
    	{
    		Node *y = x->right;
    		x->right = y->left;
    		y->left = x;
    
    		return y;
    	}
    
    	Node *splay(Node *root, const int key)
    	{
    		if (!root || key == root->key) return root;
    
    		if (key < root->key)
    		{
    			if (!root->left) return root;
    
    			if (key < root->left->key)
    			{
    				root->left->left = splay(root->left->left, key);
    				root = rightRotate(root);//不应root->left
    			}
    			else if (root->left->key < key)
    			{
    				root->left->right = splay(root->left->right, key);
    				if (root->left->right) root->left = leftRotate(root->left);
    			}
    			return root->left? rightRotate(root) : root;
    		}
    		if (!root->right) return root;
    		if (root->right->key < key)
    		{
    			root->right->right = splay(root->right->right, key);
    			root = leftRotate(root);
    		}
    		else if (key < root->right->key)
    		{
    			root->right->left = splay(root->right->left, key);
    			if (root->right->left) root->right = rightRotate(root->right);
    		}
    		return root->right?

    leftRotate(root) : root; } Node *insert(Node *root, int k) { if (!root) return new Node(k); root = splay(root, k); if (root->key == k) return root; Node *newNode = new Node(k); //learn how to handle the insertion is the best way. if (k < root->key) { newNode->right = root; newNode->left = root->left; root->left = NULL; } else { newNode->left = root; newNode->right = root->right; root->right = NULL; } return newNode; } void preOrder(Node *root) { if (root != NULL) { printf("%d ", root->key); preOrder(root->left); preOrder(root->right); } } public: SplayTree_Insertion() { Node *root = new Node(100); root->left = new Node(50); root->right = new Node(200); root->left->left = new Node(40); root->left->left->left = new Node(30); root->left->left->left->left = new Node(20); root = insert(root, 25); printf("Preorder traversal of the modified Splay tree is "); preOrder(root); putchar(' '); delete root; runInsert(); } void runInsert() { Node *root = NULL; int keys[] = {100, 50, 200, 40, 30, 20, 25}; int n = sizeof(keys) / sizeof(keys[0]); for (int i = 0; i < n; i++) { root = insert(root, keys[i]); } printf("Inser create Preorder traversal Splay tree is "); preOrder(root); putchar(' '); delete root; } };




  • 相关阅读:
    Windows 8实用窍门系列:18.windows 8开发模拟器和windows 8程序中关联文件类型
    Silverlight实用窍门系列:75.Silverlight中DataGrid制作复杂表头
    Windows 8实用窍门系列:11.Windows 8 中的Toast Tile Badge通知
    Windows 8实用窍门系列:9.Windows 8中使用FlipView
    Windows 8实用窍门系列:12.windows 8的文件管理1.File创建和String Stream Buffer方式读写
    Silverlight实用窍门系列:74.Silverlight使用Perst数据库Demo
    Windows 8实用窍门系列:17.文件选择器 文件保存器 文件夹选择器
    Windows 8实用窍门系列:16.Windows 8的右键菜单
    Windows 8实用窍门系列:3.第一个拆分布局应用程序修改Logo
    Windows 8实用窍门系列:19.Windows 8中的GridView使用(一)
  • 原文地址:https://www.cnblogs.com/mqxnongmin/p/10803458.html
Copyright © 2011-2022 走看看