Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Chapter 10 Binary Trees Andreas Savva.

Similar presentations


Presentation on theme: "Data Structures Chapter 10 Binary Trees Andreas Savva."— Presentation transcript:

1 Data Structures Chapter 10 Binary Trees Andreas Savva

2 2 Lists vs Trees Lists have great advantages of flexibility but the have one week feature: Lists have great advantages of flexibility but the have one week feature: They are sequential lists – can move through them only one position at a time. They are sequential lists – can move through them only one position at a time. Trees Trees Valuable for a range of applications, especially for problems of information retrieval. Valuable for a range of applications, especially for problems of information retrieval. The are not sequential. The are not sequential.

3 3 Binary Trees A binary tree is either empty or it consists of a node called a root together with two binary trees called the left subtree and the right subtree of the root. A binary tree is either empty or it consists of a node called a root together with two binary trees called the left subtree and the right subtree of the root. Jim DotRon Guy EvaJan Kay JonKim Tim RoyTom Amy Ann Amy Ann Dot Eva Guy Jan Jim Jon Kay Kim Ron Roy Tim Tom

4 4 Binary Trees root left right root 3 nodes 2 nodes Incorrect Binary Trees Examples:

5 5 Nodes Let x + y mean that the root has x nodes on the left and y nodes on the right, then Let x + y mean that the root has x nodes on the left and y nodes on the right, then 1 + 1 2 + 0 0 + 2

6 6 Traversal of Binary Trees Traversal means “moving through all the nodes visiting each one in turn”. Traversal means “moving through all the nodes visiting each one in turn”. It is one of the most important operations on binary trees. It is one of the most important operations on binary trees. There are many different orders in which all the nodes could be traversed. There are many different orders in which all the nodes could be traversed. V – visiting a node (root) L – traversing the left subtree (left) R – traversing the right subtree (right) VLR LVR LRV preorder inorder postorder preorder inorder postorder VRL RVL RLV mirror images

7 7 Examples 1 23 Preorder:1, 2, 3 Inorder:2, 1, 3 Postorder:2, 3, 1 Preorder:1, 2, 3, 4, 5 Inorder:1, 4, 3, 5, 2 Postorder:4, 5, 3, 2, 1 1 3 2 54 1 4 2 3 5 Preorder:1, 5, 2, 4, 3 Inorder:5, 1, 4, 2, 3 Postorder:5, 4, 3, 2, 1

8 8 Examples (continue) Jim DotRon Guy EvaJan Kay JonKim Tim RoyTom Amy Ann Preorder:Jim, Dot, Amy, Ann, Guy, Eva, Jan, Ron, Kay, Jon, Kim, Tim, Roy, Tom Inorder:Amy, Ann, Dot, Eva, Guy, Jan, Jim, Jon, Kay, Kim, Ron, Roy, Tim, Tom Postorder:Ann, Amy, Eva, Jan, Guy, Dot, Jon, Kim, Kay, Roy, Tom, Tim, Ron, Jim

9 9 Expression Trees + ab log x ! n – b × c a or c < d a < b Preorder:+ a b Inorder:a + b Postorder:a b + Preorder:log x Inorder:log x Postorder:x log Preorder:! n Inorder:n ! Postorder:n ! Preorder:– a × b c Inorder:a – b × c Postorder:a b c × – Preorder:or < a b < c d Inorder:a < b or c < d Postorder:a b < c d < or

10 10 = + / X 2 × a – b –  0.5 b  2 × c 4 × a Preorder:= X/ + – b  – b 2 × × 4 a c 0.5 × 2 a Inorder: X= ( – b + ( b 2 – 4 × a × c) 0.5 ) / ( 2 × a) Postorder: Xb – b 2 4 a × c × – 0.5 + 2 a × / = Related to the Polish form Preorder – Prefix Inorder – Infix Postorder – Postfix

11 11 The Node Implementation template struct Node { // data members Entry entry; Node *left, *right; // constructors Node(); Node(const Entry &x); }; template Node ::Node() { left = right = NULL; } template Node ::Node(const Entry &x) { entry = x; left = right = NULL; }

12 12 Linked Implementation of Binary Trees Jim Dot AmyGuy AnnEvaJanKimRoyTomJon Ron KayTim

13 13 template class Tree { public: Tree(); void clear(); bool empty() const; int size() const; int height() const; void preorder(void (*visit)(Entry &)); void inorder(void (*visit)(Entry &)); void postorder(void (*visit)(Entry &)); Error_code insert(const Entry &x); Error_code remove(const Entry &x); // Safequards ~Tree(); Tree(const Tree &original); void operator = (const Tree &original); protected: int size_recursive(Node *sub_root) const; int height_recursive(Node *sub_root) const; void recursive_preorder(Node *sub_root, void(*visit)(Entry &)); void recursive_inorder(Node *sub_root, void(*visit)(Entry &)); void recursive_postorder(Node *sub_root, void(*visit)(Entry &)); Error_code insert_recursive(Node * &subroot, const Entry &x); Error_code remove_recursive(Node * &subroot, const Entry &x); Error_code remove_root(Node * &subroot); Node *root; }; The Tree Implementation

14 14 Create Tree template Tree ::Tree() { root = NULL; } Constructor: root

15 15 Empty template bool Tree ::empty() const { return root == NULL; }

16 16 Size template int Tree ::size_recursive(Node *sub_root) const { if (sub_root == NULL) return 0; else return 1 + size_recursive(sub_root->left) + size_recursive(sub_root->right); } template int Tree ::size() const { return size_recursive(root); }

17 17 Height template int Tree ::height_recursive(Node *sub_root) const { if (sub_root == NULL) return 0; else return 1 + max(height_recursive(sub_root->left), height_recursive(sub_root->right)); } template int Tree ::height() const { return height_recursive(root); }

18 18 Preorder template void Tree ::recursive_preorder(Node *sub_root, void(*visit)(Entry &)) { if (sub_root != NULL) { (*visit)(subroot->entry); recursive_preorder(sub_root->left, visit); recursive_preorder(sub_root->right, visit); } template void Tree ::preorder(void(*visit)(Entry &)) { recursive_preorder(root, visit); }

19 19 Traverse Preorder void print(int &x) { cout << x << endl; } the_tree.preorder(print); 1 4 2 3 5 15243Output

20 20 Inorder template void Tree ::recursive_inorder(Node *sub_root, void(*visit)(Entry &)) { if (sub_root != NULL) { recursive_inorder(sub_root->left, visit); (*visit)(subroot->entry); recursive_inorder(sub_root->right, visit); } template void Tree ::inorder(void(*visit)(Entry &)) { recursive_inorder(root, visit); }

21 21 Postorder template void Tree ::recursive_postorder(Node *sub_root, void(*visit)(Entry &)) { if (sub_root != NULL) { recursive_postorder(sub_root->left, visit); recursive_postorder(sub_root->right, visit); (*visit)(subroot->entry); } template void Tree ::postorder(void(*visit)(Entry &)) { recursive_postorder(root, visit); }

22 22 template class Tree { public:..... void preorder(); void inorder(); void postorder();..... protected:..... void recursive_preorder(Node *sub_root); void recursive_inorder(Node *sub_root); void recursive_postorder(Node *sub_root);..... Node *root; }; The Class Tree

23 23 Display Preorder template void Tree ::recursive_preorder(Node *sub_root) { if (sub_root != NULL) { cout entry << ” ”; recursive_preorder(sub_root->left); recursive_preorder(sub_root->right); } template void Tree ::preorder() { recursive_preorder(root); }

24 24 Display Inorder template void Tree ::recursive_inorder(Node *sub_root) { if (sub_root != NULL) { recursive_inorder(sub_root->left); cout entry << ” ”; recursive_inorder(sub_root->right); } template void Tree ::inorder() { recursive_inorder(root); }

25 25 Display Postorder template void Tree ::recursive_postorder(Node *sub_root) { if (sub_root != NULL) { recursive_postorder(sub_root->left); recursive_postorder(sub_root->right); cout entry << ” ”; } template void Tree ::postorder() { recursive_postorder(root); }

26 26 Insert into a Binary Search Tree Insert: e, b, d, f, a, g, c e f g b a dc Exercises: Insert: b, d, f, a, g, c, e Insert: b, d, f, a, g, c, e Insert: d, c, a, f, g, b, e Insert: d, c, a, f, g, b, e Insert: 1, 2, 3, 4, 5, 6, 7, 8, 9 Insert: 1, 2, 3, 4, 5, 6, 7, 8, 9 Insert: 8, 12, 3, 5, 1, 15, 4, 10, 6, 13, 7, 20, 18, 2, 9, 14 Insert: 8, 12, 3, 5, 1, 15, 4, 10, 6, 13, 7, 20, 18, 2, 9, 14 Insert: 6, 3, 8, 1, 4, 7, 5, 9, 2 6 8 9 3 1 4 2 7 5

27 27 Insert template Error_code Tree ::insert_recursive(Node *&sub_root, const Entry &x) { if (sub_root == NULL) { Node *temp = new Node (x); if (temp == NULL) return overflow; sub_root = temp; return success; } else if (x entry) return insert_recursive(sub_root->left, x); else return insert_recursive(sub_root->right, x); } template Error_code Tree ::insert(const Entry &x) { return insert_recursive(root, x); }

28 28 Remove x Detete x Deletion of a leaf x Detete x One subtree empty Replace x by w Neither subtree empty x w w w w Delete original w

29 29 How can 1 be removed from the tree? 6 8 10 3 1 4 2 7 5 9 Remove 6 8 3 1 4 2 7 5 9 How can 2 be removed from the tree? 6 8 10 3 1 4 7 5 8 9 6 8 3 2 4 7 5 9

30 30 6 8 10 3 1 4 2 7 5 How can 3 be removed from the tree? 9 Remove 6 8 10 3 1 4 2 7 5 9 How can 6 be removed from the tree? 5 2 5 8 10 3 1 4 2 7 9 6 8 2 1 4 7 5 9

31 31 Remove How can 9 be removed from the tree? 5 9 10 3 1 4 2 6 7 8 8 5 8 3 1 4 2 6 7

32 32 Remove template Error_code Tree ::remove_recursive(Node *&sub_root, const Entry &x) { if (sub_root == NULL) return not_found; if (sub_root->entry == x) return remove_root(sub_root); else if (x entry) return remove_recursive(sub_root->left, x); else return remove_recursive(sub_root->right, x); } template Error_code Tree ::remove(const Entry &x) { return remove_recursive(root, x); }

33 33 Remove Root template Error_code Tree ::remove_root(Node *&sub_root) { Node *to_delete = sub_root; // remember to delete at end if (sub_root->right==NULL) sub_root = sub_root->left; else if (sub_root->left == NULL) sub_root = sub_root->right; else { // neither subtree is empty. to_delete = sub_root->left; // Move left to find predecessor Node *parent = sub_root; // Parent of to_delete while (to_delete->right != NULL) { // to_delete is not the predecessor parent = to_delete; to_delete = to_delete->right; } sub_root->entry = to_delete->entry; // Move from to_delete to root if (parent == sub_root) sub_root->left = to_delete->left; else parent->right = to_delete->left; } delete to_delete; return success; }


Download ppt "Data Structures Chapter 10 Binary Trees Andreas Savva."

Similar presentations


Ads by Google