POV-Ray : Newsgroups : povray.general : untested red-black tree using AI : Re: untested red-black tree using AI Server Time
9 Mar 2025 18:46:18 EDT (-0400)
  Re: untested red-black tree using AI  
From: ingo
Date: 7 Mar 2025 04:25:00
Message: <web.67cabab3491e931a17bac71e8ffb8ce3@news.povray.org>
the same thing with Mistral:

python:
# Constants for colors
RED = 0
BLACK = 1

# Node structure: [value, color, left_index, right_index, parent_index]
tree = []

def create_node(value, color, left, right, parent):
    return [value, color, left, right, parent]

def left_rotate(index):
    right_child_index = tree[index][3]
    tree[index][3] = tree[right_child_index][2]

    if tree[right_child_index][2] != None:
        tree[tree[right_child_index][2]][4] = index

    tree[right_child_index][4] = tree[index][4]

    if tree[index][4] == None:
        root_index = right_child_index
    elif index == tree[tree[index][4]][2]:
        tree[tree[index][4]][2] = right_child_index
    else:
        tree[tree[index][4]][3] = right_child_index

    tree[right_child_index][2] = index
    tree[index][4] = right_child_index

def right_rotate(index):
    left_child_index = tree[index][2]
    tree[index][2] = tree[left_child_index][3]

    if tree[left_child_index][3] != None:
        tree[tree[left_child_index][3]][4] = index

    tree[left_child_index][4] = tree[index][4]

    if tree[index][4] == None:
        root_index = left_child_index
    elif index == tree[tree[index][4]][3]:
        tree[tree[index][4]][3] = left_child_index
    else:
        tree[tree[index][4]][2] = left_child_index

    tree[left_child_index][3] = index
    tree[index][4] = left_child_index

def insert_fixup(index):
    while tree[tree[index][4]][1] == RED:
        if tree[index][4] == tree[tree[tree[index][4]][4]][2]:
            y = tree[tree[tree[index][4]][4]][3]
            if y != None and tree[y][1] == RED:
                tree[tree[index][4]][1] = BLACK
                tree[y][1] = BLACK
                tree[tree[tree[index][4]][4]][1] = RED
                index = tree[tree[index][4]][4]
            elif index == tree[tree[index][4]][3]:
                index = tree[index][4]
                left_rotate(index)
            tree[tree[index][4]][1] = BLACK
            tree[tree[tree[index][4]][4]][1] = RED
            right_rotate(tree[tree[index][4]][4])
        else:
            y = tree[tree[tree[index][4]][4]][2]
            if y != None and tree[y][1] == RED:
                tree[tree[index][4]][1] = BLACK
                tree[y][1] = BLACK
                tree[tree[tree[index][4]][4]][1] = RED
                index = tree[tree[index][4]][4]
            elif index == tree[tree[index][4]][2]:
                index = tree[index][4]
                right_rotate(index)
            tree[tree[index][4]][1] = BLACK
            tree[tree[tree[index][4]][4]][1] = RED
            left_rotate(tree[tree[index][4]][4])
    tree[root_index][1] = BLACK

def insert(value):
    global root_index
    new_node = create_node(value, RED, None, None, None)
    tree.append(new_node)
    new_index = len(tree) - 1

    if root_index is None:
        root_index = new_index
    else:
        current_index = root_index
        while True:
            if value < tree[current_index][0]:
                if tree[current_index][2] is None:
                    tree[current_index][2] = new_index
                    tree[new_index][4] = current_index
                    break
                else:
                    current_index = tree[current_index][2]
            else:
                if tree[current_index][3] is None:
                    tree[current_index][3] = new_index
                    tree[new_index][4] = current_index
                    break
                else:
                    current_index = tree[current_index][3]

    insert_fixup(new_index)

def inorder_traversal(index):
    if index is not None:
        inorder_traversal(tree[index][2])
        print(f'Value: {tree[index][0]}, Color: {"RED" if tree[index][1] == RED
else "BLACK"}')
        inorder_traversal(tree[index][3])

# Example usage
root_index = None
insert(10)
insert(20)
insert(30)
insert(15)
insert(25)

print("Inorder Traversal of the Red-Black Tree:")
inorder_traversal(root_index)



POV-Ray:

#version 3.7;
global_settings {
  assumed_gamma 1.0
}

#include "colors.inc"

// Constants for colors
#declare RED = rgb <1, 0, 0>;
#declare BLACK = rgb <0, 0, 0>;

// Node structure: [value, color, left_index, right_index, parent_index]
#declare Tree = array[50]; // Array to store tree nodes
#declare NodeSize = 0.5;
#declare NodeSpacing = 2.0;
#declare RootIndex = 0;

// Macro to create a node
#macro CreateNode(index, value, color, left, right, parent)
  #declare Tree[index] = array[6];
  #declare Tree[index][0] = value;
  #declare Tree[index][1] = color;
  #declare Tree[index][2] = left;
  #declare Tree[index][3] = right;
  #declare Tree[index][4] = parent;
  #declare Tree[index][5] = 0; // Flag to check if the node is used
  #declare Tree[index][5] = 1;
#end

// Macro to draw a node
#macro DrawNode(index)
  #if (Tree[index][5] = 1)
    sphere {
      <Tree[index][0] * NodeSpacing, -index * NodeSpacing, 0>, NodeSize
      texture {
        pigment { Tree[index][1] }
        finish { ambient 0.1 diffuse 0.6 }
      }
    }
  #end
#end

// Macro to draw an edge
#macro DrawEdge(parent_index, child_index)
  #if (Tree[child_index][5] = 1)
    cylinder {
      <Tree[parent_index][0] * NodeSpacing, -parent_index * NodeSpacing, 0>,
      <Tree[child_index][0] * NodeSpacing, -child_index * NodeSpacing, 0>,
      NodeSize / 4
      texture {
        pigment { BLACK }
        finish { ambient 0.1 diffuse 0.6 }
      }
    }
  #end
#end

// Macro to insert a value into the tree
#macro Insert(value)
  #declare new_index = 0;
  #while (Tree[new_index][5] = 1 & new_index < 50)
    #declare new_index = new_index + 1;
  #end
  #if (new_index < 50)
    #declare current_index = RootIndex;
    #while (Tree[current_index][5] = 1)
      #if (value < Tree[current_index][0])
        #if (Tree[current_index][2] = -1)
          #declare Tree[current_index][2] = new_index;
          #break
        #else
          #declare current_index = Tree[current_index][2];
        #end
      #else
        #if (Tree[current_index][3] = -1)
          #declare Tree[current_index][3] = new_index;
          #break
        #else
          #declare current_index = Tree[current_index][3];
        #end
      #end
    #end
    CreateNode(new_index, value, RED, -1, -1, current_index)
    #if (RootIndex = 0)
      #declare RootIndex = new_index;
    #end
  #end
#end

camera {
  location <0, -15, 10>
  look_at <0, 0, 0>
}

light_source {
  <2, -4, 5>
  color rgb <1, 1, 1>
}

// Insert values into the tree
Insert(10)
Insert(20)
Insert(30)
Insert(15)
Insert(25)

// Draw the tree
#declare index = 0;
#while (index < 50)
  DrawNode(index)
  #if (Tree[index][2] != -1)
    DrawEdge(index, Tree[index][2])
  #end
  #if (Tree[index][3] != -1)
    DrawEdge(index, Tree[index][3])
  #end
  #declare index = index + 1;
#end


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.