08 Hierarhiski Strukturi Drva

85
5/20/2018 08HierarhiskiStrukturiDrva-slidepdf.com http://slidepdf.com/reader/full/08-hierarhiski-strukturi-drva 1/85 ФАКУЛТЕТ ЗА ИНФОРМАТИЧКИ НАУКИ И КОМПЈУТЕРСКО ИНЖЕНЕРСТВО ФАКУЛТЕТ ЗА ИНФОРМАТИЧКИ НАУКИ И КОМПЈУТЕРСКО ИНЖЕНЕРСТВО Хиерархиски структури - дрва Алгоритми и податочни структури Аудиториска вежба 8

description

ffffd

Transcript of 08 Hierarhiski Strukturi Drva

  • -

    8

  • -

    , .. .

    - - ()

    2

  • -

    3

  • -

    (K, L, F, G, M, I J)

    n1, n2, nk, ni-1 ni, i*2, k+ (A, B, E)

    ()

    e ,

    4

  • -

    ( , 2 A, B, E).

    , .. ( , , )

    E , .

    5

  • 6

    :

    Archy

    FrankGeorge

    Fred

    Colin Joe

    David

    Susie Jeff

    Maggie Ann

    Emma Jon

  • - C#include

    #include

    #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

  • - Cint 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;

    }

    8

  • - Cnode* 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;

    }

    9

  • - Cvoid 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

  • - Cvoid deleteRecursively(node *n) {

    node *tmp, *next;

    for (tmp=n->firstChild;tmp!=NULL;) {

    next = tmp->sibling;

    deleteRecursively(tmp);

    tmp = next;

    }

    free(n);

    }

    11

  • - Javaimport java.util.Iterator;

    public interface Tree {

    public Tree.Node root();

    public Tree.Node parent(Tree.Node node);

    public int childCount(Tree.Node node);

    public void makeRoot(E elem);

    public Tree.Node addChild(Tree.Node node, E elem);

    public void remove(Tree.Node node);

    public Iterator children(Tree.Node node);

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

    public interface Node {

    public E getElement();

    public void setElement(E elem);

    }

    } 12

  • - Javapublic class SLLTree implements Tree {

    // SLLNode is the implementation of the Node interface

    class SLLNode implements Node {

    // Holds the links to the needed nodes

    SLLNode 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

    SLL Node

  • - Javaprotected SLLNode root;

    public SLLTree() {

    root = null;

    }

    public Node root() {

    return root;

    }

    public Tree.Node parent(Tree.Node node) {

    return ((SLLNode) node).parent;

    }

    public int childCount(Tree.Node node) {

    SLLNode tmp = ((SLLNode) node).firstChild;

    int num = 0;

    while (tmp != null) {

    tmp = tmp.sibling;

    num++;

    }

    return num;

    }

    14

  • - Javapublic void makeRoot(E elem) {

    root = new SLLNode(elem);

    }

    public Node addChild(Node node, E elem) {

    SLLNode tmp = new SLLNode(elem);

    SLLNode curr = (SLLNode) node;

    tmp.sibling = curr.firstChild;

    curr.firstChild = tmp;

    tmp.parent = curr;

    return tmp;

    }

    public Iterator children(Tree.Node node) {

    return new SLLTreeChildIterator(((SLLNode) node).firstChild);

    }

    15

  • - Javapublic void remove(Tree.Node node) {

    SLLNode curr = (SLLNode) 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 tmp = curr.parent.firstChild;

    while (tmp.sibling != curr) {

    tmp = tmp.sibling;

    }

    tmp.sibling = curr.sibling;

    }

    } else {

    root = null;

    }

    } 16

  • - Javaclass SLLTreeChildIterator implements Iterator {

    SLLNode start, current;

    public SLLTreeIterator(SLLNode node) {

    start = node;

    current = node;

    }

    public boolean hasNext() {

    return (current != null);

    }

    public T next() throws NoSuchElementException {

    if (current != null) {

    SLLNode tmp = current;

    current = current.sibling;

    return tmp.getElement();

    } else {

    throw new NoSuchElementException();

    }

    }

    }

    17

  • 1

    . , .

    18

  • 1 - Cvoid printTreeRecursive(node *n, int level) {

    if (n == NULL)

    return;

    int i;

    node *tmp;

    for (i=0;iinfo);

    tmp = n->firstChild;

    while (tmp != NULL) {

    printTreeRecursive(tmp, level+1);

    tmp = tmp->sibling;

    }

    }

    void printTree(tree *t) {

    printTreeRecursive(t->root, 0);

    }

    19

  • 1 - Cint 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 - Javavoid printTreeRecursive(Node node, int level) {

    if (node == null)

    return;

    int i;

    SLLNode tmp;

    for (i=0;i

  • 1 - Javapublic static void main(String[] args) {

    Tree.Node 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 - Cint 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 - Javapublic int countMaxChildren() {

    return countMaxChildrenRecursive(root);

    }

    int countMaxChildrenRecursive(SLLNode node) {

    int t = childCount(node);

    SLLNode 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. :

    28

    1221

    1

    dd

    i

    in

  • : . , , , n, .

    , , .

    29

    12 dn

    )1(log2 nd

    )1(log2 nd

  • - C#include

    #include

    #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

  • - Cvoid 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

  • - CbNode* 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) // veke postoi element

    return NULL;

    n->left = tmp;

    } else {

    if (n->right != NULL) // veke postoi element

    return NULL;

    n->right = tmp;

    }

    return tmp;

    }

    32

  • - Javapublic class BNode {

    public E info;

    public BNode left;

    public BNode 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 left, BNode right) {

    this.info = info;

    this.left = left;

    this.right = right;

    }

    }33

  • - Javapublic class BTree {

    public BNode root;

    public BTree() {

    root = null;

    }

    public BTree(E info) {

    root = new BNode(info);

    }

    public void makeRoot(E elem) {

    root = new BNode(elem);

    }

    34

  • - Javapublic BNode addChild(BNode node, int where, E elem) {

    BNode tmp = new BNode(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

  • 36

    INORDER L O D (, , )

    PREORDER O L D (, , )

    POSTORDER L D O (, , )

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

  • :

    o Inorder preorder

    o Inorder postorder

    :

    o Preorder postorder

    37

  • - Cvoid 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");

    }

    38

    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");

    }

  • - Java

    public void inorder() {

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

    inorderR(root);

    System.out.println();

    }

    public void inorderR(BNode n) {

    if (n != null) {

    inorderR(n.left);

    System.out.print(n.info.toString()

    + " ");

    inorderR(n.right);

    }

    }

    39

    public void preorder() {

    System.out.print(PREORDER: ");

    preorderR(root);

    System.out.println();

    }

    public void preorderR(BNode n) {

    if (n != null) {

    System.out.print(n.info.toString()

    + " ");

    preorderR(n.left);

    preorderR(n.right);

    }

    }

    public void postorder() {

    System.out.print(POSTORDER: ");

    postorderR(root);

    System.out.println();

    }

    public void postorderR(BNode n) {

    if (n != null) {

    postorderR(n.left);

    postorderR(n.right);

    System.out.print(n.info.toString()

    + " ");

    }

    }

  • 3

    inorder.

    40

  • 3 - Cvoid 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 - Cp = 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 - Javapublic void inorderNonRecursive() {

    ArrayStack s = new ArrayStack(100);

    BNode 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 - Javap = 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 - Cint 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 - Javaint insideNodesR(BNode 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 - Cint 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 - Javaint leavesR(BNode 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 - Cint 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 - Javaint depthR(BNode 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 - Cvoid 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 - Javavoid mirrorR(BNode node) {

    BNode 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

  • . .

    61

    20 xe

    inorder :

  • - C#include

    #include

    #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

  • - Cvoid 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

  • - CbNode* 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

  • - Javapublic class BNode {

    public E info;

    public BNode left;

    public BNode 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

  • - Javapublic class BTree {

    public BNode head;

    public BTree() {

    head = new BNode(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 makeRoot(E elem) {

    BNode tmp = new BNode(elem);

    head.left = tmp;

    head.ltag = '+';

    tmp.left = head;

    tmp.right = head;

    return tmp;

    }

    66

  • - Javapublic BNode addChild(BNode node, int where, E elem) {

    BNode tmp = new BNode(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 .

    68

    inorder : CBDAFGE

  • 8 - CbNode* 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 - Cvoid inorderNonRecursive(bTree *tree) {

    if (tree->head->ltag == '-') // drvoto e prazno

    return;

    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 - Javapublic BNode predecessorInorder(BNode node) {

    if (node.ltag == '-')

    return node.left;

    BNode p = node.left;

    while (p.rtag == '+')

    p = p.right;

    return p;

    }

    public BNode successorInorder(BNode node) {

    if (node.rtag == '-')

    return node.right;

    BNode p = node.right;

    while (p.ltag == '+')

    p = p.left;

    return p;

    }

    71

  • 8 - Javapublic void inorderNonRecursive() {

    if (head.ltag == '-') // drvoto e prazno

    return;

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

    BNode 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 .

    73

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

  • 9 - CbNode* 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; // sme stignale do vodacot, moze, no i ne

    mora

    else

    return p->right;

    }

    74

  • 9 - Cvoid 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 - JavaBNode successorPreorder(BNode 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 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 - Javapublic void preorderNonRecursive() {

    if (head.ltag == '-') // drvoto e prazno

    return;

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

    BNode p = head.left;

    while (p != head) {

    System.out.print(p.info.toString()+" ");

    p = successorPreorder(p);

    }

    System.out.println();

    }

    77

  • 10

    p postorder inorder .

    78

    , , :inorder (LOD): BAEDFCGpostorder (LDO): BEFDGCA

  • 10 - CbNode* 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 - JavaBNode predecessorPostorder(BNode 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 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 - CbNode* 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 - Javapublic BNode insertRight(BNode parent, E info) {

    BNode child = new BNode(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;

    }

    85