Data Abstraction and Structures Using C Headington and

  • Slides: 48
Download presentation
Data Abstraction and Structures Using C++ Headington and Riley Chapter 13 Trees 1

Data Abstraction and Structures Using C++ Headington and Riley Chapter 13 Trees 1

C++ Data Types Simple Structured Integral Floating array struct union class char short int

C++ Data Types Simple Structured Integral Floating array struct union class char short int long enum float double long double Address pointer reference 2

Hierarchical Structure of the Community Colleges of Baltimore County Chancellor Catonsville President Dean of

Hierarchical Structure of the Community Colleges of Baltimore County Chancellor Catonsville President Dean of Students. . . Dundalk President Dean of Instruction Dean of Students . . . Dean of Instruction. . . AH Chr Essex President Dean of Students Dean of Instruction . . . B/M HPERD H/A Chr Chr SS Chr S/M Chr Dir. Lib 3 Media

A Tree is a Hierarchical Structure A tree is a hierarchy in which each

A Tree is a Hierarchical Structure A tree is a hierarchy in which each component, except the topmost, is immediately beneath exactly one other component. That is, each component can have zero or more successors, but has only one immediate predecessor. 4

Jake’s Pizza Shop Owner Manager Delivery Joe Delivery Tim Waiter Max Chef Waiter Ted

Jake’s Pizza Shop Owner Manager Delivery Joe Delivery Tim Waiter Max Chef Waiter Ted Cook Len Cook Dale 5

A Tree Has a Root Node Owner ROOT NODE Manager Delivery Joe Delivery Tim

A Tree Has a Root Node Owner ROOT NODE Manager Delivery Joe Delivery Tim Waiter Max Chef Waiter Ted Cook Len Cook Dale 6

Leaf nodes have no children Owner Manager Delivery Joe Delivery Tim Waiter Max Chef

Leaf nodes have no children Owner Manager Delivery Joe Delivery Tim Waiter Max Chef Waiter Ted Cook Len Cook Dale LEAF NODES 7

Sibling nodes have same parent Owner SIBLINGS Manager Delivery Joe Delivery Tim Waiter Max

Sibling nodes have same parent Owner SIBLINGS Manager Delivery Joe Delivery Tim Waiter Max Chef Waiter Ted Cook Len Cook Dale 8

Tree Terminology Owner Manager Delivery Joe Delivery Tim Waiter Max Chef Waiter Ted Cook

Tree Terminology Owner Manager Delivery Joe Delivery Tim Waiter Max Chef Waiter Ted Cook Len Cook Dale SIBLINGS 9

Tree Terminology Owner Manager Delivery Joe Delivery Tim Waiter Max Chef Waiter Ted Cook

Tree Terminology Owner Manager Delivery Joe Delivery Tim Waiter Max Chef Waiter Ted Cook Len Cook Dale SIBLINGS 10

A Tree Has Levels LEVEL #1 Owner Manager Delivery Joe Delivery Tim Waiter Max

A Tree Has Levels LEVEL #1 Owner Manager Delivery Joe Delivery Tim Waiter Max Chef Waiter Ted Cook Len Cook Dale 11

Level Two Owner LEVEL #2 Manager Delivery Joe Delivery Tim Waiter Max Chef Waiter

Level Two Owner LEVEL #2 Manager Delivery Joe Delivery Tim Waiter Max Chef Waiter Ted Cook Len Cook Dale 12

Level Three Owner Manager LEVEL #3 Delivery Joe Delivery Tim Waiter Max Chef Waiter

Level Three Owner Manager LEVEL #3 Delivery Joe Delivery Tim Waiter Max Chef Waiter Ted Cook Len Cook Dale 13

A Subtree Owner Manager Delivery Joe Delivery Tim Waiter Max Chef Waiter Ted Cook

A Subtree Owner Manager Delivery Joe Delivery Tim Waiter Max Chef Waiter Ted Cook Len Cook Dale A SUBTREE OF ROOT NODE 14

Another Subtree Owner Manager Delivery Joe Delivery Tim Waiter Max Chef Waiter Ted Cook

Another Subtree Owner Manager Delivery Joe Delivery Tim Waiter Max Chef Waiter Ted Cook Len Cook Dale ANOTHER SUBTREE OF ROOT NODE 15

Binary Tree A binary tree is a special kind of tree in which: 1.

Binary Tree A binary tree is a special kind of tree in which: 1. A node can have at most two children, and 2. The two children of a node are called the left child and the right child. 16

A Binary Tree V Q L T E K A S 17

A Binary Tree V Q L T E K A S 17

How many leaf nodes? V Q L T E K A S 18

How many leaf nodes? V Q L T E K A S 18

How many descendants of Q? V Q L T E K A S 19

How many descendants of Q? V Q L T E K A S 19

How many ancestors of K? V Q L T E K A S 20

How many ancestors of K? V Q L T E K A S 20

Implementing a Binary Tree with Pointers and Dynamic Data V Q L T E

Implementing a Binary Tree with Pointers and Dynamic Data V Q L T E K A S 21

A Binary Expression Tree ‘*’ ‘+’ ‘ 4’ ‘ 3’ ‘ 2’ What infix,

A Binary Expression Tree ‘*’ ‘+’ ‘ 4’ ‘ 3’ ‘ 2’ What infix, prefix, postfix expressions does it represent? 22

A Binary Expression Tree is. . . A special kind of binary tree in

A Binary Expression Tree is. . . A special kind of binary tree in which: 1. Each leaf node contains a single operand, 2. Each nonleaf node contains a single binary operator, and 3. The left and right subtrees of an operator node represent subexpressions that must be evaluated before applying the operator at the root of the subtree. 23

Evaluate this binary expression tree ‘*’ ‘-’ ‘ 8’ ‘/’ ‘ 5’ ‘ 3’

Evaluate this binary expression tree ‘*’ ‘-’ ‘ 8’ ‘/’ ‘ 5’ ‘ 3’ ‘+’ ‘ 4’ ‘ 2’ What infix, prefix, postfix expressions does it represent? 24

A binary expression tree ‘*’ ‘/’ ‘-’ ‘ 8’ ‘ 5’ ‘ 3’ ‘+’

A binary expression tree ‘*’ ‘/’ ‘-’ ‘ 8’ ‘ 5’ ‘ 3’ ‘+’ ‘ 4’ ‘ 2’ Infix: ((8 -5)*((4+2)/3)) Prefix: *-85 /+423 Postfix: 8 5 - 4 2 + 3 / * has operators in order used 25

Each node contains two pointers struct Tree. Node { char data; Tree. Node* left;

Each node contains two pointers struct Tree. Node { char data; Tree. Node* left; Tree. Node* right; // Data member // Pointer to left child // Pointer to right child Tree. Node( char, Tree. Node* ); // Constructor }; NULL. left ‘A’ 6000 . data . right 26

Or, using a class for nodes class Expr. Tree. Node { char element; Expr.

Or, using a class for nodes class Expr. Tree. Node { char element; Expr. Tree. Node* left; Expr. Tree. Node* right; // Data member // Pointer to left child // Pointer to right child // Constructor Expr. Tree. Node( char, Expr. Tree. Node* ); }; NULL ‘A’ . left . element 6000. right 27

class Expr. Tree { public: Expr. Tree ( ); // Constructor ~Expr. Tree (

class Expr. Tree { public: Expr. Tree ( ); // Constructor ~Expr. Tree ( ); // Destructor void build ( ); // build tree from prefix expression void expression ( ) const; // output expression in fully parenthesized infix form float evaluate ( ) const; // evaluate expression void clear ( ); // clear tree void show. Structure ( ) const; // display tree private: void show. Sub( ); . . . Expr. Tree. Node* root; }; // recursive partners 28

class Expr. Tree from Roberge Lab 11 Expr. Tree ~Expr. Tree build ‘*’ private:

class Expr. Tree from Roberge Lab 11 Expr. Tree ~Expr. Tree build ‘*’ private: root ‘+’ ‘ 3’ expression evaluate ‘ 4’ ‘ 2’ clear 29

A Binary Search Tree (BST) is. . . A special kind of binary tree

A Binary Search Tree (BST) is. . . A special kind of binary tree in which: 1. Each node contains a distinct data value, 2. The data values in the tree can be compared using “greater than” and “less than”, and 3. The data value of each node in the tree is less than every data value in its right subtree, and greater than every data value in its left subtree. 30

Shape of a binary search tree. . . Depends on its values and on

Shape of a binary search tree. . . Depends on its values and on their order of insertion. Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that order. The first value to be inserted is put into the root node. ‘J’ 31

Inserting ‘E’ into the BST Thereafter, each value to be inserted begins by comparing

Inserting ‘E’ into the BST Thereafter, each value to be inserted begins by comparing itself to the value in the root node, moving left it is less, or moving right if it is greater. This continues at each level until it can be inserted as a new leaf. ‘J’ ‘E’ 32

Inserting ‘F’ into the BST Begin by comparing ‘F’ to the value in the

Inserting ‘F’ into the BST Begin by comparing ‘F’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a new leaf. ‘J’ ‘E’ ‘F’ 33

Inserting ‘T’ into the BST Begin by comparing ‘T’ to the value in the

Inserting ‘T’ into the BST Begin by comparing ‘T’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a new leaf. ‘J’ ‘T’ ‘E’ ‘F’ 34

Inserting ‘A’ into the BST Begin by comparing ‘A’ to the value in the

Inserting ‘A’ into the BST Begin by comparing ‘A’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a new leaf. ‘J’ ‘T’ ‘E’ ‘A’ ‘F’ 35

What binary search tree. . . is obtained by inserting the elements ‘A’ ‘E’

What binary search tree. . . is obtained by inserting the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order? ‘A’ 36

Binary search tree. . . obtained by inserting the elements ‘A’ ‘E’ ‘F’ ‘J’

Binary search tree. . . obtained by inserting the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order. ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ 37

Another binary search tree ‘J’ ‘T’ ‘E’ ‘A’ ‘H’ ‘V’ ‘M’ ‘P’ ‘K’ Where

Another binary search tree ‘J’ ‘T’ ‘E’ ‘A’ ‘H’ ‘V’ ‘M’ ‘P’ ‘K’ Where would nodes containing these values be added? ‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’ 38

Each node contains two pointers struct Tree. Node { char data; Tree. Node* left;

Each node contains two pointers struct Tree. Node { char data; Tree. Node* left; Tree. Node* right; // Data member // Pointer to left child // Pointer to right child Tree. Node( char, Tree. Node* ); // Constructor }; typedef Tree. Node* Node. Ptr; NULL. left ‘A’ 6000 . data . right 40

// - - - - - - - - - - - - -

// - - - - - - - - - - - - - // SPECIFICATION FILE charbst. h // Exports an ADT for a binary search tree containing char values // - - - - - - - - - - - - - class Char. BSTree { public: // MEMBER FUNCTIONS Char. BSTree ( ); // Constructor void Insert ( /* in */ char some. Char ); // insert into BST void Inorder. Traverse( ) const; // tree traversal Boolean Is. Empty( ) const; Boolean Has. Element( /* in */ char some. Char ) const; void Clear ( ); // deallocate all nodes ~Char. BSTree ( ); // Destructor private: Node. Ptr root; }; // data member pointer to tree 41

// SPECIFICATION FILE (continued) charbst. h // - - - - - - -

// SPECIFICATION FILE (continued) charbst. h // - - - - - - - - - - - - - // RECURSIVE PARTNERS OF MEMBER FUNCTIONS void Inorder. Traverse. Helper ( /* in */ Node. Ptr ptr ) ; void Insert. Helper ( /* inout */ Node. Ptr& ptr, /* in */ char some. Char ) ; Boolean Has. Element. Helper ( /* in */ Node. Ptr ptr, /* in */ char some. Char ) ; void Clear. Helper( /* in */ Node. Ptr ptr ) ; 42

class Char. BSTree ‘J’ Char. BSTree ~Char. BSTree Has. Element private: root ‘E’ ‘S’

class Char. BSTree ‘J’ Char. BSTree ~Char. BSTree Has. Element private: root ‘E’ ‘S’ Is. Empty Insert Inorder. Traverse. . . ‘H’ ‘A ‘V’ ‘C’ 43

// IMPLEMENTATION FILE charbst. h Tree. Node: : Tree. Node( /* in */ char

// IMPLEMENTATION FILE charbst. h Tree. Node: : Tree. Node( /* in */ char some. Char, /* in */ Node. Ptr left. Ptr, /* in */ Node. Ptr right. Ptr ) { data = some. Char; left = left. Ptr; right = right. Ptr; } // - - - - - - - - - - - - - Char. BSTree: : Char. BSTree( ) // Constructor { root = NULL; } // - - - - - - - - - - - - - Boolean Char. BSTree: : Is. Empty( ) const { return ( root == NULL ); 44 }

// IMPLEMENTATION FILE (continued) charbst. h // - - - - - - -

// IMPLEMENTATION FILE (continued) charbst. h // - - - - - - - - - - - - - Boolean Char. BSTree: : Has. Element ( /* in */ char some. Char ) const { return Has. Element. Helper( root, some. Char ); } // - - - - - - - - - - - - - Boolean Has. Element. Helper ( /* in */ Node. Ptr ptr, /* in */ char some. Char ) { if ( ptr != NULL ) { if ( some. Char == ptr->data ) return TRUE; else if ( some. Char < ptr->data ) return Has. Element. Helper( ptr->left , some. Char ); else return Has. Element. Helper( ptr->right, some. Char); } else return FALSE; 45 }

// IMPLEMENTATION FILE (continued) charbst. h // - - - - - - -

// IMPLEMENTATION FILE (continued) charbst. h // - - - - - - - - - - - - - void Char. BSTree: : Clear ( ) { Clear. Helper( root ); root = NULL ; } // - - - - - - - - - - - - - void Clear. Helper( /* in */ Node. Ptr ptr ) { if ( ptr != NULL ) { Clear. Helper ( ptr-> left ); Clear. Helper ( ptr-> right ); delete ptr ; } } // - - - - - - - - - - - - - Char BSTree: : ~Char. BSTree ( ) { Clear( ); } 46

// IMPLEMENTATION FILE (continued) charbst. h // - - - - - - -

// IMPLEMENTATION FILE (continued) charbst. h // - - - - - - - - - - - - - void Char. BSTree: : Inorder. Traverse ( ) const { Inorder. Traverse. Helper( root ); } // - - - - - - - - - - - - - void Inorder. Traverse. Helper ( /* in */ Node. Ptr ptr ) { if ( ptr != NULL ) { Inorder. Traverse. Helper( ptr-> left ); cout << ptr->data << endl; Inorder. Traverse. Helper( ptr->right ); } } 47

// IMPLEMENTATION FILE (continued) charbst. h // - - - - - - -

// IMPLEMENTATION FILE (continued) charbst. h // - - - - - - - - - - - - - void Char. BSTree: : Insert ( /* in */ char some. Char ) { Insert. Helper( root , some. Char ); } // - - - - - - - - - - - - - void Insert. Helper ( /* inout */ Node. Ptr& ptr, /* in */ char some. Char ) // PRE: some. Char is not already in this BST { if ( ptr != NULL ) { if ( some. Char < ptr->data ) // go left Insert. Helper( ptr-> left , some. Char ); else // go right Insert. Helper( ptr->right , some. Char ); } else } // attach as leaf ptr = new Tree. Node(some. Char, NULL); 48