binary tree implementation -

31
Binary Tree Implementation Lecture 31 Sections 12.2 - 12.3 Robb T. Koether Hampden-Sydney College Mon, Apr 5, 2010 Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 1 / 25

Upload: others

Post on 12-Sep-2021

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Binary Tree Implementation -

Binary Tree ImplementationLecture 31

Sections 12.2 - 12.3

Robb T. Koether

Hampden-Sydney College

Mon, Apr 5, 2010

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 1 / 25

Page 2: Binary Tree Implementation -

Outline

1 The Binary Tree Interface

2 Array Implementation

3 Linked Implementation

4 Assignment

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 2 / 25

Page 3: Binary Tree Implementation -

Outline

1 The Binary Tree Interface

2 Array Implementation

3 Linked Implementation

4 Assignment

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 3 / 25

Page 4: Binary Tree Implementation -

Binary Tree Constructors

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

const BinaryTree& rgt);

BinaryTree() – Constructs an empty binary tree.BinaryTree(T) – Constructs a binary tree with one node withthe specified value.BinaryTree(BinaryTree, BinaryTree) – Constructs abinary tree with the specified left and right subtrees.

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 4 / 25

Page 5: Binary Tree Implementation -

Binary Tree Constructors

Binary Tree ConstructorsBinaryTree(const T& value, const BinaryTree& lft,

const BinaryTree& rgt);BinaryTree(const BinaryTree& tree);

BinaryTree(T, BinaryTree, BinaryTree) – Constructs abinary tree with the specified root value and the specified left andright subtrees.BinaryTree(BinaryTree) – Constructs a copy of an existingbinary tree.

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 5 / 25

Page 6: Binary Tree Implementation -

Binary Tree Destructor

Binary Tree Destructor˜BinaryTree();

˜BinaryTree() – Destroys the binary tree.

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 6 / 25

Page 7: Binary Tree Implementation -

Binary Tree Inspectors

Binary Tree Inspectorsint size() const;int height() const;bool isEmpty() const;T rootValue() const;T& rootValue();

size() – Returns the number of nodes in the binary tree.height() – Returns the height of the binary tree.isEmpty() – Determines whether the binary tree is empty.rootValue() const – Returns a copy the value in the rootnode.rootValue() – Returns a reference to the value in the root node.

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 7 / 25

Page 8: Binary Tree Implementation -

Binary Tree Inspectors

Binary Tree InspectorsBinaryTree leftSubtree() const;BinaryTree rightSubtree() const;bool isCountBalanced() const;bool isHeightBalanced() const;

leftSubtree() – Returns a copy of the left subtree.rightSubtree() – Returns a copy of the right subtree.isCountBalanced() – Determines whether the binary tree iscount balanced.isHeightBalanced() – Determines whether the binary tree isheight balanced.

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 8 / 25

Page 9: Binary Tree Implementation -

Binary Tree Mutators

Binary Tree Mutatorsvoid makeEmpty();

makeEmpty() – Removes all the nodes from the binary tree.

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 9 / 25

Page 10: Binary Tree Implementation -

Binary Tree Facilitators

Binary Tree Facilitatorsvoid input(istream& in);void output(ostream& out) const;bool isEqual(BinaryTree tree) const;

input() – Reads a binary tree from the input stream.output() – Writes a binary tree to the output stream.isEqual() – Determines whether two binary trees are equal.

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 10 / 25

Page 11: Binary Tree Implementation -

Binary Tree Operators

Binary Tree OperatorsBinaryTree& operator=(const BinaryTree& t);istream& operator>>(istream& in, BinaryTree& t);ostream& operator<<(ostream& out, const BinaryTree& t);bool operator==(const BinaryTree& t1, const BinaryTree& t2);bool operator!=(const BinaryTree& t1, const BinaryTree& t2);

operator=() – Assigns a binary tree.operator>>() – Reads a binary tree from the input stream.operator<<() – Writes a binary tree to the output stream.operator==() – Determines whether two binary trees are equal.operator!=() – Determines whether two binary trees are notequal.

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 11 / 25

Page 12: Binary Tree Implementation -

Binary Tree Traversal Functions

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

preorderTraversal() – Performs a pre-order traversal of thebinary tree.inorderTraversal() – Performs an in-order traversal of thebinary tree.postorderTraversal() – Performs a post-order traversal ofthe binary tree.levelorderTraversal() – Performs a level-order traversal ofthe binary tree.

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 12 / 25

Page 13: Binary Tree Implementation -

Other Binary Tree Functions

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

search() – Searches the binary tree for a specified value.draw() – Draws a representation of the binary tree.

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 13 / 25

Page 14: Binary Tree Implementation -

Outline

1 The Binary Tree Interface

2 Array Implementation

3 Linked Implementation

4 Assignment

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 14 / 25

Page 15: Binary Tree Implementation -

Array Implementation of a Binary Tree

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

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

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

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 15 / 25

Page 16: Binary Tree Implementation -

Array Implementation

10 20 30 40 50 60 70

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 16 / 25

Page 17: Binary Tree Implementation -

Array Implementation

10 20 30 40 50 60 70

Unused

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 16 / 25

Page 18: Binary Tree Implementation -

Array Implementation

10 20 30 40 50 60 70

Root

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 16 / 25

Page 19: Binary Tree Implementation -

Array Implementation

10 20 30 40 50 60 70

Parent

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 16 / 25

Page 20: Binary Tree Implementation -

Array Implementation

10 20 30 40 50 60 70

Parent

Parents, do you know where your children are?

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 16 / 25

Page 21: Binary Tree Implementation -

Array Implementation

10 20 30 40 50 60 70

Parent

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

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 16 / 25

Page 22: Binary Tree Implementation -

Array Implementation

10 20 30 40 50 60 70

Children

Children, do you know where your parents are?

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 16 / 25

Page 23: Binary Tree Implementation -

Array Implementation

10 20 30 40 50 60 70

Children

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

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 16 / 25

Page 24: Binary Tree Implementation -

Advantages of the Array Implementation

This representation is very efficient whenI The tree is complete, andI The structure of the tree will not be modified.

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 17 / 25

Page 25: Binary Tree Implementation -

Outline

1 The Binary Tree Interface

2 Array Implementation

3 Linked Implementation

4 Assignment

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 18 / 25

Page 26: Binary Tree Implementation -

Linked Binary Tree Implementation

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

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 19 / 25

Page 27: Binary Tree Implementation -

Linked Binary Tree Implementation

ConstructorBinaryTree(const T& value, const BinaryTree& lft,

const BinaryTree& rgt);

Implement the above constructor.

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 20 / 25

Page 28: Binary Tree Implementation -

Linked Binary Tree Implementation

The Destructor and makeEmpty()˜BinaryTree();void makeEmpty();

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

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 21 / 25

Page 29: Binary Tree Implementation -

Linked Binary Tree Implementation

makeCopy()void makeCopy(BinaryTreeNode*& new_node,

const BinaryTreeNode* old_node);

Implement the private, recursive function makeCopy().

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 22 / 25

Page 30: Binary Tree Implementation -

Outline

1 The Binary Tree Interface

2 Array Implementation

3 Linked Implementation

4 Assignment

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 23 / 25

Page 31: Binary Tree Implementation -

Assignment

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

Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 24 / 25