datastruct 7. treeselearning.kocw.net/kocw/document/2013/youngnam/youngtak... · 2016-09-09 · the...

53
DataStruct 7. Trees 2013-2 O-O Programming & Data Structure November 6, 2013 Prof. Young-Tak Kim Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, College of Engineering, Yeungnam University, KOREA (Tel : +82-53-810-2497; Fax : +82-53-810-4742 http://antl.yu.ac.kr/; E-mail : [email protected]) Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011.

Upload: others

Post on 16-Mar-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

DataStruct 7. Trees

2013-2 O-O Programming & Data Structure

November 6, 2013

Prof. Young-Tak Kim Advanced Networking Technology Lab. (YU-ANTL)

Dept. of Information & Comm. Eng, College of Engineering, Yeungnam University, KOREA

(Tel : +82-53-810-2497; Fax : +82-53-810-4742 http://antl.yu.ac.kr/; E-mail : [email protected])

Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2nd Ed., John Wiley & Sons, Inc., 2011.

Page 2: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 2

Outline

General Trees Tree Traversal Algorithms Binary Trees

Page 3: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 3

7.1 General Tree

In computer science, a tree is an abstract model of a hierarchical structure

A tree consists of nodes with a parent-child relation

Applications: Organization charts File systems Programming environments

Computers”R”Us

Sales R&D Manufacturing

Laptops Desktops US International

Europe Asia Canada

Page 4: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 4

Formal Tree Definitions (1)

Root: node without parent (A)

Internal node: node with at least one child (A, B, C, F)

External node (a.k.a. leaf ): node without children (E, I, J, K, G, H, D)

Ancestors of a node: parent, grandparent, grand-grandparent, etc.

Depth of a node: number of ancestors

Height of a tree: maximum depth of any node (3)

Descendant of a node: child, grandchild, grand-grandchild, etc.

Subtree: tree consisting of a node and its descendants

subtree

A

B D C

G H E F

I J K

Page 5: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 5

Formal Tree Definitions (2)

parent, child, subtree root parent child sibling: nodes that are children of the same parent external node: leaf node that has no children internal node: node that has one or more children

ancestor descendent

subtree

Page 6: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 6

Formal Tree Definitions (3)

Edges and paths edge of tree T is a pair of nodes (u, v) such that u is the parent

of v, or vice versa path of T is a sequence of nodes such that any two consecutive

nodes in the sequence form an edge

Ordered Tree a tree is ordered if there is a linear ordering defined for the

children of each node e.g.) structured document

Page 7: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 7

Tree Functions

We use positions to abstract nodes Generic methods: integer size() // returns the number of nodes in the tree boolean empty() // return true if the tree is empty

Accessor methods: position root() // return a position for the tree’s root list<position> positions()

// return a position list of all the nodes of the tree Position-based methods: position p.parent() // return the parent of p list<position> p.children() // return a position list containing the children of p

Query methods: boolean p.isRoot() boolean p.isExternal()

Additional update methods may be defined by data structures implementing the Tree ADT

Page 8: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 8

C++ Tree Interface

Informal interface for a position in a tree

Informal interface for the tree ADT

template <typename E> // base element type class Position<E> { // a node position public:

E& operator*(); // get element Position parent() const; // get parent PositionList children() const; // get node's children bool isRoot() const; // root node? bool isExternal() const; // external node?

};

template <typename E> // base element type class Tree<E> { public: // public types

class Position; // a node position class PositionList; // a list of positions of children

public: // public functions int size() const; // number of nodes bool empty() const; // is tree empty? Position root() const; // get the root PositionList positions() const; // get positions of all nodes

};

Page 9: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 9

Linked Structure for General Trees

linked structure of general tree

element

children container

parent

(a) Node Structure (b) Portion of data structure associated with a node and its children

Page 10: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 10

7.2 Tree Traversal Algorithms

Depth p : a node of a tree T depth of p is the number of ancestors of p

int depth(const Tree& T, const Position& p) { if (p.isRoot())

return 0; // root has depth 0 else

return (1 + depth(T, p.parent())); // 1 + (depth of parent) }

Page 11: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 11

Height if p is external, then the height of p is 0 height of a node p in a tree T is one plus the maximum height of a child

of p height of a tree T is the height of the root of T

int height2(const Tree& T, const Position& p) { if (p.isExternal())

return 0; // leaf has height 0 int h = 0; PositionList ch = p.children(); // list of children for (Iterator q = ch.begin(); q != ch.end(); ++q)

h = max(h, height2(T, *q)); return 1 + h; // 1 + max height of children

}

Page 12: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 12

Preorder Traversal

A traversal visits the nodes of a tree in a systematic manner

In a preorder traversal, a node is visited before its descendants

Application: print a structured document

Algorithm preOrder(v) visit(v) for each child w of v preorder (w)

Page 13: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 13

preorderPrint

void preorderPrint(const Tree& T, const Position& p) { cout << *p; // print element PositionList ch = p.children(); // list of children for (Iterator q = ch.begin(); q != ch.end(); ++q) {

cout << " "; preorderPrint(T, *q);

} }

Page 14: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 14

Postorder Traversal

In a postorder traversal, a node is visited after its descendants

Application: compute space used by files in a directory and its subdirectories

Algorithm postOrder(v) for each child w of v postOrder (w) visit(v)

Page 15: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 15

postOrderPrint

void postorderPrint(const Tree& T, const Position& p) { PositionList ch = p.children(); // list of children for (Iterator q = ch.begin(); q != ch.end(); ++q) {

postorderPrint(T, *q); cout << " ";

} cout << *p; // print element

}

Page 16: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 16

7.3 Binary Trees

A binary tree is a tree with the following properties: Each internal node has at most

two children (exactly two for proper binary trees)

The children of a node are an ordered pair

We call the children of an internal node left child and right child

Alternative recursive definition: a binary tree is either a tree consisting of a single

node, or a tree whose root has an

ordered pair of children, each of which is a binary tree

Applications: arithmetic expressions decision processes searching

A

B C

F G D E

H I

Page 17: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 17

Arithmetic Expression Tree

Binary tree associated with an arithmetic expression internal nodes: operators external nodes: operands

Example: arithmetic expression tree for the expression (2 × (a − 1) + (3 × b))

+

× ×

− 2

a 1

3 b

Page 18: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 18

Decision Tree

Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions

Example: dining decision

Want a fast meal?

How about coffee? On expense account?

Starbucks Spike’s Al Forno Café Paragon

Yes No

Yes No Yes No

Page 19: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 19

BinaryTree ADT

The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT

Additional methods: position p.left() position p.right()

Update methods may be defined by data structures implementing the BinaryTree ADT

Proper binary tree: Each node has either 0 or 2 children

Page 20: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 20

Traversals of a Binary Tree

Preorder Traversal

Post-order Traversal

In-order Traversal Algorithm binaryPreorder(T, p)

perform the “visit” action for node p if p is an internal node then binaryPreorder(T, p.left()) binaryPreorder(T, p.right())

Algorithm binaryPostorder(T, p) if p is an internal node then binaryPostorder(T, p.left()) binaryPostorder(T, p.right()) perform the “visit” action for node p

Algorithm binaryInorder(T, p) if p is an internal node then binaryInorder(T, p.left()) perform the “visit” action for node p if p is an internal node then binaryInorder(T, p.right())

Page 21: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 21

Inorder Traversal

In an inorder traversal a node is visited after its left subtree and before its right subtree

Application: draw a binary tree x(v) = in-order rank of v y(v) = depth of v

Algorithm inOrder(v)

if ¬ v.isExternal() inOrder(v.left())

visit(v) if ¬ v.isExternal()

inOrder(v.right())

3

1

2

5

6

7 9

8

4

Page 22: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 22

Print Arithmetic Expressions

Specialization of an inorder traversal print operand or operator when

visiting node print “(“ before traversing left

subtree print “)“ after traversing right

subtree

Algorithm printExpression(v)

if ¬v.isExternal() print(“(’’)

inOrder(v.left()) print(v.element()) if ¬v.isExternal()

inOrder(v.right()) print (“)’’)

+

× ×

− 2

a 1

3 b ((2 × (a − 1)) + (3 × b))

Page 23: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 23

Evaluate Arithmetic Expressions

Specialization of a postorder traversal recursive method returning

the value of a subtree when visiting an internal node,

combine the values of the subtrees

Algorithm evalExpr(v)

if v.isExternal() return v.element()

else x ← evalExpr(v.left()) y ← evalExpr(v.right()) ◊ ← operator stored at v

return x ◊ y +

× ×

− 2

5 1

3 2

Page 24: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 24

Euler Tour Traversal

Generic traversal of a binary tree

Includes a special cases the preorder, postorder and inorder traversals

Walk around the tree and visit each node three times: on the left (preorder) from below (inorder) on the right (postorder)

+

×

− 2

5 1

3 2

L B

R ×

Page 25: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 25

C++ Binary Tree Interface

Class Position

template <typename E> // base element type class Position<E> { // a node position public:

E& operator*(); // get element Position left() const; // get left child Position right() const; // get right child Position parent() const; // get parent bool isRoot() const; // root of tree? bool isExternal() const; // an external node?

};

Page 26: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 26

Class BinaryTree template <typename E> // base element type

class BinaryTree<E> { // binary tree public: // public types

class Position; // a node position class PositionList; // a list of positions

public: // member functions int size() const; // number of nodes bool empty() const; // is tree empty? Position root() const; // get the root PositionList positions() const; // list of nodes

};

Page 27: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 27

Properties of Proper Binary Trees

Notation n number of nodes e number of external nodes i number of internal nodes h height

Properties: e = i + 1 n = 2e − 1 h ≤ i h ≤ (n − 1)/2 e ≤ 2h

h ≥ log2 e h ≥ log2 (n + 1) − 1

Page 28: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 28

Maximum Number of Nodes in the levels of a Binary Tree

Page 29: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 29

Linked Structure for Binary Trees A node is represented by an object storing

Element Parent node Left child node Right child node

Node objects implement the Position ADT

B

D A

C E ∅ ∅

∅ ∅ ∅ ∅

B

A D

C E

Page 30: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 30

Linked Binary Tree (1)

/** LinkedBinaryTree.h */ #ifndef LINKEDBINARYTREE_H #define LINKEDBINARYTREE_H #include <iostream> #include <list> using namespace std; typedef int Elem; // base element type class LinkedBinaryTree { protected: // TreeNode declaration struct TreeNode { // a node of the tree Elem elt; // element value TreeNode* parent; // parent TreeNode* left; // left child TreeNode* right; // right child TreeNode() : elt(), parent(NULL), left(NULL), right(NULL) { } // constructor TreeNode(Elem e) : elt(e), parent(NULL), left(NULL), right(NULL) { } // constructor };

Page 31: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 31

Linked Binary Tree (2)

/** LinkedBinaryTree.h (2) */ public: // Position declaration class Position { // position in the tree private: TreeNode* v; // pointer to the TreeNode public: Position(TreeNode* _v = NULL) : v(_v) { } // constructor Elem& operator*() { return v->elt; } // get element Position left() const { return Position(v->left); } // get left child Position right() const { return Position(v->right); } // get right child Position parent() const { return Position(v->parent); } // get parent bool isNULL() { return (v == NULL); } // check if position is null bool isRoot() const { return v->parent == NULL; } // root of the tree? bool isExternal() const { return v->left == NULL && v->right == NULL; } // an external TreeNode? friend class LinkedBinaryTree; // give tree access }; typedef std::list<Position> PositionList; // list of positions

Page 32: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 32

Linked Binary Tree (3)

/** LinkedBinaryTree.h (2) */ public: LinkedBinaryTree(); // constructor int size() const; // number of TreeNodes bool empty() const; // is tree empty? Position root() const; // get the root PositionList positions() const; // list of TreeNodes void addRoot(); // add root to empty tree void expandExternal(const Position& p); // expand external TreeNode Position removeAboveExternal(const Position& p); // remove p and parent Position addElementInOrder(Position& p, Position& paren, const Elem& element); void printTreeInOrder(Position p); void printTreeByLevel(Position p, int level); // housekeeping functions omitted... protected: // local utilities void preorder(TreeNode* v, PositionList& pl) const; // preorder utility private: TreeNode* _root; // pointer to the root int n; // number of TreeNodes }; #endif

Page 33: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 33

Linked Binary Tree (4) /** LinkedBinaryTree.cpp (1) */

#include "LinkedBinaryTree.h" LinkedBinaryTree::LinkedBinaryTree() // constructor : _root(NULL), n(0) { } int LinkedBinaryTree::size() const // number of nodes { return n; } bool LinkedBinaryTree::empty() const // is tree empty? { return size() == 0; } LinkedBinaryTree::Position LinkedBinaryTree::root() const // get the root { return Position(_root); } void LinkedBinaryTree::addRoot() // add root to empty tree { _root = new TreeNode; n = 1; }

Page 34: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 34

Linked Binary Tree (5) /** LinkedBinaryTree.cpp (2) */

// expand external node void LinkedBinaryTree::expandExternal(const Position& p) { TreeNode* v = p.v; // p's node v->left = new TreeNode; // add a new left child v->left->parent = v; // v is its parent v->right = new TreeNode; // and a new right child v->right->parent = v; // v is its parent n += 2; // two more nodes } LinkedBinaryTree::Position // remove p and parent LinkedBinaryTree::removeAboveExternal(const Position& p) { TreeNode* w = p.v; TreeNode* v = w->parent; // get p's node and parent TreeNode* sib = (w == v->left ? v->right : v->left); if (v == _root) { // child of root? _root = sib; // ...make sibling root sib->parent = NULL; } else { TreeNode* gpar = v->parent; // w's grandparent if (v == gpar->left) gpar->left = sib; // replace parent by sib else gpar->right = sib; sib->parent = gpar; } delete w; delete v; // delete removed nodes n -= 2; // two fewer nodes return Position(sib); }

Page 35: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 35

Binary Tree Update Functions expandExternal(p) removeAboveExternal(p)

Page 36: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 36

Linked Binary Tree (6) /** LinkedBinaryTree.cpp (3) */

// list of all nodes LinkedBinaryTree::PositionList LinkedBinaryTree::positions() const { PositionList pl; preorder(_root, pl); // preorder traversal return PositionList(pl); // return resulting list } // preorder traversal void LinkedBinaryTree::preorder(TreeNode* v, PositionList& pl) const { pl.push_back(Position(v)); // add this node if (v->left != NULL) // traverse left subtree preorder(v->left, pl); if (v->right != NULL) // traverse right subtree preorder(v->right, pl); }

Page 37: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 37

Linked Binary Tree (7) /** LinkedBinaryTree.cpp (4) */

LinkedBinaryTree::Position LinkedBinaryTree::addElementInOrder(Position& p, Position& paren, const Elem& element) { LinkedBinaryTree::Position newPos; if (p.isNULL()) { p.v = new TreeNode(element); if (paren.v == NULL) { _root = p.v; } p.v->parent = paren.v; n++; // increment the number of elements return LinkedBinaryTree::Position(p.v); } else if (element < *p) { newPos = addElementInOrder(p.left(), p, element); p.v->left = newPos.v; } else if (element > *p) { newPos = addElementInOrder(p.right(), p, element); p.v->right = newPos.v; } else { cout << "Duplicated element in addElementInOrder !!" << endl; } }

Page 38: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 38

Linked Binary Tree (8) /** LinkedBinaryTree.cpp (5) */

void LinkedBinaryTree::printTreeInOrder(Position p) { if (p.isNULL()) return; printTreeInOrder(p.left()); cout << *p <<" "; printTreeInOrder(p.right()); } void LinkedBinaryTree::printTreeByLevel(Position p, int level) { LinkedBinaryTree::TreeNode* pChild = NULL; if (p.v != NULL) { if (level == 0) cout << "\ nRoot (data: "; cout << *p << ")" << endl; pChild = (p.left()).v; for (int i=0; i<level; i++) cout << " ";

Page 39: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 39

Linked Binary Tree (9) /** LinkedBinaryTree.cpp (6) */

if (pChild != NULL) { cout << "L (data: "; printTreeByLevel(pChild, level+1); } else { cout << "L (NULL)" << endl; } pChild = (p.right()).v; for (int i=0; i<level; i++) cout << " "; if (pChild != NULL) { cout << "R (data: "; printTreeByLevel(pChild, level+1); } else { cout << "R (NULL)" << endl; } } }

Page 40: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 40

Linked Binary Tree (10) /** main.cpp */

#include <iostream> #include "LinkedBinaryTree.h" void main() { LinkedBinaryTree* pLBTree; int data; pLBTree = new LinkedBinaryTree; cout << endl; for (int i=0; i<10; i++) { data = rand() % 100; cout << "Adding " << data << " into LinkedBinaryTree" << endl; pLBTree->addElementInOrder(pLBTree->root(), LinkedBinaryTree::Position(NULL), data); } cout << "\ nElements in LinkedBinaryTree, in order : " << endl; pLBTree->printTreeInOrder(pLBTree->root()); cout << endl; pLBTree->printTreeByLevel(pLBTree->root(), 0); // start from level 0 cout << endl; delete pLBTree; }

Page 41: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 41

Execution Results

(case 1) (case 2)

Page 42: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 42

Tri-node Restructuring

height height of leaf node : 1 height of inner node: 1 + MAX (height of left child, height of right child)

height difference height difference = height of left child – height of right child

tri-node restructuring re-arrange three node so that the re-arranged subtree has height difference less

than or equal to 1

h 1 diff 0

h 2 diff 1 h 1

diff 0

h 3 diff 1

h 4 diff 3

(Example 1)

h 1 diff 0

h 2 diff -1 h 1

diff 0

h 3 diff -1

h 4 diff -3

(Example 2)

Page 43: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 43

Tri-node Restructuring

Single rotation LL of subtree

C

B

A

h 0 h 0

h 1 diff 0

h 0

h 0 h 2 diff 1

h 3 diff 2

B

A C

h 0 h 0

h 1 diff 0

h 2 diff 0

h 0 h 0

h 1 diff 0

rotate_LL()

Page 44: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 44

Example of Tri-node Restructuring

Single rotation RR of subtree

A

B

C

h 0 h 0

h 1 diff 0

ht 0

h 0 h 2 diff 1

h 3 diff 2

B

C A

h 0 h 0

h 1 diff 0

h 2 diff 0

h 0 h 0

h 1 diff 0

rotate_RR()

Page 45: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 45

Example of Tri-node Restructuring

Double rotation LR of subtree

B

A C

h 0 h 0

h 1 diff 0

h 2 diff 0

h 0 h 0

h 1 diff 0

C

A

B

h 0 h 0

h 1 diff 0

h 0

h 0 h 2 diff -1

h 3 diff 2

rotate_LR()

Page 46: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 46

Example of Tri-node Restructuring

Double rotation RL of subtree

B

A C

h 0 h 0

h 1 diff 0

h 2 diff 0

h 0 h 0

h 1 diff 0

A

C

B

h 0 h 0

h 1 diff 0

h 0

h 0 h 2 diff 1

h 3 diff -2

rotate_RL()

Page 47: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 47

Algorithm getHeight()

Algorithm getHeightDiff()

Algorithm getHeight(TreeNode* pTN) height = 0; if (pTN != NULL) height = 1 + max (getHeight(pTN->left()), getHeight(pTN->right()); return height; end

Algorithm getHeightDiff(TreeNode* pTN) heightDiff = 0; if (pTN == NULL) return 0; heightDiff = getHeight(pTN->left() - getHeight(pTN->right(); return heightDiff; end

Page 48: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 48

Algorithm reBalance()

Algorithm reBalance(TreeNode** ppTN) heightDiff = getheightDiff(*ppTN); if (heightDiff > 1) // L child is higher than R child if (getHeightDiff(*ppTN->left() > 0) *ppTN = rotate_LL(*ppTN); // L.L child is higher than L.R child else *ppTN = rotate_LR(*ppTN); // L.R child is higher than L.L child else if (heightDiff < -1) // R child is higher than L child if (getHeightDiff(*ppTN->right() < 0) *ppTN = rotate_RR(*ppTN); // R.R child is higher than R.L child else *ppTN = rotate_RL(*ppTN); // R.L child is higher than R.R child return *ppTN; end

Page 49: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 49

Algorithm rotate_LL()

Algorithm rotate_LL(TreeNode* pParent) TreeNode* pChild; pChild = pParent->left(); pParent->setLeft(pChild->right()); pChild->setRight(pParent); return pChild; end

rotate_LL()

D

B

A

h 0 h 0

h 1 diff 0

h 0 h 2

diff 0

h 3 diff 2

C

h 0 h 0

h 1 diff 0

B

A

h 0 h 0

h 1 diff 0

h 3 diff 1

D

h 0

h diff 1

C

h 0 h 0

h 1 diff 0

Page 50: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 50

Algorithm rotate_RR()

Algorithm rotate_RR(TreeNode* pParent) TreeNode* pChild; pChild = pParent->right(); pParent->setRight(pChild->left()); pChild->setLeft(pParent); return pChild; end

rotate_RR()

A

C

D

h 0 h 0

h 1 diff 0

h 0 h 2

diff 0

h 3 diff -2

B

h 0 h 0

h 1 diff 0

C

D

h 0 h 0

h 1 diff 0

h 3 diff 1

A

h 0

h diff 1

B

h 0 h 0

h 1 diff 0

Page 51: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 51

Algorithm rotate_LR(), rotate_RL()

Algorithm rotate_LR(TreeNode* pParent) TreeNode* pChild; pChild = pParent->left(); pParent->setLeft(rotate_RR(pChild)); return rotate_LL(pParent); end

D

B

C

h 0 h 0

h 1 diff 0

h 0 h 2 diff 0

h 3 diff 2

A

h 0 h 0

h 1 diff 0

setLeft(rotate_RR())

D

C

h 0

h 1 diff 2

h 0

h 3 diff 3

B h 2

diff 1

A

h 0 h 0

h 1 diff 0

h 0

C

D

h 3 diff 1

h 0 h 0

h 1 diff 0

rotate_LR() B

h 2 diff 1

A

h 0 h 0

h 1 diff 0

h 0

Page 52: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 52

Algorithm rotate_LR(), rotate_RL()

Algorithm rotate_RL(TreeNode* pParent) TreeNode* pChild; pChild = pParent->right(); pParent->setRight(rotate_LL(pChild)); return rotate_RR(pParent); end

A

C

B

h 0 h 0

h 1 diff 0

h 0 h 2 diff 0

h 3 diff -2

D

h 0 h 0

h 1 diff 0

setRight(rotate_CC( C ))

A

B

h 0

h 1 diff -2

h 0

h 3 diff -3

C h 2

diff -1

D

h 0 h 0

h 1 diff 0

h 0

rotate_RR( B )

B

A

h 3 diff -1

h 0 h 0

h 1 diff 0

C h 2

diff -1

D

h 0 h 0

h 1 diff 0

h 0

Page 53: DataStruct 7. Treeselearning.kocw.net/KOCW/document/2013/youngnam/YoungTak... · 2016-09-09 · The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree

Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)

O-O Programming & Data Structure Prof. Young-Tak Kim DS 7 - 53

DS-7.1 Projects P-7.2 (LinedBinaryTree using class template)

DS-7.2 Projects P-7.11 (game tree for Tic-Tac-Toe)

Homework DS-7