You are on page 1of 85

8

-




, ..
.

-
- ()

2

-

(K, L, F, G, M, I J)
n1, n2, nk,
ni-1 ni, i*2, k+ (A, B, E)

()

e
,

-

( ,
2
A, B, E).

,
.. (
,
,
)
E ,
.
5

Archy

George

Frank

Colin

Joe

Fred

David

Maggie

Ann

Susie

Jeff

Emma

Jon

- C
#include <stdio.h>
#include <malloc.h>
#define MEMCHECK(x) if(x==NULL) { printf("Nema memorija!\n"); exit(-1); }
typedef char* info_t;
typedef struct element {
info_t info;
struct element *parent;
struct element *sibling;
struct element *firstChild;
} node;

typedef struct tree_element {


node *root;
} tree;
void initialize(tree *t) {
t->root = NULL;
}
7

- C
int childCount(node *n) {
node *tmp = n->firstChild;
int num = 0;
while (tmp != NULL) {
tmp = tmp->sibling;
num++;
}
return num;
}
void makeRoot(info_t elem, tree *t) {
t->root = (node *)malloc(sizeof(node));
t->root->info = elem;
t->root->parent = NULL;
t->root->sibling = NULL;
t->root->firstChild = NULL;
}

- C
node* parent(node *n) {
return n->parent;
}
node* addChild(node *n, info_t elem) {
node *tmp = (node *)malloc(sizeof(node));
tmp->info = elem;
tmp->parent = n;
tmp->firstChild = NULL;
tmp->sibling = n->firstChild;
n->firstChild = tmp;
return tmp;
}

- C
void removeNode(node *n, tree *t) {
node *tmp;
if (n->parent != NULL) {
if (n->parent->firstChild == n) {
// n e prvo dete na tatkoto
n->parent->firstChild = n->sibling;
} else {
// go barame "prethodnikot" na n
tmp = n->parent->firstChild;
while (tmp->sibling != n)
tmp = tmp->sibling;
tmp->sibling = n->sibling;
}
} else {
// jazelot n e korenot na drvoto
t->root = NULL;
}
deleteRecursively(n);
}
10

- C
void deleteRecursively(node *n) {
node *tmp, *next;
for (tmp=n->firstChild;tmp!=NULL;) {
next = tmp->sibling;
deleteRecursively(tmp);
tmp = next;
}
free(n);
}

11

- Java
import java.util.Iterator;

public interface Tree<E> {


public
public
public
public
public
public
public

Tree.Node<E> root();
Tree.Node<E> parent(Tree.Node<E> node);
int childCount(Tree.Node<E> node);
void makeRoot(E elem);
Tree.Node<E> addChild(Tree.Node<E> node, E elem);
void remove(Tree.Node<E> node);
Iterator<E> children(Tree.Node<E> node);

////////Inner interface for tree nodes ////////


public interface Node<E> {
public E getElement();
public void setElement(E elem);
}
}

12

- Java
public class SLLTree<E> implements Tree<E> {
// SLLNode is the implementation of the Node interface
class SLLNode<P> implements Node<P> {


SLL Node

// Holds the links to the needed nodes


SLLNode<P> parent, sibling, firstChild;
// Hold the data
P element;
public SLLNode(P o) {
element = o;
parent = sibling = firstChild = null;
}
public P getElement() {
return element;
}
public void setElement(P o) {
element = o;
}
}

13

- Java

protected SLLNode<E> root;

public SLLTree() {
root = null;
}
public Node<E> root() {
return root;
}

public Tree.Node<E> parent(Tree.Node<E> node) {


return ((SLLNode<E>) node).parent;
}
public int childCount(Tree.Node<E> node) {
SLLNode<E> tmp = ((SLLNode<E>) node).firstChild;
int num = 0;
while (tmp != null) {
tmp = tmp.sibling;
num++;
}
return num;
}

14

- Java
public void makeRoot(E elem) {
root = new SLLNode<E>(elem);
}
public Node<E> addChild(Node<E> node, E elem) {
SLLNode<E> tmp = new SLLNode<E>(elem);
SLLNode<E> curr = (SLLNode<E>) node;
tmp.sibling = curr.firstChild;
curr.firstChild = tmp;
tmp.parent = curr;
return tmp;
}

public Iterator<E> children(Tree.Node<E> node) {


return new SLLTreeChildIterator<E>(((SLLNode<E>) node).firstChild);
}

15

- Java
public void remove(Tree.Node<E> node) {
SLLNode<E> curr = (SLLNode<E>) node;
if (curr.parent != null) {
if (curr.parent.firstChild == curr) {
// The node is the first child of its parent
// Reconnect the parent to the next sibling
curr.parent.firstChild = curr.sibling;
} else {
// The node is not the first child of its parent
// Start from the first and search the node
// in the sibling list and remove it
SLLNode<E> tmp = curr.parent.firstChild;
while (tmp.sibling != curr) {
tmp = tmp.sibling;
}
tmp.sibling = curr.sibling;
}
} else {
root = null;
}
}

16

- Java
class SLLTreeChildIterator<T> implements Iterator<T> {
SLLNode<T> start, current;

public SLLTreeIterator(SLLNode<T> node) {


start = node;
current = node;
}
public boolean hasNext() {
return (current != null);
}
public T next() throws NoSuchElementException {
if (current != null) {
SLLNode<T> tmp = current;
current = current.sibling;
return tmp.getElement();
} else {
throw new NoSuchElementException();
}
}
}
17

1

.
,

.

18

1 - C
void printTreeRecursive(node *n, int level) {
if (n == NULL)
return;
int i;
node *tmp;
for (i=0;i<level;i++)
printf(" ");
printf("%s\n", n->info);
tmp = n->firstChild;
while (tmp != NULL) {
printTreeRecursive(tmp, level+1);
tmp = tmp->sibling;
}
}
void printTree(tree *t) {
printTreeRecursive(t->root, 0);
}
19

1 - C
int main() {
node *a, *b, *c;
tree *t = (tree *)malloc(sizeof(tree));
makeRoot("C:", t);
a = addChild(t->root, "Program files");
b = addChild(a, "CodeBlocks");
c = addChild(b, "codeblocks.dll");
c = addChild(b, "codeblocks.exe");
b = addChild(a, "Nodepad++");
c = addChild(b, "langs.xml");
c = addChild(b, "notepad++.exe");
a = addChild(t->root, "Users");
b = addChild(a, "Darko");
c = addChild(b, "Desktop");
c = addChild(b, "Downloads");
b = addChild(a, "Public");
a = addChild(t->root, "Windows");
b = addChild(a, "Media");
printTree(t);
return 0;
}

20

1 - Java
void printTreeRecursive(Node<E> node, int level) {
if (node == null)
return;
int i;
SLLNode<E> tmp;
for (i=0;i<level;i++)
System.out.print(" ");
System.out.println(node.getElement().toString());
tmp = ((SLLNode<E>)node).firstChild;
while (tmp != null) {
printTreeRecursive(tmp, level+1);
tmp = tmp.sibling;
}
}

public void printTree() {


printTreeRecursive(root, 0);
}
21

1 - Java
public static void main(String[] args) {
Tree.Node<String> a,b,c,d;
SLLTree t = new SLLTree();
t.makeRoot("C:");
a = t.addChild(t.root, "Program files");
b = t.addChild(a, "CodeBlocks");
c = t.addChild(b, "codeblocks.dll");
c = t.addChild(b, "codeblocks.exe");
b = t.addChild(a, "Nodepad++");
c = t.addChild(b, "langs.xml");
c = t.addChild(b, "notepad++.exe");
a = t.addChild(t.root, "Users");
b = t.addChild(a, "Darko");
c = t.addChild(b, "Desktop");
c = t.addChild(b, "Downloads");
c = t.addChild(b, "My Documents");
c = t.addChild(b, "My Pictures");
b = t.addChild(a, "Public");
a = t.addChild(t.root, "Windows");
b = t.addChild(a, "Media");
t.printTree();
}

22

2

.

23

2 - C
int max(int a, int b) {
if (a > b)
return a;
return b;
}
int countMaxChildrenRecursive(node *n) {
int p = childCount(n);
node *tmp = n->firstChild;
while (tmp != NULL) {
p = max(p, countMaxChildrenRecursive(tmp));
tmp = tmp->sibling;
}
return p;
}

int countMaxChildren(tree *t) {


return countMaxChildrenRecursive(t->root);
}
24

2 - Java
public int countMaxChildren() {
return countMaxChildrenRecursive(root);
}
int countMaxChildrenRecursive(SLLNode<E> node) {
int t = childCount(node);
SLLNode<E> tmp = node.firstChild;
while (tmp != null) {
t = Math.max(t, countMaxChildrenRecursive(tmp));
tmp = tmp.sibling;
}
return t;
}

25





:
,
-



26



,
.

:

27



1,
,
i 2i-1,
i>=1
n
(
) d 2d-1,
d>=1. :
d

n 2i 1 2 d 1
i 1

28


: n 2 d 1 .
, ,
d log 2 (n 1)
,

n,
d log 2 (n 1) .

,
,

.
29

- C
#include <stdio.h>
#include <malloc.h>
#define MEMCHECK(x) if(x==NULL) { printf("Nema memorija!\n"); exit(-1); }
#define LEFT 1
#define RIGHT 2
typedef char info_t;

typedef struct bNodeType {


info_t info;
struct bNodeType *left;
struct bNodeType *right;
} bNode;
typedef struct bTreeType {
bNode *root;
} bTree;

30

- C
void initialize(bTree *t) {
t->root = NULL;
}
void makeRoot(info_t elem, bTree *t) {
t->root = (bNode *)malloc(sizeof(bNode));
t->root->info = elem;
t->root->left = NULL;
t->root->right = NULL;
}

31

- C
bNode* addChild(bNode *n, int where, info_t elem) {
bNode *tmp = (bNode *)malloc(sizeof(bNode));
tmp->info = elem;
tmp->left = NULL;
tmp->right = NULL;
if (where == LEFT) {
if (n->left != NULL)
return NULL;
n->left = tmp;
} else {
if (n->right != NULL)
return NULL;
n->right = tmp;
}

// veke postoi element

// veke postoi element

return tmp;
}

32

- Java
public class BNode<E> {
public E info;
public BNode<E> left;
public BNode<E> right;
static int LEFT = 1;
static int RIGHT = 2;
public BNode(E info) {
this.info = info;
left = null;
right = null;
}

public BNode(E info, BNode<E> left, BNode<E> right) {


this.info = info;
this.left = left;
this.right = right;
}
}

33

- Java
public class BTree<E> {

public BNode<E> root;


public BTree() {
root = null;
}

public BTree(E info) {


root = new BNode<E>(info);
}
public void makeRoot(E elem) {
root = new BNode(elem);
}

34

- Java
public BNode<E> addChild(BNode<E> node, int where, E elem) {

BNode<E> tmp = new BNode<E>(elem);


if (where == BNode.LEFT) {
if (node.left != null) // veke postoi element
return null;
node.left = tmp;
} else {
if (node.right != null) // veke postoi element
return null;
node.right = tmp;
}
return tmp;
}
}

35


INORDER

LOD

(, , )

PREORDER

OLD

(, , )

POSTORDER

LDO

(, , )

Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20


Preorder: 15, 6, 3, 2, 4, 7, 13, 9, 18, 17, 20
Postorder: 2, 4, 3, 9, 13, 7, 6, 17, 20, 18, 15

36



:
o Inorder preorder
o Inorder postorder


:
o Preorder postorder

37

- C
void inorderR(bNode *n) {
if (n != NULL) {
inorderR(n->left);
printf("%c ", n->info);
inorderR(n->right);
}
}
void inorder(bTree *t) {
printf("INORDER: ");
inorderR(t->root);
printf("\n");
}

void postorderR(bNode *n) {


if (n != NULL) {
postorderR(n->left);
postorderR(n->right);
printf("%c ", n->info);
}
}
void postorder(bTree *t) {
printf("POSTORDER: ");
postorderR(t->root);
printf("\n");
}

void preorderR(bNode *n) {


if (n != NULL) {
printf("%c ", n->info);
preorderR(n->left);
preorderR(n->right);
}
}
void preorder(bTree *t) {
printf("PREORDER: ");
preorderR(t->root);
printf("\n");
}

38

- Java
public void inorder() {
System.out.print("INORDER: ");
inorderR(root);
System.out.println();
}

public void postorder() {


System.out.print(POSTORDER: ");
postorderR(root);
System.out.println();
}

public void inorderR(BNode<E> n) {


if (n != null) {
inorderR(n.left);
System.out.print(n.info.toString()
+ " ");
inorderR(n.right);
}
}

public void postorderR(BNode<E> n) {


if (n != null) {
postorderR(n.left);
postorderR(n.right);
System.out.print(n.info.toString()
+ " ");
}
}

public void preorder() {


System.out.print(PREORDER: ");
preorderR(root);
System.out.println();
}
public void preorderR(BNode<E> n) {
if (n != null) {
System.out.print(n.info.toString()
+ " ");
preorderR(n.left);
preorderR(n.right);
}
}

39

3

inorder.

40

3 - C
void inorderNonRecursive(bTree *tree) {
stackT s;
stackInit(&s);
bNode *p = tree->root;
// na pocetok pokazuva kon korenot

printf("INORDER (nonrecursive): ");


while (1) {
// pridvizuvanje do kraj vo leva nasoka pri sto site koreni
// na potstebla se dodavaat vo magacin za podocnezna obrabotka
while (p != NULL) {
stackPush(&s, p);
p = p->left;
}
// ako magacinot e prazen znaci deka stebloto e celosno izminato
if (stackEmpty(&s))
break;

41

3 - C
p = stackTop(&s);
// pecatenje (obrabotka) na jazelot na vrvot od magacinot
printf("%c ", p->info);
// brisenje na obraboteniot jazel od magacinot
stackPop(&s);
// pridvizuvanje vo desno od obraboteniot jazel i povtoruvanje na
// postapkata za desnoto potsteblo na jazelot
p = p->right;

}
printf("\n");
}

42

3 - Java
public void inorderNonRecursive() {
ArrayStack<BNode<E>> s = new ArrayStack<BNode<E>>(100);
BNode<E> p = root;
System.out.print("INORDER (nonrecursive): ");

while (true) {
// pridvizuvanje do kraj vo leva nasoka pri sto site koreni
// na potstebla se dodavaat vo magacin za podocnezna obrabotka
while (p != null) {
s.push(p);
p = p.left;
}
// ako magacinot e prazen znaci deka stebloto e celosno
izminato
if (s.isEmpty())
break;

43

3 - Java
p = s.peek();
// pecatenje (obrabotka) na jazelot na vrvot od magacinot
System.out.print(p.info.toString()+" ");
// brisenje na obraboteniot jazel od magacinot
s.pop();
// pridvizuvanje vo desno od obraboteniot jazel i povtoruvanje
na
// postapkata za desnoto potsteblo na jazelot
p = p.right;
}
System.out.println();
}

44

3.
:
o preorderNonRecursive

preorder
o postorderNonRecursive

preorder

45

4

()
.

46

4 - C
int insideNodesR(bNode *n) {
if (n == NULL)
return 0;
if ((n->left == NULL)&&(n->right == NULL))
return 0;
return insideNodesR(n->left) + insideNodesR(n->right) + 1;

}
int insideNodes(bTree *t) {
return insideNodesR(t->root);
}

47

4 - Java
int insideNodesR(BNode<E> node) {
if (node == null)
return 0;
if ((node.left == null)&&(node.right == null))
return 0;
return insideNodesR(node.left) + insideNodesR(node.right) + 1;

}
public int insideNodes() {
return insideNodesR(root);
}

48

5

.

49

5 - C
int leavesR(bNode *n) {
if (n != NULL) {
if ((n->left == NULL)&&(n->right == NULL))
return 1;
else
return (leavesR(n->left) + leavesR(n->right));
} else {
return 0;
}
}
int leaves(bTree *t) {
return leavesR(t->root);
}

50

5 - Java
int leavesR(BNode<E> node) {
if (node != null) {
if ((node.left == null)&&(node.right == null))
return 1;
else
return (leavesR(node.left) + leavesR(node.right));
} else {
return 0;
}
}
public int leaves() {
return leavesR(root);
}

51

6


(
).

52

6 - C
int max(int a, int b) {
if (a > b)
return a;
return b;
}
int depthR(bNode *n) {
if (n == NULL)
return 0;
if ((n->left == NULL)&&(n->right == NULL))
return 0;
return (1 + max(depthR(n->left), depthR(n->right)));
}
int depth(bTree *t) {
return depthR(t->root);
}

53

6 - Java
int depthR(BNode<E> node) {
if (node == null)
return 0;
if ((node.left == null)&&(node.right == null))
return 0;
return (1 + Math.max(depthR(node.left), depthR(node.right)));
}

public int depth() {


return depthR(root);
}

54

7



.
.

55

7 - C
void mirrorR(bNode *n) {
bNode *tmp;
if (n == NULL)
return;
// simetricno preslikuvanje na levoto i desnoto potsteblo
mirrorR(n->left);
mirrorR(n->right);
// smena na ulogite na pokazuvacite na momentalniot jazel
tmp = n->left;
n->left = n->right;
n->right = tmp;
}
void mirror(bTree *t) {
mirrorR(t->root);
}
56

7 - Java
void mirrorR(BNode<E> node) {
BNode<E> tmp;
if (node == null)
return;
// simetricno preslikuvanje na levoto i desnoto potsteblo
mirrorR(node.left);
mirrorR(node.right);
// smena na ulogite na pokazuvacite na momentalniot jazel
tmp = node.left;
node.left = node.right;
node.right = tmp;
}
public void mirror() {
mirrorR(root);
}

57



null ( ).


. n
-null :
n-1 ( ).
: 2n,
null : 2n-(n-1)=n+1.

null
.. .
58

59


inorder
.
inorder, ..
:
o ptr->left e null,

ptr inorder
o ptr->right null,

ptr inorder
o

60


.
.

inorder :

e0 x 2
61

- C
#include<stdio.h>
#include<malloc.h>
#define MEMCHECK(x) if(x==NULL) { printf("Nema memorija!\n"); exit(-1); }
#define LEFT 1
#define RIGHT 2
typedef char info_t;

typedef struct bNodeType {


info_t info;
struct bNodeType *left;
struct bNodeType *right;
char ltag;
char rtag;
} bNode;
typedef struct bTreeType {
bNode *head;
} bTree;
62

- C
void initialize(bTree *t) {
t->head = (bNode *)malloc(sizeof(bNode));
t->head->left = t->head;
t->head->ltag = '-';
t->head->right = t->head;
t->head->rtag = '+';
}

bNode* makeRoot(info_t elem, bTree *t) {


bNode *tmp = (bNode *)malloc(sizeof(bNode));
t->head->left = tmp;
t->head->ltag = '+';
tmp->info = elem;
tmp->left = t->head;
tmp->ltag = '-';
tmp->right = t->head;
tmp->rtag = '-';
return tmp;
}

63

- C
bNode* addChild(bNode *node, int where, info_t elem) {
bNode *tmp = (bNode *)malloc(sizeof(bNode));
tmp->info = elem;
if (where == LEFT) {
if (node->ltag == '+')
// veke postoi element
return NULL;
tmp->left = node->left;
tmp->ltag = '-';
tmp->right = node;
tmp->rtag = '-';
node->left = tmp;
node->ltag = '+';
} else {
if (node->rtag == '+')
// veke postoi element
return NULL;
tmp->right = node->right;
tmp->rtag = '-';
tmp->left = node;
tmp->ltag = '-';
node->right = tmp;
node->rtag = '+';
}
return tmp;
}

64

- Java
public class BNode<E> {

public E info;
public BNode<E> left;
public BNode<E> right;
char ltag;
char rtag;
static int LEFT = 1;
static int RIGHT = 2;
public BNode(E info) {
this.info = info;
left = null;
right = null;
ltag = '-';
rtag = '-';
}
}
65

- Java
public class BTree<E> {

public BNode<E> head;


public BTree() {
head = new BNode<E>(null);
// po definicija ako nema koren, t.e. ako stebloto e prazno
head.left = head;
head.ltag = '-';
// kaj vodacot sekogas desnata vrska pokazuva kon samiot sebe
head.right = head;
head.rtag = '+';
}
public BNode<E> makeRoot(E elem) {
BNode<E> tmp = new BNode<E>(elem);
head.left = tmp;
head.ltag = '+';
tmp.left = head;
tmp.right = head;
return tmp;
}
66

- Java
public BNode<E> addChild(BNode<E> node, int where, E elem) {
BNode<E> tmp = new BNode<E>(elem);
if (where == BNode.LEFT) {
if (node.ltag == '+')
// veke postoi element
return null;
tmp.left = node.left;
tmp.right = node;
node.left = tmp;
node.ltag = '+';
} else {
if (node.rtag == '+')
// veke postoi element
return null;
tmp.right = node.right;
tmp.left = node;
node.right = tmp;
node.rtag = '+';
}
return tmp;
}

67

8

p
inorder , inorder
.

inorder
: CBDAFGE

68

8 - C
bNode* predecessorInorder(bNode *node) {
if (node->ltag == '-')
return node->left;
bNode *p = node->left;
while (p->rtag == '+')
p = p->right;
return p;

}
bNode* successorInorder(bNode *node) {
if (node->rtag == '-')
return node->right;
bNode *p = node->right;
while (p->ltag == '+')
p = p->left;
return p;
}
69

8 - C
void inorderNonRecursive(bTree *tree) {

if (tree->head->ltag == '-')
return;

// drvoto e prazno

printf("INORDER (nonrecursive): ");


bNode *p = tree->head->left;
while (p->ltag == '+')
p = p->left;
while (p != tree->head) {
printf("%c ", p->info);
p = successorInorder(p);
}
printf("\n");
}

70

8 - Java
public BNode<E> predecessorInorder(BNode<E> node) {

if (node.ltag == '-')
return node.left;
BNode<E> p = node.left;
while (p.rtag == '+')
p = p.right;
return p;
}
public BNode<E> successorInorder(BNode<E> node) {

if (node.rtag == '-')
return node.right;
BNode<E> p = node.right;
while (p.ltag == '+')
p = p.left;
return p;
}
71

8 - Java
public void inorderNonRecursive() {

if (head.ltag == '-')
return;

// drvoto e prazno

System.out.print("INORDER (nonrecursive): ");


BNode<E> p = head.left;
while (p.ltag == '+')
p = p.left;
while (p != head) {
System.out.print(p.info.toString()+" ");
p = successorInorder(p);
}
System.out.println();
}
72

9

p preorder
inorder .

, , :
preorder (OLD): 1 2 4 7 3 5 6 8
inorder (LOD): 4 7 2 1 5 3 8 6

73

9 - C
bNode* successorPreorder(bNode *node, bTree *tree) {
if (node->ltag == '+')
return node->left;
if (node->rtag == '+')
return node->right;
// ako nema nitu levo nitu desno potsteblo, nslednik e korenot
// na desnoto potsteblo vo cie levo posteblo se naoga p
// (se odi se po niski dodeka ne se dojde do vrska)
bNode *p = node;
while (p->rtag == '-')
p = p->right;

if (p == tree->head)
return tree->head;
mora
else
return p->right;

// sme stignale do vodacot, moze, no i ne

}
74

9 - C
void preorderNonRecursive(bTree *tree) {
if (tree->head->ltag == '-')
// drvoto e prazno
return;
printf("PREORDER (nonrecursive): ");
bNode *p = tree->head->left;

while (p != tree->head) {
printf("%c ", p->info);
p = successorPreorder(p, tree);
}
printf("\n");
}

75

9 - Java
BNode<E> successorPreorder(BNode<E> node) {
if (node.ltag == '+')
return node.left;
if (node.rtag == '+')
return node.right;
// ako nema nitu levo nitu desno potsteblo, nslednik e korenot
// na desnoto potsteblo vo cie levo posteblo se naoga p
// (se odi se po niski dodeka ne se dojde do vrska)
BNode<E> p = node;
while (p.rtag == '-')
p = p.right;

if (p == head)
return head;
// sme stignale do vodacot, moze, no i ne mora
else
return p.right;
}

76

9 - Java
public void preorderNonRecursive() {
if (head.ltag == '-')
// drvoto e prazno
return;
System.out.print("PREORDER (nonrecursive): ");
BNode<E> p = head.left;

while (p != head) {
System.out.print(p.info.toString()+" ");
p = successorPreorder(p);
}
System.out.println();

77

10

p postorder
inorder
.
, , :
inorder (LOD): BAEDFCG
postorder (LDO): BEFDGCA

78

10 - C
bNode* predecessorPostorder(bNode *node) {
if (node->rtag == '+')
return node->right;
while (node->ltag == '-')
node = node->left;
return node->left;
}
void postorderOppNonRecursive(bTree *tree) {
if (tree->head->ltag == '-')
// drvoto e prazno
return;

printf("POSTORDER OPPOSITE (nonrecursive): ");


bNode *p = tree->head->left;
while (1) {
printf("%c ", p->info);
p = predecessorPostorder(p);
if (p == tree->head->left)
break;
}
printf("\n");

}
79

10 - Java
BNode<E> predecessorPostorder(BNode<E> node) {
if (node.rtag == '+')
return node.right;
while (node.ltag == '-')
node = node.left;
return node.left;
}
public void postorderOppNonRecursive() {
if (head.ltag == '-')
// drvoto e prazno
return;

System.out.print("POSTORDER OPPOSITE (nonrecursive): ");


BNode<E> p = head.left;
while (true) {
System.out.print(p.info.toString()+" ");
p = predecessorPostorder(p);
if (p == head.left)
break;
}
System.out.println();

}
80

11

child parent,
parent .

parent parent->right. inorder
.

81

11 -
1

82

11 -
2

83

11 - C
bNode* insertRight(bNode *parent, info_t info) {
bNode *child = (bNode *)malloc(sizeof(bNode));
child->info = info;
child->ltag = '-';
child->left = parent;
child->rtag = parent->rtag;
child->right = parent->right;
parent->right = child;
parent->rtag = '+';
if (child->rtag == '+') {
bNode *temp = child->right;
while (temp->ltag == '+')
temp = temp->left;
temp->left = child;
}
return child;
}
84

11 - Java
public BNode<E> insertRight(BNode<E> parent, E info) {
BNode<E> child = new BNode<E>(info);
child.ltag = '-';
child.left = parent;
child.rtag = parent.rtag;;
child.right = parent.right;

parent.right = child;
parent.rtag = '+';
if (child.rtag == '+') {
BNode<E> temp = child.right;
while (temp.ltag == '+')
temp = temp.left;
temp.left = child;
}

return child;
}

85