c8 binary trees

9
The Binary Tree Data Structure Mugurel Ionuț Andreica Spring 2012

Upload: leslie-nelson

Post on 16-Dec-2015

213 views

Category:

Documents


1 download

DESCRIPTION

tree

TRANSCRIPT

  • The Binary Tree Data StructureMugurel Ionu Andreica

    Spring 2012

  • The Elements of a Binary Treecomposed of nodesone special node: the root => rooted treesunrooted trees also exist (but they are not studied in this course)each node has:one left son (possibly NULL)one right son (possibly NULL)one parent (NULL, in case of the root)a pointer to some useful information

  • Binary Tree - example

    4

    5

    2

    3

    13

    9

    20

    8

    55

    32

  • Binary Tree C++ code#include #include

    char chstack[1000]; // will be used later

    template class BinaryTreeNode { public: T *pinfo; BinaryTreeNode *left_son, *right_son, *parent, *root;

    BinaryTreeNode() { pinfo = NULL; left_son = right_son = parent = NULL; root = this; }

    void setInfo(T info) { pinfo = new T; *pinfo = info; } void setRoot(BinaryTreeNode *r) { root = r; }

    void insert(T x) { if (pinfo == NULL) setInfo(x); else insert_rec(x); }

    void insert_rec(T x) { int next_son = rand() % 2;

    if (next_son == 0) // left son { if (left_son == NULL) { left_son = new BinaryTreeNode; left_son->pinfo = new T; *(left_son->pinfo) = x; left_son->left_son = left_son->right_son = NULL;

  • Binary Tree C++ code (cont.) left_son->parent = this; left_son->root = root; } else left_son->insert_rec(x); } else // right son { if (right_son == NULL) { right_son = new BinaryTreeNode; right_son->pinfo = new T; *(right_son->pinfo) = x; right_son->left_son = right_son->right_son = NULL; right_son->parent = this; right_son->root = root; } else right_son->insert_rec(x); } BinaryTreeNode* find(T x) { BinaryTree *rez;

    if (pinfo == NULL) return NULL;

    // Use an equality testing function instead if ((*pinfo) == x) return this;

    if (left_son != NULL) rez = left_son->find(x); else rez = NULL;

    if (rez != NULL) return rez; else if (right_son != NULL) return right_son->find(x);

  • Binary Tree C++ code (cont.) else return NULL; }

    void remove() { BinaryTreeNode *leaf; // find a leaf in this node's subtree leaf = findLeaf(); if (this == leaf) { if (parent == NULL) // this == root { if (this->pinfo != NULL) delete this->pinfo; root->pinfo = NULL; } else { if (parent->left_son == this) parent->left_son = NULL; else parent->right_son = NULL; delete this->pinfo; delete this; }} else { if (leaf->parent->left_son == leaf) leaf->parent->left_son = NULL; else leaf->parent->right_son = NULL; leaf->parent = parent; leaf->left_son = left_son; leaf->right_son = right_son; delete this->pinfo; this->pinfo = leaf->pinfo; delete leaf; } }

    void removeInfo(T x) { BinaryTreeNode *t = find(x); if (t != NULL) t->remove(); }

    BinaryTreeNode* findLeaf() { if (left_son == NULL && right_son == NULL) return this;

  • Binary Tree C++ code (cont.) else if (left_son != NULL) return left_son->findLeaf(); else return right_son->findLeaf(); }

    void preOrderTraversal() { printf("%d\n", *pinfo); /* we should use the correct format for printing type T values */ if (left_son != NULL) left_son->preOrderTraversal(); if (right_son != NULL) right_son->preOrderTraversal(); }

    void postOrderTraversal() { if (left_son != NULL) left_son->postOrderTraversal(); if (right_son != NULL) right_son->postOrderTraversal();

    printf("%d\n", *pinfo); /* we should use the correct format for printing type T values */ }

    void inOrderTraversal() { if (left_son != NULL) left_son->inOrderTraversal(); printf("%d\n", *pinfo); /* we should use the correct format for printing type T values */ if (right_son != NULL) right_son->inOrderTraversal(); }

    void preOrderTraversal2(int level) { int i;

    for (i = 0; i < level; i++) printf("-");

    printf("%d\n", *pinfo); /* we should use the correct format for printing type T values */

  • Binary Tree C++ code (cont.) if (left_son != NULL) left_son->preOrderTraversal2(level + 1); if (right_son != NULL) right_son->preOrderTraversal2(level + 1); }

    void preOrderTraversal3(int level) { int i; for (i = 1; i preOrderTraversal3(level + 1); chstack[level + 1] = 'R'; if (right_son != NULL) right_son->preOrderTraversal3(level + 1); }};

    int main() { srand(7290);

    BinaryTreeNode *r = new BinaryTreeNode; // r->setRoot(r); r->insert(6); r->insert(8); r->insert(1);

    r->insert(9); r->insert(10); r->insert(4); r->insert(13); r->insert(1); r->insert(12);

    r->preOrderTraversal(); printf("___\n"); r->preOrderTraversal2(0); printf("___\n");

  • Binary Tree C++ code (cont.) r->preOrderTraversal3(0); printf("___\n"); r->postOrderTraversal(); printf("___\n"); r->inOrderTraversal(); printf("___\n");

    printf("%d\n", r->find(100)); printf("%d\n", r->find(1)); printf("%d\n", r->find(12)); printf("%d\n", r->find(8)); printf("%d\n", r->find(10)); printf("%d\n", r->find(20));

    (r->find(10))->remove(); printf("_______\n%d\n", r->find(1)); printf("%d\n", r->find(10)); printf("%d\n", r->find(12)); printf("%d\n", r->find(8));

    return 0;}