Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Binary Tree Data Structure

Similar presentations


Presentation on theme: "The Binary Tree Data Structure"— Presentation transcript:

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

2 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

3 Binary Tree - example

4 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;

5 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; 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; else if (right_son != NULL) return right_son->find(x);

6 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; 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;

7 Binary Tree – C++ code (cont.)
else if (left_son != NULL) return left_son->findLeaf(); return right_son->findLeaf(); } void preOrderTraversal() { printf("%d\n", *pinfo); /* we should use the correct format for printing type T values */ left_son->preOrderTraversal(); if (right_son != NULL) right_son->preOrderTraversal(); void postOrderTraversal() { left_son->postOrderTraversal(); 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(); if (right_son != NULL) right_son->inOrderTraversal(); void preOrderTraversal2(int level) { int i; for (i = 0; i < level; i++) printf("-");

8 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(12); r->preOrderTraversal(); printf("___\n"); r->preOrderTraversal2(0); 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'; left_son->preOrderTraversal3(level + 1); chstack[level + 1] = 'R'; right_son->preOrderTraversal3(level + 1); };

9 Binary Tree – C++ code (cont.)
r->preOrderTraversal3(0); printf("___\n"); r->postOrderTraversal(); r->inOrderTraversal(); 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)); return 0; }


Download ppt "The Binary Tree Data Structure"

Similar presentations


Ads by Google