Beycan Kahraman >> B-Trees
B-Tree-Search(x, k)

  i <- 1
  while i <= n[x] and k > keyi[x]
      do i <- i + 1
  if i <= n[x] and k = keyi[x]
      then return (x, i)
  if leaf[x]
      then return NIL
  else Disk-Read(ci[x])
      return B-Tree-Search(ci[x], k)

B-Tree-Create(T)

  x <- Allocate-Node()
  leaf[x] <- TRUE
  n[x] <- 0
  Disk-Write(x)
  root[T] <- x

B-Tree-Split-Child(x, i, y)

  z <- Allocate-Node()
  leaf[z] <- leaf[y]
  n[z] <- t - 1
  for j <- 1 to t - 1
      do keyj[z] <- keyj+t[y]
  if not leaf[y]
      then for j <- 1 to t
          do cj[z] <- cj+t[y]
  n[y] <- t - 1
  for j <- n[x] + 1 downto i + 1
      do cj+1[x] <- cj[x]
  ci+1 <- z
  for j <- n[x] downto i
      do keyj+1[x] <- keyj[x]
  keyi[x] <- keyt[y]
  n[x] <- n[x] + 1
  Disk-Write(y)
  Disk-Write(z)
  Disk-Write(x)

B-Tree-Insert(T, k)

  r <- root[T]
  if n[r] = 2t - 1
      then s <- Allocate-Node()
      root[T] <- s
      leaf[s] <- FALSE
      n[s] <- 0
      c1 <- r
      B-Tree-Split-Child(s, 1, r)
      B-Tree-Insert-Nonfull(s, k)
  else B-Tree-Insert-Nonfull(r, k)

B-Tree-Insert-Nonfull(x, k)

  i <- n[x]
  if leaf[x]
      then while i >= 1 and k < keyi[x]
          do keyi+1[x] <- keyi[x]
          i <- i - 1
          keyi+1[x] <- k
          n[x] <- n[x] + 1
          Disk-Write(x)
      else while i >= and k < keyi[x]
          do i <- i - 1
          i <- i + 1
          Disk-Read(ci[x])
          if n[ci[x]] = 2t - 1
              then B-Tree-Split-Child(x, i, ci[x])
                  if k > keyi[x]
                      then i <- i + 1
          B-Tree-Insert-Nonfull(ci[x], k)


>> Download Example Code download