binary trees - pepperdine university · the formal definition of a binary tree is recursive. •...

47
Design Patterns for Data Structures Binary Trees Chapter 8

Upload: others

Post on 21-May-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures

Binary Trees

Chapter 8

Page 2: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Chapter 8

Chapter 8

Binary Trees

Although it is possible to design binary trees that are immutable, the binary trees in thischapter are all mutable. Chapter 7 presents three mutable list data structures, ListL,the classic linked implementation, ListCS, the composite state pattern implementation,and ListCSV, the composite state visitor pattern implementation. This chapter presentsthe same three implementations of binary trees as follows.

• BiTreeL Classic linked implementation• BiTreeCS Composite and State patterns• BiTreeCSV Composite, State, and Visitor patterns

As is the case with mutable linked lists, each of these binary tree implementations usenodes with state to achieve their mutability.

The formal definition of a binary tree is recursive.• An empty tree is a binary tree.• A nonempty binary tree has three parts:

– a left child, which is a binary tree,– a data value of some type T, and– a right child, which is a binary tree.

Many of the properties of binary trees are recursive and are implemented with a compu-tational structure that mirrors this formal definition.

8.1 The Classic Linked ImplementationFigure 8.1 shows two common implementations of the classic linked binary tree. Part(a) is the abstract tree, and part (b) is the simpler implementation. A node has threeattributes, _left, _data, and _right.

template<class T> class LNode {private:

LNode *_left;T _data;LNode *_right;

261

Page 3: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Chapter 8

Chapter 8

Binary Trees

Although it is possible to design binary trees that are immutable, the binary trees in thischapter are all mutable. Chapter 7 presents three mutable list data structures, ListL,the classic linked implementation, ListCS, the composite state pattern implementation,and ListCSV, the composite state visitor pattern implementation. This chapter presentsthe same three implementations of binary trees as follows.

• BiTreeL Classic linked implementation• BiTreeCS Composite and State patterns• BiTreeCSV Composite, State, and Visitor patterns

As is the case with mutable linked lists, each of these binary tree implementations usenodes with state to achieve their mutability.

The formal definition of a binary tree is recursive.• An empty tree is a binary tree.• A nonempty binary tree has three parts:

– a left child, which is a binary tree,– a data value of some type T, and– a right child, which is a binary tree.

Many of the properties of binary trees are recursive and are implemented with a compu-tational structure that mirrors this formal definition.

8.1 The Classic Linked ImplementationFigure 8.1 shows two common implementations of the classic linked binary tree. Part(a) is the abstract tree, and part (b) is the simpler implementation. A node has threeattributes, _left, _data, and _right.

template<class T> class LNode {private:

LNode *_left;T _data;LNode *_right;

261

Page 4: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.1

262 Chapter 8 Binary Trees

8070

30

90 50 40

30

80

4050

70

90

(a) An abstract binary tree.

(b) A linked binary tree with left and right links. (c) A linked binary tree with left, right, and parent links.

30

70 80

5090 40

Figure 8.1 Classic linked binary trees. The trees in parts (b) and (c) are two differentimplementations of the tree in part (a).

Attribute _left is a pointer to a LNode and points to the root node in the left subtree.Attribute _data has type T. Attribute _right is a pointer to a LNode and points to theroot node in the right subtree. The binary tree itself consists of a single pointer to theroot node of the binary tree.

template<class T> class BiTreeL {private:

LNode<T> *_root;

A node with no left subtree in the binary tree has a value of nullptr for _left, whichis used to detect the bottom of a branch.

Some binary tree algorithms require a node to have direct access to its parent node.Figure 8.1(c) shows the structure of a node in such a scenario. The node is augmentedwith an additional field that points to the parent node.

template<class T> class LNode {private:

LNode *_parent;LNode *_left;T _data;LNode *_right;

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 5: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.1

262 Chapter 8 Binary Trees

8070

30

90 50 40

30

80

4050

70

90

(a) An abstract binary tree.

(b) A linked binary tree with left and right links. (c) A linked binary tree with left, right, and parent links.

30

70 80

5090 40

Figure 8.1 Classic linked binary trees. The trees in parts (b) and (c) are two differentimplementations of the tree in part (a).

Attribute _left is a pointer to a LNode and points to the root node in the left subtree.Attribute _data has type T. Attribute _right is a pointer to a LNode and points to theroot node in the right subtree. The binary tree itself consists of a single pointer to theroot node of the binary tree.

template<class T> class BiTreeL {private:

LNode<T> *_root;

A node with no left subtree in the binary tree has a value of nullptr for _left, whichis used to detect the bottom of a branch.

Some binary tree algorithms require a node to have direct access to its parent node.Figure 8.1(c) shows the structure of a node in such a scenario. The node is augmentedwith an additional field that points to the parent node.

template<class T> class LNode {private:

LNode *_parent;LNode *_left;T _data;LNode *_right;

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

262 Chapter 8 Binary Trees

8070

30

90 50 40

30

80

4050

70

90

(a) An abstract binary tree.

(b) A linked binary tree with left and right links. (c) A linked binary tree with left, right, and parent links.

30

70 80

5090 40

Figure 8.1 Classic linked binary trees. The trees in parts (b) and (c) are two differentimplementations of the tree in part (a).

Attribute _left is a pointer to a LNode and points to the root node in the left subtree.Attribute _data has type T. Attribute _right is a pointer to a LNode and points to theroot node in the right subtree. The binary tree itself consists of a single pointer to theroot node of the binary tree.

template<class T> class BiTreeL {private:

LNode<T> *_root;

A node with no left subtree in the binary tree has a value of nullptr for _left, whichis used to detect the bottom of a branch.

Some binary tree algorithms require a node to have direct access to its parent node.Figure 8.1(c) shows the structure of a node in such a scenario. The node is augmentedwith an additional field that points to the parent node.

template<class T> class LNode {private:

LNode *_parent;LNode *_left;T _data;LNode *_right;

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Chapter 8

Binary Trees

Although it is possible to design binary trees that are immutable, the binary trees in thischapter are all mutable. Chapter 7 presents three mutable list data structures, ListL,the classic linked implementation, ListCS, the composite state pattern implementation,and ListCSV, the composite state visitor pattern implementation. This chapter presentsthe same three implementations of binary trees as follows.

• BiTreeL Classic linked implementation• BiTreeCS Composite and State patterns• BiTreeCSV Composite, State, and Visitor patterns

As is the case with mutable linked lists, each of these binary tree implementations usenodes with state to achieve their mutability.

The formal definition of a binary tree is recursive.• An empty tree is a binary tree.• A nonempty binary tree has three parts:

– a left child, which is a binary tree,– a data value of some type T, and– a right child, which is a binary tree.

Many of the properties of binary trees are recursive and are implemented with a compu-tational structure that mirrors this formal definition.

8.1 The Classic Linked ImplementationFigure 8.1 shows two common implementations of the classic linked binary tree. Part(a) is the abstract tree, and part (b) is the simpler implementation. A node has threeattributes, _left, _data, and _right.

template<class T> class LNode {private:

LNode *_left;T _data;LNode *_right;

261

Page 6: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.1

262 Chapter 8 Binary Trees

8070

30

90 50 40

30

80

4050

70

90

(a) An abstract binary tree.

(b) A linked binary tree with left and right links. (c) A linked binary tree with left, right, and parent links.

30

70 80

5090 40

Figure 8.1 Classic linked binary trees. The trees in parts (b) and (c) are two differentimplementations of the tree in part (a).

Attribute _left is a pointer to a LNode and points to the root node in the left subtree.Attribute _data has type T. Attribute _right is a pointer to a LNode and points to theroot node in the right subtree. The binary tree itself consists of a single pointer to theroot node of the binary tree.

template<class T> class BiTreeL {private:

LNode<T> *_root;

A node with no left subtree in the binary tree has a value of nullptr for _left, whichis used to detect the bottom of a branch.

Some binary tree algorithms require a node to have direct access to its parent node.Figure 8.1(c) shows the structure of a node in such a scenario. The node is augmentedwith an additional field that points to the parent node.

template<class T> class LNode {private:

LNode *_parent;LNode *_left;T _data;LNode *_right;

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

262 Chapter 8 Binary Trees

8070

30

90 50 40

30

80

4050

70

90

(a) An abstract binary tree.

(b) A linked binary tree with left and right links. (c) A linked binary tree with left, right, and parent links.

30

70 80

5090 40

Figure 8.1 Classic linked binary trees. The trees in parts (b) and (c) are two differentimplementations of the tree in part (a).

Attribute _left is a pointer to a LNode and points to the root node in the left subtree.Attribute _data has type T. Attribute _right is a pointer to a LNode and points to theroot node in the right subtree. The binary tree itself consists of a single pointer to theroot node of the binary tree.

template<class T> class BiTreeL {private:

LNode<T> *_root;

A node with no left subtree in the binary tree has a value of nullptr for _left, whichis used to detect the bottom of a branch.

Some binary tree algorithms require a node to have direct access to its parent node.Figure 8.1(c) shows the structure of a node in such a scenario. The node is augmentedwith an additional field that points to the parent node.

template<class T> class LNode {private:

LNode *_parent;LNode *_left;T _data;LNode *_right;

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

262 Chapter 8 Binary Trees

8070

30

90 50 40

30

80

4050

70

90

(a) An abstract binary tree.

(b) A linked binary tree with left and right links. (c) A linked binary tree with left, right, and parent links.

30

70 80

5090 40

Figure 8.1 Classic linked binary trees. The trees in parts (b) and (c) are two differentimplementations of the tree in part (a).

Attribute _left is a pointer to a LNode and points to the root node in the left subtree.Attribute _data has type T. Attribute _right is a pointer to a LNode and points to theroot node in the right subtree. The binary tree itself consists of a single pointer to theroot node of the binary tree.

template<class T> class BiTreeL {private:

LNode<T> *_root;

A node with no left subtree in the binary tree has a value of nullptr for _left, whichis used to detect the bottom of a branch.

Some binary tree algorithms require a node to have direct access to its parent node.Figure 8.1(c) shows the structure of a node in such a scenario. The node is augmentedwith an additional field that points to the parent node.

template<class T> class LNode {private:

LNode *_parent;LNode *_left;T _data;LNode *_right;

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 7: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.28.1 The Classic Linked Implementation 263

BiTreeL

– BiTreeL(rhs: BiTreeL<T> const &)+ BiTreeL( )+ ~BiTreeL( )+ clear( )+ contains(data: T const &) const: bool– copyRoot(rhs: BiTreeL<T> const &): LNode<T> *+ equals(rhs: BiTreeL<T> const &) const: bool+ height( ) const: int+ inOrder(os: ostream &) const+ insertRoot(data: T const &);+ isEmpty( ) const: bool+ leftIsEmpty( ) const: bool+ max( ) const: T const &+ numLeaves( ) const: int+ numNodes( ) const: int+ operator=(rhs: const BiTreeL<T> &): BiTreeL &+ postOrder(os: ostream &) const+ preOrder(os: ostream &) const+ remLeaves( )+ remRoot( ): T+ rightIsEmpty( ) const: bool+ root( ) const: T const &+ setLeft(subTree: BiTreeL<T> &)+ setRight(subTree: BiTreeL<T> &)+ setRoot(data: T const &)+ toStream(os: ostream &) const

T

– _root: LnodeT<T> * LNode– _left: LNode *– _data: T– _right: LNode *

– LNode(data: T)– clear( )– contains(data: T const &) const: bool– copyRoot( ): LNode<T> *– equals(rhs: LNode<T> const *) const: bool– height( ) const: int– inOrder(os: ostream &) const– max( ) const: T const &– numLeaves( ) const: int– numNodes( ) const: int– postOrder(os: ostream &) const– preOrder(os: ostream &) const– remLeaves( )– root( ) const: T const &– setLeft(subTree: BiTreeL<T> &)– setRight(subTree: BiTreeL<T> &)– setRoot(data: T const &)– toStream(prRight: string, prRoot: string, prLeft: string, os: ostream &) const

T

Figure 8.2 The UML diagram for the BiTreeL data structure.

With this data structure, a nullptr value for _parent is used to detect the root of abinary tree.

The dp4ds linked binary tree

Hello, here is some text without a meaning. This text should show what a printed textwill look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

Page 8: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.3264 Chapter 8 Binary Trees

(b) An abbreviated rendering.(a) A detailed rendering of a binary tree and a node.

myBiTree

*myBiTree._root

myBiTree._root->_data

myBiTree._root

myBiTree._root->_right

myBiTree._root->_left

Figure 8.3 Rendering of the BiTreeL binary tree and the NodeL node.

“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Figure 8.2 shows that BiTreeL has a total of 26 constructors, destructors, and meth-ods. In addition, class NodeL has a private constructor. Most of the BiTreeL functionshave corresponding NodeL functions. This pairing of BiTreeL functions with NodeLfunctions is necessary for two reasons. First, most of the methods for binary trees requirerecursive calls for the left and right subtrees of a node. Second, the classical linked im-plementation of a binary tree does not use the composite design pattern. That is, the leftchild of a node is not a pointer to a tree, but a pointer to a node. Therefore, the recursivecall that a node makes cannot call a tree method, but a node method. The general codingpattern is that the method for the tree calls the corresponding method for the root node.Then, the root node recursively calls the node methods for its left and right subtrees.

Each of the operations in Figure 8.2 falls into one of the following three categories:

• Output and characterization

• Construction and insertion

• Destruction and removal

The code in the dp4ds distribution specifies preconditions and postconditions for each

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 9: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures

Demo BiTreeL

Chapter 8

Page 10: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures

Methods for output and characterization

BiTreeL

Page 11: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.4

8.1 The Classic Linked Implementation 265

void toStream(ostream &os) const;// Post: A string representation of this tree is sent to os.

void preOrder(ostream &os) const;// Post: A preorder representation of this tree is sent to os.

void inOrder(ostream &os) const;// Post: An inorder representation of this tree is sent to os.

void postOrder(ostream &os) const;// Post: A postorder representation of this tree is sent to os.

bool isEmpty() const;// Post: true is returned if this tree is empty;// otherwise, false is returned.

bool leftIsEmpty() const;// Pre: This tree is not empty.// Post: true is returned if the left subtree of this tree is empty;// otherwise, false is returned.

bool rightIsEmpty() const;// Pre: This tree is not empty.// Post: true is returned if the right subtree of this tree is empty;// otherwise, false is returned.

Figure 8.4 Specification for the output and characterization operations for theBiTreeL data structure. The specification continues in the next figure.

of the methods. The following sections describe the methods, giving the code for someof them and leaving the code for others as exercises at the end of the chapter.

Output and characterizationHello, here is some text without a meaning. This text should show what a printed textwill look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

Page 12: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.4

8.1 The Classic Linked Implementation 265

void toStream(ostream &os) const;// Post: A string representation of this tree is sent to os.

void preOrder(ostream &os) const;// Post: A preorder representation of this tree is sent to os.

void inOrder(ostream &os) const;// Post: An inorder representation of this tree is sent to os.

void postOrder(ostream &os) const;// Post: A postorder representation of this tree is sent to os.

bool isEmpty() const;// Post: true is returned if this tree is empty;// otherwise, false is returned.

bool leftIsEmpty() const;// Pre: This tree is not empty.// Post: true is returned if the left subtree of this tree is empty;// otherwise, false is returned.

bool rightIsEmpty() const;// Pre: This tree is not empty.// Post: true is returned if the right subtree of this tree is empty;// otherwise, false is returned.

Figure 8.4 Specification for the output and characterization operations for theBiTreeL data structure. The specification continues in the next figure.

of the methods. The following sections describe the methods, giving the code for someof them and leaving the code for others as exercises at the end of the chapter.

Output and characterizationHello, here is some text without a meaning. This text should show what a printed textwill look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

Page 13: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.5

266 Chapter 8 Binary Trees

T const &root() const;// Pre: This tree is not empty.// Post: The root element of this tree is returned.

int numNodes() const;// Post: The number of nodes of the host tree is returned.

int numLeaves() const;// Post: The number of leaves of the host tree is returned.

int height() const;// Post: The height of the host tree is returned.

T const &max() const;// Pre: This tree is not empty.// Post: The maximum element of this tree is returned.

bool contains(T const &data) const;// Post: true is returned if val is contained in this tree;// otherwise, false is returned.

bool equals(BiTreeL<T> const &rhs) const;// Post: true is returned if this tree equals tree rhs;// otherwise, false is returned.// Two trees are equal if they contain the same number// of equal elements with the same shape.

Figure 8.5 Specification for the output and characterization operations for theBiTreeL data structure (continued). This completes the specification.

about the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 14: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.5

266 Chapter 8 Binary Trees

T const &root() const;// Pre: This tree is not empty.// Post: The root element of this tree is returned.

int numNodes() const;// Post: The number of nodes of the host tree is returned.

int numLeaves() const;// Post: The number of leaves of the host tree is returned.

int height() const;// Post: The height of the host tree is returned.

T const &max() const;// Pre: This tree is not empty.// Post: The maximum element of this tree is returned.

bool contains(T const &data) const;// Post: true is returned if val is contained in this tree;// otherwise, false is returned.

bool equals(BiTreeL<T> const &rhs) const;// Post: true is returned if this tree equals tree rhs;// otherwise, false is returned.// Two trees are equal if they contain the same number// of equal elements with the same shape.

Figure 8.5 Specification for the output and characterization operations for theBiTreeL data structure (continued). This completes the specification.

about the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 15: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.68.1 The Classic Linked Implementation 267

// ========= operator<< =========template<class T>ostream & operator<<(ostream &os, BiTreeL<T> const &rhs) {

rhs.toStream(os);return os;

}

// ========= toStream =========template<class T>void BiTreeL<T>::toStream(ostream &os) const {

if (_root == nullptr) {os << ”*”;

}else {

_root->toStream(””, ””, ””, os);}

}

-*40--|

| -*80--|

| | -*| 50--|| -*

30--|| -*| 90--|| | -*70--|

-*

30

70 80

5090 40

Figure 8.6 Output operations for the BiTreeL data structure. See the dp4ds distri-bution for the node implementation of toStream().

about the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Because a list is a one-dimensional data structure, there is only one way to traverse it,namely from its head to the end of the list. Binary trees are inherently two-dimensionalstructures, which gives rise to more than one way to traverse them. The three most com-mon traversals are preorder, inorder, and postorder. Each traversal is defined recursivelyas follows.

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

Node implementation of toStream() is not shown.

Page 16: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.6

8.1 The Classic Linked Implementation 267

// ========= operator<< =========template<class T>ostream & operator<<(ostream &os, BiTreeL<T> const &rhs) {

rhs.toStream(os);return os;

}

// ========= toStream =========template<class T>void BiTreeL<T>::toStream(ostream &os) const {

if (_root == nullptr) {os << ”*”;

}else {

_root->toStream(””, ””, ””, os);}

}

-*40--|

| -*80--|

| | -*| 50--|| -*

30--|| -*| 90--|| | -*70--|

-*

30

70 80

5090 40

Figure 8.6 Output operations for the BiTreeL data structure. See the dp4ds distri-bution for the node implementation of toStream().

about the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Because a list is a one-dimensional data structure, there is only one way to traverse it,namely from its head to the end of the list. Binary trees are inherently two-dimensionalstructures, which gives rise to more than one way to traverse them. The three most com-mon traversals are preorder, inorder, and postorder. Each traversal is defined recursivelyas follows.

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

Page 17: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures

8.1 The Classic Linked Implementation 267

// ========= operator<< =========template<class T>ostream & operator<<(ostream &os, BiTreeL<T> const &rhs) {

rhs.toStream(os);return os;

}

// ========= toStream =========template<class T>void BiTreeL<T>::toStream(ostream &os) const {

if (_root == nullptr) {os << ”*”;

}else {

_root->toStream(””, ””, ””, os);}

}

-*40--|

| -*80--|

| | -*| 50--|| -*

30--|| -*| 90--|| | -*70--|

-*

30

70 80

5090 40

Figure 8.6 Output operations for the BiTreeL data structure. See the dp4ds distri-bution for the node implementation of toStream().

about the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Because a list is a one-dimensional data structure, there is only one way to traverse it,namely from its head to the end of the list. Binary trees are inherently two-dimensionalstructures, which gives rise to more than one way to traverse them. The three most com-mon traversals are preorder, inorder, and postorder. Each traversal is defined recursivelyas follows.

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

268 Chapter 8 Binary Trees

// ========= preOrder =========template<class T>void BiTreeL<T>::preOrder(ostream &os) const {

if (_root != nullptr) {_root->preOrder(os);

}}

template<class T>void LNode<T>::preOrder(ostream &os) const {

os << _data << ” ”;if (_left != nullptr) {

_left->preOrder(os);}if (_right != nullptr) {

_right->preOrder(os);}

}

Figure 8.7 Implementation of the preOrder() operation for the BiTreeL datastructure.

Preorder traversal:• Visit the root.• Do a preorder traversal of the left subtree if it is not empty.• Do a preorder traversal of the right subtree if it is not empty.

Inorder traversal:• Do an inorder traversal of the left subtree if it is not empty.• Visit the root.• Do an inorder traversal of the right subtree if it is not empty.

Postorder traversal:• Do a postorder traversal of the left subtree if it is not empty.• Do a postorder traversal of the right subtree if it is not empty.• Visit the root.Here are the three traversals for the binary tree in Figure 8.6.

Preorder traversal: 30 70 90 80 50 40Inorder traversal: 70 90 30 50 80 40Postorder traversal: 90 70 50 40 80 30

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

268 Chapter 8 Binary Trees

// ========= preOrder =========template<class T>void BiTreeL<T>::preOrder(ostream &os) const {

if (_root != nullptr) {_root->preOrder(os);

}}

template<class T>void LNode<T>::preOrder(ostream &os) const {

os << _data << ” ”;if (_left != nullptr) {

_left->preOrder(os);}if (_right != nullptr) {

_right->preOrder(os);}

}

Figure 8.7 Implementation of the preOrder() operation for the BiTreeL datastructure.

Preorder traversal:• Visit the root.• Do a preorder traversal of the left subtree if it is not empty.• Do a preorder traversal of the right subtree if it is not empty.

Inorder traversal:• Do an inorder traversal of the left subtree if it is not empty.• Visit the root.• Do an inorder traversal of the right subtree if it is not empty.

Postorder traversal:• Do a postorder traversal of the left subtree if it is not empty.• Do a postorder traversal of the right subtree if it is not empty.• Visit the root.Here are the three traversals for the binary tree in Figure 8.6.

Preorder traversal: 30 70 90 80 50 40Inorder traversal: 70 90 30 50 80 40Postorder traversal: 90 70 50 40 80 30

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 18: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures

8.1 The Classic Linked Implementation 267

// ========= operator<< =========template<class T>ostream & operator<<(ostream &os, BiTreeL<T> const &rhs) {

rhs.toStream(os);return os;

}

// ========= toStream =========template<class T>void BiTreeL<T>::toStream(ostream &os) const {

if (_root == nullptr) {os << ”*”;

}else {

_root->toStream(””, ””, ””, os);}

}

-*40--|

| -*80--|

| | -*| 50--|| -*

30--|| -*| 90--|| | -*70--|

-*

30

70 80

5090 40

Figure 8.6 Output operations for the BiTreeL data structure. See the dp4ds distri-bution for the node implementation of toStream().

about the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Because a list is a one-dimensional data structure, there is only one way to traverse it,namely from its head to the end of the list. Binary trees are inherently two-dimensionalstructures, which gives rise to more than one way to traverse them. The three most com-mon traversals are preorder, inorder, and postorder. Each traversal is defined recursivelyas follows.

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

268 Chapter 8 Binary Trees

// ========= preOrder =========template<class T>void BiTreeL<T>::preOrder(ostream &os) const {

if (_root != nullptr) {_root->preOrder(os);

}}

template<class T>void LNode<T>::preOrder(ostream &os) const {

os << _data << ” ”;if (_left != nullptr) {

_left->preOrder(os);}if (_right != nullptr) {

_right->preOrder(os);}

}

Figure 8.7 Implementation of the preOrder() operation for the BiTreeL datastructure.

Preorder traversal:• Visit the root.• Do a preorder traversal of the left subtree if it is not empty.• Do a preorder traversal of the right subtree if it is not empty.

Inorder traversal:• Do an inorder traversal of the left subtree if it is not empty.• Visit the root.• Do an inorder traversal of the right subtree if it is not empty.

Postorder traversal:• Do a postorder traversal of the left subtree if it is not empty.• Do a postorder traversal of the right subtree if it is not empty.• Visit the root.Here are the three traversals for the binary tree in Figure 8.6.

Preorder traversal: 30 70 90 80 50 40Inorder traversal: 70 90 30 50 80 40Postorder traversal: 90 70 50 40 80 30

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

268 Chapter 8 Binary Trees

// ========= preOrder =========template<class T>void BiTreeL<T>::preOrder(ostream &os) const {

if (_root != nullptr) {_root->preOrder(os);

}}

template<class T>void LNode<T>::preOrder(ostream &os) const {

os << _data << ” ”;if (_left != nullptr) {

_left->preOrder(os);}if (_right != nullptr) {

_right->preOrder(os);}

}

Figure 8.7 Implementation of the preOrder() operation for the BiTreeL datastructure.

Preorder traversal:• Visit the root.• Do a preorder traversal of the left subtree if it is not empty.• Do a preorder traversal of the right subtree if it is not empty.

Inorder traversal:• Do an inorder traversal of the left subtree if it is not empty.• Visit the root.• Do an inorder traversal of the right subtree if it is not empty.

Postorder traversal:• Do a postorder traversal of the left subtree if it is not empty.• Do a postorder traversal of the right subtree if it is not empty.• Visit the root.Here are the three traversals for the binary tree in Figure 8.6.

Preorder traversal: 30 70 90 80 50 40Inorder traversal: 70 90 30 50 80 40Postorder traversal: 90 70 50 40 80 30

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 19: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures

8.1 The Classic Linked Implementation 267

// ========= operator<< =========template<class T>ostream & operator<<(ostream &os, BiTreeL<T> const &rhs) {

rhs.toStream(os);return os;

}

// ========= toStream =========template<class T>void BiTreeL<T>::toStream(ostream &os) const {

if (_root == nullptr) {os << ”*”;

}else {

_root->toStream(””, ””, ””, os);}

}

-*40--|

| -*80--|

| | -*| 50--|| -*

30--|| -*| 90--|| | -*70--|

-*

30

70 80

5090 40

Figure 8.6 Output operations for the BiTreeL data structure. See the dp4ds distri-bution for the node implementation of toStream().

about the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Because a list is a one-dimensional data structure, there is only one way to traverse it,namely from its head to the end of the list. Binary trees are inherently two-dimensionalstructures, which gives rise to more than one way to traverse them. The three most com-mon traversals are preorder, inorder, and postorder. Each traversal is defined recursivelyas follows.

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

268 Chapter 8 Binary Trees

// ========= preOrder =========template<class T>void BiTreeL<T>::preOrder(ostream &os) const {

if (_root != nullptr) {_root->preOrder(os);

}}

template<class T>void LNode<T>::preOrder(ostream &os) const {

os << _data << ” ”;if (_left != nullptr) {

_left->preOrder(os);}if (_right != nullptr) {

_right->preOrder(os);}

}

Figure 8.7 Implementation of the preOrder() operation for the BiTreeL datastructure.

Preorder traversal:• Visit the root.• Do a preorder traversal of the left subtree if it is not empty.• Do a preorder traversal of the right subtree if it is not empty.

Inorder traversal:• Do an inorder traversal of the left subtree if it is not empty.• Visit the root.• Do an inorder traversal of the right subtree if it is not empty.

Postorder traversal:• Do a postorder traversal of the left subtree if it is not empty.• Do a postorder traversal of the right subtree if it is not empty.• Visit the root.Here are the three traversals for the binary tree in Figure 8.6.

Preorder traversal: 30 70 90 80 50 40Inorder traversal: 70 90 30 50 80 40Postorder traversal: 90 70 50 40 80 30

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

268 Chapter 8 Binary Trees

// ========= preOrder =========template<class T>void BiTreeL<T>::preOrder(ostream &os) const {

if (_root != nullptr) {_root->preOrder(os);

}}

template<class T>void LNode<T>::preOrder(ostream &os) const {

os << _data << ” ”;if (_left != nullptr) {

_left->preOrder(os);}if (_right != nullptr) {

_right->preOrder(os);}

}

Figure 8.7 Implementation of the preOrder() operation for the BiTreeL datastructure.

Preorder traversal:• Visit the root.• Do a preorder traversal of the left subtree if it is not empty.• Do a preorder traversal of the right subtree if it is not empty.

Inorder traversal:• Do an inorder traversal of the left subtree if it is not empty.• Visit the root.• Do an inorder traversal of the right subtree if it is not empty.

Postorder traversal:• Do a postorder traversal of the left subtree if it is not empty.• Do a postorder traversal of the right subtree if it is not empty.• Visit the root.Here are the three traversals for the binary tree in Figure 8.6.

Preorder traversal: 30 70 90 80 50 40Inorder traversal: 70 90 30 50 80 40Postorder traversal: 90 70 50 40 80 30

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 20: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.7268 Chapter 8 Binary Trees

// ========= preOrder =========template<class T>void BiTreeL<T>::preOrder(ostream &os) const {

if (_root != nullptr) {_root->preOrder(os);

}}

template<class T>void LNode<T>::preOrder(ostream &os) const {

os << _data << ” ”;if (_left != nullptr) {

_left->preOrder(os);}if (_right != nullptr) {

_right->preOrder(os);}

}

Figure 8.7 Implementation of the preOrder() operation for the BiTreeL datastructure.

Preorder traversal:• Visit the root.• Do a preorder traversal of the left subtree if it is not empty.• Do a preorder traversal of the right subtree if it is not empty.

Inorder traversal:• Do an inorder traversal of the left subtree if it is not empty.• Visit the root.• Do an inorder traversal of the right subtree if it is not empty.

Postorder traversal:• Do a postorder traversal of the left subtree if it is not empty.• Do a postorder traversal of the right subtree if it is not empty.• Visit the root.Here are the three traversals for the binary tree in Figure 8.6.

Preorder traversal: 30 70 90 80 50 40Inorder traversal: 70 90 30 50 80 40Postorder traversal: 90 70 50 40 80 30

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 21: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.88.1 The Classic Linked Implementation 269

// ========= isEmpty =========template<class T>bool BiTreeL<T>::isEmpty() const {

return _root == nullptr;}

// ========= leftIsEmpty =========template<class T>bool BiTreeL<T>::leftIsEmpty() const {

if (_root == nullptr) {cerr << ”Precondition violated:”

<< ”Cannot test left subtree of an empty tree.”<< endl;

throw -1;}return _root->_left == nullptr;

}

Figure 8.8 Implementation of the isEmpty() and leftIsEmpty() operations forthe BiTreeL data structure.

about the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. This

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

Page 22: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.9270 Chapter 8 Binary Trees

// ========= root =========template<class T>T const &BiTreeL<T>::root() const {

if (_root == nullptr) {cerr << ”root precondition violated:”

<< ”An empty tree has no root.”<< endl;

throw -1;}return _root->root();

}

template<class T>T const &LNode<T>::root() const {

return _data;}

// ========= numNodes =========template<class T>int BiTreeL<T>::numNodes() const {

return _root == nullptr ? 0 : _root->numNodes();}

template<class T>int LNode<T>::numNodes() const {

int result = 1;if (_left != nullptr) {

result += _left->numNodes();}if (_right != nullptr) {

result += _right->numNodes();}return result;

}

Figure 8.9 Implementation of the root() and numNodes() operations for theBiTreeL data structure.

text should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. This

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 23: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.9

270 Chapter 8 Binary Trees

// ========= root =========template<class T>T const &BiTreeL<T>::root() const {

if (_root == nullptr) {cerr << ”root precondition violated:”

<< ”An empty tree has no root.”<< endl;

throw -1;}return _root->root();

}

template<class T>T const &LNode<T>::root() const {

return _data;}

// ========= numNodes =========template<class T>int BiTreeL<T>::numNodes() const {

return _root == nullptr ? 0 : _root->numNodes();}

template<class T>int LNode<T>::numNodes() const {

int result = 1;if (_left != nullptr) {

result += _left->numNodes();}if (_right != nullptr) {

result += _right->numNodes();}return result;

}

Figure 8.9 Implementation of the root() and numNodes() operations for theBiTreeL data structure.

text should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. This

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 24: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.10

8.1 The Classic Linked Implementation 271

60

40

20 65

30

30

70 80

20

90 60

height = 3

height = 1 50

15

40

height = 5

75

(a) A binary tree with leaf 60.

(b) A binary tree with leaves 20 and 30.

(c) A binary tree with leaves 90, 40, 50, and 75.

Figure 8.10 Leaves and heights of binary trees.

text should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

A leaf node is a node with no children. An internal node is a node is a node that isnot a leaf, that is, a node with at least one child. In Figure 8.10(a), the internal nodes are40 and 65 and in (b) they are 30, 70, 80, 15, 60, and 20. Implementation of operationnumLeaves() is an exercise for the student.

The definition of the height of a binary tree is recursive.

• The height of an empty tree is 0.

• The height of a nonempty tree is one plus the maximum of the heights of the leftchild and right child.

In Figure 8.10(c), the left child of 30 has a height of 3, and the the right child of 30 hasa height of 4. The maximum of 3 and 4 is 4. So, the height of the tree rooted at 30 is1+4 = 5. Implementation of operation height() is an exercise for the student.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

Page 25: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.11272 Chapter 8 Binary Trees

// ========= max =========template<class T>T const &BiTreeL<T>::max() const {

if (_root == nullptr) {cerr << ”Precondition violated:”

<< ”An empty tree has no maximum.”<< endl;

throw -1;}return _root->max();

}

template<class T>T const &LNode<T>::max() const {

T const *dataTemp = &_data; // To avoid restrictions on T const &_data.T const *leftMax = (_left == nullptr) ? dataTemp : &_left->max();T const *rightMax = (_right == nullptr) ? dataTemp : &_right->max();return (*leftMax > *rightMax)

? ((*leftMax > *dataTemp) ? *leftMax : *dataTemp): ((*rightMax > *dataTemp) ? *rightMax : *dataTemp);

}

Figure 8.11 Implementation of the max() operation for the BiTreeL data structure.

about the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Construction and insertionHello, here is some text without a meaning. This text should show what a printed textwill look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the original

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 26: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures

Methods for construction and

insertion

BiTreeL

Page 27: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.12

8.1 The Classic Linked Implementation 273

BiTreeL(BiTreeL<T> const &rhs);// Copy constructor disabled. Private.

BiTreeL();// Post: This tree is initialized to be empty.

void insertRoot(T const &data);// Pre: This tree is empty.// Post: This tree has one root node containing data.

void setRoot(T const &data);// Pre: This tree is not empty.// Post: The root element of this tree is changed to data.

void setLeft(BiTreeL<T> &subTree);// Pre: This tree is not empty.// Post: The left child of this tree is subTree.// The old left child of this tree is deallocated.// subTree is the empty tree (cut setLeft, as opposed to copy setLeft).

void setRight(BiTreeL<T> &subTree);// Pre: This tree is not empty.// Post: The right child of this tree is subTree.// The old right child of this tree is deallocated.// subTree is the empty tree (cut setRight, as opposed to copy setRight).

BiTreeL & operator=(BiTreeL<T> const &rhs);// Post: A deep copy of rhs is returned with garbage collection.

LNode<T> *copyRoot(BiTreeL<T> const &rhs);// Post: A deep copy of the root of rhs is returned.

Figure 8.12 Specification for the construction and insertion operations for theBiTreeL data structure.

language. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printed

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

Page 28: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.12

8.1 The Classic Linked Implementation 273

BiTreeL(BiTreeL<T> const &rhs);// Copy constructor disabled. Private.

BiTreeL();// Post: This tree is initialized to be empty.

void insertRoot(T const &data);// Pre: This tree is empty.// Post: This tree has one root node containing data.

void setRoot(T const &data);// Pre: This tree is not empty.// Post: The root element of this tree is changed to data.

void setLeft(BiTreeL<T> &subTree);// Pre: This tree is not empty.// Post: The left child of this tree is subTree.// The old left child of this tree is deallocated.// subTree is the empty tree (cut setLeft, as opposed to copy setLeft).

void setRight(BiTreeL<T> &subTree);// Pre: This tree is not empty.// Post: The right child of this tree is subTree.// The old right child of this tree is deallocated.// subTree is the empty tree (cut setRight, as opposed to copy setRight).

BiTreeL & operator=(BiTreeL<T> const &rhs);// Post: A deep copy of rhs is returned with garbage collection.

LNode<T> *copyRoot(BiTreeL<T> const &rhs);// Post: A deep copy of the root of rhs is returned.

Figure 8.12 Specification for the construction and insertion operations for theBiTreeL data structure.

language. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printed

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

Page 29: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.12

8.1 The Classic Linked Implementation 273

BiTreeL(BiTreeL<T> const &rhs);// Copy constructor disabled. Private.

BiTreeL();// Post: This tree is initialized to be empty.

void insertRoot(T const &data);// Pre: This tree is empty.// Post: This tree has one root node containing data.

void setRoot(T const &data);// Pre: This tree is not empty.// Post: The root element of this tree is changed to data.

void setLeft(BiTreeL<T> &subTree);// Pre: This tree is not empty.// Post: The left child of this tree is subTree.// The old left child of this tree is deallocated.// subTree is the empty tree (cut setLeft, as opposed to copy setLeft).

void setRight(BiTreeL<T> &subTree);// Pre: This tree is not empty.// Post: The right child of this tree is subTree.// The old right child of this tree is deallocated.// subTree is the empty tree (cut setRight, as opposed to copy setRight).

BiTreeL & operator=(BiTreeL<T> const &rhs);// Post: A deep copy of rhs is returned with garbage collection.

LNode<T> *copyRoot(BiTreeL<T> const &rhs);// Post: A deep copy of the root of rhs is returned.

Figure 8.12 Specification for the construction and insertion operations for theBiTreeL data structure.

language. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printed

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

Page 30: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.13274 Chapter 8 Binary Trees

// ========= Constructors =========template<class T>BiTreeL<T>::BiTreeL() :

_root(nullptr) {}

template<class T>LNode<T>::LNode(T data) :

_left(nullptr),_data(data),_right(nullptr) {

}

p

(a) Initial pointer.

p

(b) Allocate storage from the heap.

p

(c) Execute the constructor.

p

(d) Return the pointer and assign to p.

70

70

Figure 8.13 The constructors for the BiTreeL data structure and the LNode class.The snapshots show the execution of p = new LNode<T>(70).

text will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

p = new LNode<T>(70)

Page 31: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.13274 Chapter 8 Binary Trees

// ========= Constructors =========template<class T>BiTreeL<T>::BiTreeL() :

_root(nullptr) {}

template<class T>LNode<T>::LNode(T data) :

_left(nullptr),_data(data),_right(nullptr) {

}

p

(a) Initial pointer.

p

(b) Allocate storage from the heap.

p

(c) Execute the constructor.

p

(d) Return the pointer and assign to p.

70

70

Figure 8.13 The constructors for the BiTreeL data structure and the LNode class.The snapshots show the execution of p = new LNode<T>(70).

text will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

p = new LNode<T>(70)

Page 32: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.14

8.1 The Classic Linked Implementation 275

// ========= insertRoot =========template<class T>void BiTreeL<T>::insertRoot(T const &data) {

if (_root != nullptr) {cerr << ”insertRoot precondition violated:”

<< ”Cannot insert root into a non empty tree”<< endl;

throw -1;}_root = new LNode<T>(data);

} 20

myTree

myTree

(a) Original tree.

(b) After inserting root.

Figure 8.14 The implementation of insertRoot() for the BiTreeL data struc-ture. The snapshots show the execution of myTree.insertRoot(20).

Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the original

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

myTree.insertRoot(20)

Page 33: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.14

8.1 The Classic Linked Implementation 275

// ========= insertRoot =========template<class T>void BiTreeL<T>::insertRoot(T const &data) {

if (_root != nullptr) {cerr << ”insertRoot precondition violated:”

<< ”Cannot insert root into a non empty tree”<< endl;

throw -1;}_root = new LNode<T>(data);

} 20

myTree

myTree

(a) Original tree.

(b) After inserting root.

Figure 8.14 The implementation of insertRoot() for the BiTreeL data struc-ture. The snapshots show the execution of myTree.insertRoot(20).

Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the original

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

myTree.insertRoot(20)

Page 34: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.15

myTree.setRoot(10)

276 Chapter 8 Binary Trees

(a) Original tree. (b) After setting root.

5070

80

myTree

5070

myTree

10

Figure 8.15 Action of myTree.setRoot(10) for the BiTreeL data structure.

language. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match the

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 35: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.168.1 The Classic Linked Implementation 277

// ========= setRight =========template<class T>void BiTreeL<T>::setRight(BiTreeL<T> &subTree) {

if (_root == nullptr) {cerr << ”Precondition violated:”

<< ”Cannot set right on an empty tree.”<< endl;

throw -1;}_root->setRight(subTree);

}

template<class T>void LNode<T>::setRight(BiTreeL<T> &subTree) {

if (_right != nullptr) {_right->clear();

}_right = subTree._root;subTree._root = nullptr;

}

Figure 8.16 Implementation of the setRight() operation for the BiTreeL datastructure.

language.Hello, here is some text without a meaning. This text should show what a printed

text will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. This

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

Page 36: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.17

278 Chapter 8 Binary Trees

4020

60

90 10

(a) Initial trees. myTree

5070

80

yourTree

20

60

90

(b) right->clear(); myTree

5070

80

yourTree

Figure 8.17 A trace of myTree.setRight(yourTree) for the BiTreeL datastructure. The trace continues in the next figure.

text should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the original

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

myTree.setRight(yourTree)

Page 37: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.17

_right->clear()

278 Chapter 8 Binary Trees

4020

60

90 10

(a) Initial trees. myTree

5070

80

yourTree

20

60

90

(b) right->clear(); myTree

5070

80

yourTree

Figure 8.17 A trace of myTree.setRight(yourTree) for the BiTreeL datastructure. The trace continues in the next figure.

text should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the original

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 38: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.18

_right = subtree._root

8.1 The Classic Linked Implementation 279

20

60

90

(c) _right = subTree._root; myTree

5070

80

yourTree

20

60

90

(d) subTree._root = nullptr; myTree

5070

80

yourTree

Figure 8.18 (continued) A trace of myTree.setRight(yourTree) for theBiTreeL data structure.

language. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match the

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

Page 39: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.18

subtree._root = nullptr

8.1 The Classic Linked Implementation 279

20

60

90

(c) _right = subTree._root; myTree

5070

80

yourTree

20

60

90

(d) subTree._root = nullptr; myTree

5070

80

yourTree

Figure 8.18 (continued) A trace of myTree.setRight(yourTree) for theBiTreeL data structure.

language. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match the

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

Page 40: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.19

280 Chapter 8 Binary Trees

// ========= operator= =========template<class T>BiTreeL<T> &BiTreeL<T>::operator=(BiTreeL<T> const &rhs) {

if (this != &rhs) { // In case someone writes myTree = myTree;delete _root;_root = copyRoot(rhs);

}return *this;

}

// ========= copyRoot =========template<class T>LNode<T> *BiTreeL<T>::copyRoot(BiTreeL<T> const &rhs) {

return rhs.isEmpty() ? nullptr : rhs._root->copyRoot();}

template<class T>LNode<T> *LNode<T>::copyRoot() {

LNode<T> *result = new LNode<T > (_data);if (_left != nullptr) {

result->_left = _left->copyRoot();}if (_right != nullptr) {

result->_right = _right->copyRoot();}return result;

}

Figure 8.19 Implementation of the assignment operator and copyRoot() for theBiTreeL data structure.

language.Hello, here is some text without a meaning. This text should show what a printed

text will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the original

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 41: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.19

280 Chapter 8 Binary Trees

// ========= operator= =========template<class T>BiTreeL<T> &BiTreeL<T>::operator=(BiTreeL<T> const &rhs) {

if (this != &rhs) { // In case someone writes myTree = myTree;delete _root;_root = copyRoot(rhs);

}return *this;

}

// ========= copyRoot =========template<class T>LNode<T> *BiTreeL<T>::copyRoot(BiTreeL<T> const &rhs) {

return rhs.isEmpty() ? nullptr : rhs._root->copyRoot();}

template<class T>LNode<T> *LNode<T>::copyRoot() {

LNode<T> *result = new LNode<T > (_data);if (_left != nullptr) {

result->_left = _left->copyRoot();}if (_right != nullptr) {

result->_right = _right->copyRoot();}return result;

}

Figure 8.19 Implementation of the assignment operator and copyRoot() for theBiTreeL data structure.

language.Hello, here is some text without a meaning. This text should show what a printed

text will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the original

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 42: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures

Methods for destruction and removal

BiTreeL

Page 43: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.208.1 The Classic Linked Implementation 281

~BiTreeL();// Post: This tree is deallocated.

void clear();// Post: This tree is cleared to the empty tree.

T remRoot();// Pre: This tree is not empty and its root// has at least one empty child.// Post: The root node is removed from this tree// and its element is returned.

void remLeaves();// Post: The leaves are removed from this tree.

Figure 8.20 Specification for the destruction and removal operations for theBiTreeL data structure.

language. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Destruction and removalHello, here is some text without a meaning. This text should show what a printed textwill look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match the

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

Page 44: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.21282 Chapter 8 Binary Trees

/// ========= Destructors =========template<class T>BiTreeL<T>::~BiTreeL() {

clear();}

// ========= clear =========template<class T>void BiTreeL<T>::clear() {

cerr << ”BiTreeL<T>::clear: Exercise for the student.” << endl;throw -1;

}

template<class T>void LNode<T>::clear() {

cerr << ”LNode<T>::clear: Exercise for the student.” << endl;throw -1;

}

60

myTree

(a) Original.

60

myTree

(b) Clear left.

60

myTree

(c) Set to nil.

60

myTree

(d) Clear right.

60

myTree

(e) Set to nil.

myTree

(f) Delete node.

myTree

(g) Set to nil.

Figure 8.21 The destructors for the BiTreeL data structure and the LNode class.The snapshots show the execution of delete myTree.

language.Hello, here is some text without a meaning. This text should show what a printed

text will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 45: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.21

delete myTree

282 Chapter 8 Binary Trees

/// ========= Destructors =========template<class T>BiTreeL<T>::~BiTreeL() {

clear();}

// ========= clear =========template<class T>void BiTreeL<T>::clear() {

cerr << ”BiTreeL<T>::clear: Exercise for the student.” << endl;throw -1;

}

template<class T>void LNode<T>::clear() {

cerr << ”LNode<T>::clear: Exercise for the student.” << endl;throw -1;

}

60

myTree

(a) Original.

60

myTree

(b) Clear left.

60

myTree

(c) Set to nil.

60

myTree

(d) Clear right.

60

myTree

(e) Set to nil.

myTree

(f) Delete node.

myTree

(g) Set to nil.

Figure 8.21 The destructors for the BiTreeL data structure and the LNode class.The snapshots show the execution of delete myTree.

language.Hello, here is some text without a meaning. This text should show what a printed

text will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

282 Chapter 8 Binary Trees

/// ========= Destructors =========template<class T>BiTreeL<T>::~BiTreeL() {

clear();}

// ========= clear =========template<class T>void BiTreeL<T>::clear() {

cerr << ”BiTreeL<T>::clear: Exercise for the student.” << endl;throw -1;

}

template<class T>void LNode<T>::clear() {

cerr << ”LNode<T>::clear: Exercise for the student.” << endl;throw -1;

}

60

myTree

(a) Original.

60

myTree

(b) Clear left.

60

myTree

(c) Set to nil.

60

myTree

(d) Clear right.

60

myTree

(e) Set to nil.

myTree

(f) Delete node.

myTree

(g) Set to nil.

Figure 8.21 The destructors for the BiTreeL data structure and the LNode class.The snapshots show the execution of delete myTree.

language.Hello, here is some text without a meaning. This text should show what a printed

text will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016

Page 46: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.22

remRoot()

8.1 The Classic Linked Implementation 283

30

70

90 15

40

30

80

20

60

50

75

70

90 15

40

80

20

60

50

75

(a) Original tree. (b) After remRoot(). (c) Original tree. (d) After remRoot().

Figure 8.22 Action of the remRoot() operation for the BiTreeL data structure.

about the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printed

Revised: September 22, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford

Page 47: Binary Trees - Pepperdine University · The formal definition of a binary tree is recursive. • An empty tree is a binary tree. • Anonempty binary tree has three parts: – a

Design Patterns for Data Structures Figure 8.23

remLeaves()

284 Chapter 8 Binary Trees

30

70 80

20

90 60

50

15

40 75

30

70 80

20

6015

(a) Original tree. (b) After remLeaves().

Figure 8.23 Action of the remLeaves() operation for the BiTreeL data structure.

text will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.

Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the original

Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: September 22, 2016