100 likes | 204 Views
The Binary Tree Data Structure. Mugurel Ionu ț Andreica Spring 2012. The Elements of a Binary Tree. composed of nodes one special node: the root => rooted trees unrooted trees also exist (but they are not studied in this course) each node has: one left son (possibly NULL)
E N D
The Binary Tree Data Structure Mugurel Ionuț Andreica Spring 2012
The Elements of a Binary Tree • composed of nodes • one special node: the root => rooted trees • unrooted 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 – C++ code void setRoot(BinaryTreeNode<T> *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<T>; left_son->pinfo = new T; *(left_son->pinfo) = x; left_son->left_son = left_son->right_son = NULL; #include <stdio.h> #include <stdlib.h> char chstack[1000]; // will be used later template <typename T> class BinaryTreeNode { public: T *pinfo; BinaryTreeNode<T> *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; }
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<T>; 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<T>* find(T x){ BinaryTree<T> *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; elseif (right_son != NULL) return right_son->find(x);
Binary Tree – C++ code (cont.) else return NULL; } void remove(){ BinaryTreeNode<T> *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> *t = find(x); if (t != NULL)t->remove(); } BinaryTreeNode<T>* 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.) int main(){ srand(7290); BinaryTreeNode<int> *r = new BinaryTreeNode<int>; // 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"); 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 <= level; i++) printf("%c", chstack[i]); printf("* %d\n", (*pinfo)); /* use the correct format */ chstack[level + 1] = 'L'; if (left_son != NULL) left_son->preOrderTraversal3(level + 1); chstack[level + 1] = 'R'; if (right_son != NULL) right_son->preOrderTraversal3(level + 1); } };
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; }