view Untitled.ipynb @ 12:77f0530f2eff

fix typo
author soto
date Thu, 11 Feb 2021 17:30:33 +0900
parents ce192a384cb6
children
line wrap: on
line source

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding: utf-8\n",
    "#\n",
    "# llrbnode23.py : Left-Leaning Red Black Tree 用操作関数\n",
    "#                 (2-3 木バージョン)\n",
    "#\n",
    "#                 Copyright (C) 2009 Makoto Hiroi\n",
    "#\n",
    "\n",
    "# 終端\n",
    "null = None\n",
    "\n",
    "# 色\n",
    "BLACK = 0\n",
    "RED = 1\n",
    "\n",
    "# 節\n",
    "class Node:\n",
    "    def __init__(self, x, color = RED):\n",
    "        self.data = x\n",
    "        self.color = color\n",
    "        self.left = null\n",
    "        self.right = null\n",
    "\n",
    "# 終端の設定\n",
    "def make_null():\n",
    "    global null\n",
    "    if null is None:\n",
    "        null = Node(None, BLACK)\n",
    "        null.left = None\n",
    "        null.right = None\n",
    "    return null\n",
    "\n",
    "# 右回転\n",
    "def rotate_right(node):\n",
    "    lnode = node.left\n",
    "    node.left = lnode.right\n",
    "    lnode.right = node\n",
    "    lnode.color = node.color\n",
    "    node.color = RED\n",
    "    return lnode\n",
    "\n",
    "# 左回転\n",
    "def rotate_left(node):\n",
    "    rnode = node.right\n",
    "    node.right = rnode.left\n",
    "    rnode.left = node\n",
    "    rnode.color = node.color\n",
    "    node.color = RED\n",
    "    return rnode\n",
    "\n",
    "\n",
    "#\n",
    "# データの挿入\n",
    "#\n",
    "\n",
    "# 4 node の分割\n",
    "def split(node):\n",
    "    node.color = RED\n",
    "    node.left.color = BLACK\n",
    "    node.right.color = BLACK\n",
    "\n",
    "# 挿入\n",
    "def insert(node, x):\n",
    "    if node is null: return Node(x)\n",
    "    if x < node.data:\n",
    "        node.left = insert(node.left, x)\n",
    "    elif x > node.data:\n",
    "        node.right = insert(node.right, x)\n",
    "    # skew\n",
    "    if node.right.color == RED:\n",
    "        print(\"hoge\",node.right.data)\n",
    "        print_node(node, 0)\n",
    "        node = rotate_left(node)\n",
    "    # split\n",
    "    if node.left.color == RED and node.left.left.color == RED:\n",
    "        node = rotate_right(node)\n",
    "        split(node)\n",
    "    return node\n",
    "\n",
    "# 木の表示\n",
    "def print_node(node, x):\n",
    "    if node is not null:\n",
    "        print_node(node.right, x + 1)\n",
    "        print(\"    \" * x, node.color, node.data)\n",
    "        print_node(node.left, x + 1)\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "root = make_null()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "root = insert(root, 0)\n",
    "root.color = BLACK\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         0 0\n"
     ]
    }
   ],
   "source": [
    "print_node(root, 2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hoge 1\n",
      "     1 1\n",
      " 0 0\n"
     ]
    }
   ],
   "source": [
    "root = insert(root, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 0 0\n"
     ]
    }
   ],
   "source": [
    "print_node(root, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "type object 'Node' has no attribute 'make_null'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-52-6016b722b985>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mNode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmake_null\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m: type object 'Node' has no attribute 'make_null'"
     ]
    }
   ],
   "source": [
    "a=Node.make_null()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = Node.insert(a, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'print_node' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-44-30dbdc80e254>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mNode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprint_node\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m<ipython-input-19-ffab890a4141>\u001b[0m in \u001b[0;36mprint_node\u001b[0;34m(node, x)\u001b[0m\n\u001b[1;32m     62\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mprint_node\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     63\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mnode\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mnull\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 64\u001b[0;31m             \u001b[0mprint_node\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mleft\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     65\u001b[0m             \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\" \"\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolor\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     66\u001b[0m             \u001b[0mprint_node\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mright\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'print_node' is not defined"
     ]
    }
   ],
   "source": [
    "Node.print_node(a, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}