http://kmlinux.fjfi.cvut.cz/~culik/zalg/Tree05.zip

#include "stdafx.h"
 
#include "form.h"
#include "tree.h"
 
#include <string>
using namespace std;
 
const int MAX = 4;
 
struct Item
{
    int cnt; // pocet klicu , 1 <= cnt <= MAX ( obcas MAX+1 )
    int key [MAX + 1]; // key [0] ... key [cnt-1]
    Item * ptr[MAX + 2]; // ptr [0] ... ptr [cnt],  ukazatelu je o jedna vice nez klicu
    Item ();
};
 
Item::Item() 
{
    cnt = 0;
    for (int i = 0; i <= MAX; i++) key[i] = -1;
    for (int i = 0; i <= MAX+1; i++) ptr[i] = nullptr;
}
 
void insert (Item * p, int inx, int new_key, Item * new_ptr)
// vlozit do bloku p
// vlozit na pozici inx , (0 <= inx <= p->cnt)
// vlozit novy klic a za nim novy ukazatel
{
    for (int i = p->cnt - 1; i >= inx; i--)
    {
        p->key[i + 1] = p->key[i]; 
    }
    for (int i = p->cnt; i >= inx + 1; i--)
    {
        p->ptr[i + 1] = p->ptr[i];
    }
    p->key[inx] = new_key;
    p->ptr[inx + 1] = new_ptr;
    p->cnt++;
 
}
 
void enter (Item * & p, int val)
{
    if (p == nullptr) {
        p = new Item;
        p->cnt = 1; p->ptr[0] = nullptr; p->key[0] = val; p->ptr[1] = nullptr;
    }
    else {
        int i = 0;
        while (i < p->cnt && p->key[i] < val) { i++; }
 
        if (i < p->cnt && val == p->key[i]) {}
        else {
            insert(p, i, val, nullptr);
        }
    }
}
 
void display (Item * p)
{
    if (p != nullptr) 
    {
        openBranch("");
        for (int i = 0; i < p->cnt; i++) addNode (to_string (p->key[i]));
        closeBranch();
    }
}
 
void run()
{
    Item * root = nullptr;
    enter(root, 30);
    enter(root, 20);
    enter(root, 25);
    /*
    enter(root, 40);
    enter(root, 7);
    */
 
    display(root);
    print ("Hello");
    status ("O.K.");
}
 
avl_ut_2019.txt · Last modified: 2019/05/07 11:15 by 147.32.8.110
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki