binary tree robb t. binary tree...

29
Binary Tree Implementa- tion Robb T. Koether The Binary Tree Interface Array Imple- mentation Linked Imple- mentation Assignment Binary Tree Implementation Lecture 31 Sections 12.2 - 12.3 Robb T. Koether Hampden-Sydney College Mon, Apr 6, 2009

Upload: vuongnga

Post on 14-Jun-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Binary Tree ImplementationLecture 31

Sections 12.2 - 12.3

Robb T. Koether

Hampden-Sydney College

Mon, Apr 6, 2009

Page 2: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Outline

1 The Binary Tree Interface

2 Array Implementation

3 Linked Implementation

4 Assignment

Page 3: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Binary Tree Constructors

Binary Tree ConstructorsBinaryTree();

Constructs an empty binary tree.BinaryTree(const T& value);

Constructs a binary tree with one node with thespecified value.

BinaryTree(const BinaryTree& lft,const BinaryTree& rgt);

Constructs a binary tree with the specified left and rightsubtrees.

Page 4: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Binary Tree Constructors

Binary Tree ConstructorsBinaryTree(const T& value,const BinaryTree& lft,const BinaryTree& rgt);

Constructs a binary tree with the specified root valueand the specified left and right subtrees.

BinaryTree(const BinaryTree& tree);Constructs a copy of an existing binary tree.

Page 5: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Binary Tree Destructor

Binary Tree Destructor˜BinaryTree();

Destroys the binary tree.

Page 6: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Binary Tree Inspectors

Binary Tree Inspectorsint size() const;

Returns the number of nodes in the binary tree.int height() const;

Returns the height of the binary tree.bool isEmpty() const;

Determines whether the binary tree is empty.T& rootValue() const;

Returns a reference to the value in the root node.

Page 7: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Binary Tree Inspectors

Binary Tree InspectorsBinaryTree leftSubtree() const;

Returns a copy of the left subtree.BinaryTree rightSubtree() const;

Returns a copy of the right subtree.bool isCountBalanced() const;

Determines whether the binary tree is count balanced.bool isHeightBalanced() const;

Determines whether the binary tree is height balanced.

Page 8: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Binary Tree Mutators

Binary Tree Mutatorsvoid makeEmpty();

Removes all the nodes from the binary tree.void setRootValue(const T& value);

Sets the root value to the specified value.

Page 9: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Binary Tree Facilitators

Binary Tree Facilitatorsvoid input(istream& in);

Reads a binary tree from the input stream.void output(ostream& out) const;

Writes a binary tree to the output stream.bool isEqual(BinaryTree tree) const;

Determines whether two binary trees are equal.

Page 10: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Binary Tree Operators

Binary Tree Operators

BinaryTree& operator=(const BinaryTree& t);

Assigns a binary tree.

istream& operator>>(istream& in, BinaryTree&t);

Reads a binary tree from the input stream.

ostream& operator<<(ostream& out,const BinaryTree& t);

Writes a binary tree to the output stream.

bool operator==(BinaryTree& t);

Determines whether two binary trees are equal.

Page 11: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Binary Tree Traversal Functions

Binary Tree Traversal Functionsvoid preorderTraversal(void(*visit)(BinaryTreeNode*)) const;

Performs a pre-order traversal of the binary tree.void inorderTraversal(void(*visit)(BinaryTreeNode*)) const;

Performs an in-order traversal of the binary tree.void postorderTraversal(void(*visit)(BinaryTreeNode*)) const;

Performs a post-order traversal of the binary tree.

Page 12: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Binary Tree Traversal Functions

Binary Tree Traversal Functionsvoid levelorderTraversal(void(*visit)(BinaryTreeNode*)) const;

Performs a level-order traversal of the binary tree.

Page 13: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Other Binary Tree Functions

Other Binary Tree FunctionsT* search(const T& value) const;

Searches the binary tree for a specified value.void draw() const;

Draws a representation of the binary tree.

Page 14: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Array Implementation of a Binary Tree

In an array binary tree, the nodes of the tree are storedin an array.Position 0 is left empty.The root is stored in position 1.For the element in position n,

The left child is in position 2n.The right child is in position 2n + 1.

For the element in position n, the parent is in positionn/2.

Page 15: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Array Implementation

10 20 30 40 50 60 70

Page 16: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Array Implementation

10 20 30 40 50 60 70

Unused

Page 17: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Array Implementation

10 20 30 40 50 60 70

Root

Page 18: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Array Implementation

10 20 30 40 50 60 70

Parent

Page 19: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Array Implementation

10 20 30 40 50 60 70

Parent

Parents, do you know where your children are?

Page 20: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Array Implementation

10 20 30 40 50 60 70

Parent

Yes, they are at 2n and 2n + 1.

Page 21: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Array Implementation

10 20 30 40 50 60 70

Children

Children, do you know where your parents are?

Page 22: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Array Implementation

10 20 30 40 50 60 70

Children

Yes, Mom and Dad are at floor(n/2).

Page 23: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Array Implementation

10 20 30 40 50 60 70

Children

Page 24: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Advantages of the Array Implementation

This representation is very efficient whenThe tree is complete, andThe structure of the tree will not be modified.

Page 25: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Linked Binary Tree Implementation

As we have seen, the linked implementation usesBinaryTreeNodes.Each BinaryTreeNode has two node pointers, onethe the left subtree and one to the right subtree.The BinaryTree itself consists of a single nodepointer to the root node.

Page 26: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Linked Binary Tree Implementation

ConstructorBinaryTree(const T& value,const BinaryTree& lft,const BinaryTree& rgt);

Implement the above constructor.

Page 27: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Linked Binary Tree Implementation

The Destructor and makeEmpty()

˜BinaryTree();

void makeEmpty();

Implement the destructor along with the recursive andnon-recursive makeEmpty() functions.

Page 28: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Linked Binary Tree Implementation

makeCopy()

void makeCopy(BinaryTreeNode*& new_node,const BinaryTreeNode* old_node);

Implement the private, recursive function makeCopy().

Page 29: Binary Tree Robb T. Binary Tree Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009... · Binary Tree Implementa-tion Robb T. Koether The Binary Tree Interface

Binary TreeImplementa-

tion

Robb T.Koether

The BinaryTree Interface

Array Imple-mentation

Linked Imple-mentation

Assignment

Assignment

HomeworkRead Section 12.2, pages 651 - 658.Read Section 12.3, pages 660 - 664.