970 likes | 1.3k Views
M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees. 2. Topics. TerminologyADT Binary Tree (BT)OperationsImplementations Tree TraversalADT Binary-Search Tree (BST)OperationsImplementationsEfficiencyTree SortGeneral Tre
E N D
1. Chapter 10
Trees
2. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 2 Topics Terminology
ADT Binary Tree (BT)
Operations
Implementations
Tree Traversal
ADT Binary-Search Tree (BST)
Operations
Implementations
Efficiency
Tree Sort
General Trees
3. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 3 Introduction & Terminology Review
Position-oriented ADT:
Insert, delete, or ask questions about data items at specific position
Examples: Stacks, Queues
Value-oriented ADT:
Insert, delete, or ask questions about data items containing a specific value
Examples: Sorted lists
4. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 4 Introduction & Terminology Hierarchical (Tree) structure:
Parent-child relationship between elements of the structure
Nonlinear
One-to-many relationships among the elements
Examples:
Organization chart
Family tree
Library card-catalog
Expression tree
5. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 5 Introduction & Terminology Example: Library card catalog
6. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 6 Introduction & Terminology Example: Expression trees
7. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 7 Introduction & Terminology Trees:
Computer representation of hierarchical structures
Flexible and rapid data access
Can contain any type of information
Components:
Nodes ( vertices )
Edges ( arcs )
8. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 8 Introduction & Terminology General tree
A general tree T is a set of one or more nodes such that T is partitioned into disjoint subsets:
A single node r, the root
Sets that are general trees, called subtrees of r
Subtree of node n
A tree that consists of a child (if any) of n and the childs descendants
Any node, together with all of its descendents
9. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 9 Introduction & Terminology Nodes:
Contains information of an element
Unless otherwise stated, each node contains only one element
Usually divided into key part & data part
Key has unique value and is used in searching
Each node may contain one or more pointers to other nodes
A node can have more than one immediate successor (child) that lies directly below it
Each node has at most one predecessor (parent) that lies directly above it
10. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 10 Introduction & Terminology Special nodes
Parent (predecessor) of node n
The (predecessor) node directly above node n in the tree
Child (successor) of node n
The (successor) node directly below node n in the tree
Leaf
A node with no children (successors)
Root
The only node in the tree with no parent (predecessor)
Exactly one node per tree
Internal node
Neither a root node nor a leaf node
Have a unique predecessor and at least one successor
Sibling nodes
Children of the same parent
11. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 11 Introduction & Terminology Path from node n1 to nk:
A sequence of nodes n1, n2, . nk such that there is an edge between each pair of nodes
(n1, n2) (n2, n3), . . . (nk-1, nk)
Ancestor-descendent relationship
Generalization of parent-child relationship
Ancestor of node n:
A node on the path from the root to n
Descendent of node n:
A node on the path from n to a leaf
12. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 12 Introduction & Terminology Level of a node n in a tree T
If n is the root of T, it is at level 1
If n is not the root of T, its level is 1 greater than the level of its parent
13. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 13 Introduction & Terminology Height h of a tree T defined in terms of the levels of its nodes
The number of nodes on the longest path from the root to a leaf
If T is empty, its height is 0
If T is not empty, its height is equal to the maximum level of its nodes
A recursive definition of height (assuming a binary tree)
height(T) = 1 + max{height(TL), height(TR)}
14. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 14 Special Trees Binary tree (BT):
A set T of nodes that is either
T is empty, or
T is partitioned into three disjoint subsets:
A single node r, the root node
Two, possibly empty sets that are binary trees, called the left & right subtrees of r
Each node has at most two children
Left child
Right child
Left (or Right) child of node n:
For binary trees
A node directly below and to the left (or right) of node n
15. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 15 Special Trees Examples: Expression trees
16. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 16 Special Trees Binary search tree (BST):
A binary tree that has the following properties for each node n
ns value is greater than all values in its left subtree TL
ns value is less than all values in its right subtree TR
Both TL and TR are binary search trees
17. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 17 Special Trees Left (right) subtree of node n:
In a binary tree, the left (right) child (if any) of node n plus its descendants
Empty BT:
A binary tree with no nodes
18. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 18 Special Trees Full BT:
A binary tree of height h with no missing nodes. All leaves are at level h and all other nodes each have two children
Recursive definition of a full binary tree
If T is empty, T is a full binary tree of height 0
If T is not empty and has height h > 0, T is a full binary tree if its roots subtrees are both full binary trees of height h 1
19. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 19 Special Trees Complete BT:
A binary tree T of height h is complete if
All nodes at level h 2 and above have two children each, and
When a node at level h 1 has children, all nodes to its left at the same level have two children each, and
When a node at level h 1 has one child, it is a left child
20. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 20 Special Trees Balanced BT:
A binary tree is balanced if the height of any nodes right subtree differs from the height of the nodes left subtree by no more than 1
Full binary trees are complete
Complete binary trees are balanced
21. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 21 ADT Binary Tree Operations
Create
Empty BT
One node BT, given an item
BT from an item for its root and two BTs for the roots subtrees
Destroy a BT
Determine whether a BT is empty
Retrieve BTs root
Retrieve left/right subtree of the BT
Attach left or right child / subtree to the BTs root
Detach the left/right subtree of the BTs root
Traverse a BT in:
Preorder
Inorder
Postorder
22. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 22 ADT Binary Tree Operations
+CreateBinaryTree()
// Creates an empty Binary Tree
+CretaeBinaryTree(in rootItem:TreeItemType)
// Creates a one-node BT whose root contains RootItem
+CreateBT(in rootItem:TreeItemType,
inout leftTree:BinaryTree,inout rightTree:BinaryTree)
// Creates a BT with root data RootItem and a leftTree and rightTree,
// respectively as its left & right subtrees. Makes leftTree & rightTree empty
// so they cant be used to gain access to the new tree.
+destroyBinaryTree()
// Destroys a binary tree
+isEmpty():boolean {query}
// Determines whether a binary tree is empty
23. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 23 ADT Binary Tree Operations
+getRootData():TreeItemType throw TreeException
// Returns the data item in the root of a nonempty subtree.
// Throws an exception if the tree is empty
+setRootData(in newItem:TreeItemType) throw TreeException
// Replaces the data item in the root of a binary tree with newItem, if the
// tree is not empty, otherwise a root node is created with newItem as its data
// item and is inserted in the tree.
// If a new node cant be created, TreeException is thrown
+attachLeft(in newItem: TreeItemType) throw TreeException
// Attaches a left child containing newItem to the root of a binary tree.
// Throws TreeException if the binary tree is empty
// or a left subtree already exists.
+attachRight(in newItem:TreeItemType) throw TreeException
// Attaches a right child containing newItem to the root of a binary tree.
// Throws TreeException if the binary tree is empty
// or a right subtree already exists.
24. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 24 ADT Binary Tree Operations
+attachLeftSubtree(inout leftTree:BinaryTree) throw TreeException
// Attaches leftTree as the left subtree of the root of a binary tree and makes
// leftTree empty so that it cant be used to access this tree. Throws
// TreeException if the binary tree is empty or a left subtree already exists.
+attachRightSubtree(inout rightTree:BinaryTree) throw TreeException
// Attaches rightTree as the right subtree of the root of a binary tree and
// makes rightTree empty so that it cant be used to access this tree. Throws
// TreeException if the binary tree is empty or a right subtree already exists.
+detachLeftSubtree(out leftTree:BinaryTree) ThrowTreeException
// Detaches the left subtree of a binary trees root. and retains it in
// leftTree. Throws TreeException if the tree is empty.
+detachRightSubtree(out rightTree:BinaryTree) ThrowTreeException
// Detaches the right subtree of a binary trees root. and retains it in
// rightTree. Throws TreeException if the tree is empty.
25. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 25 ADT Binary Tree Operations
+getLeftSubtree( ):BinaryTree
// Returns a copy of the left subtree of a binary trees root without
// detaching the subtree. Returns an empty tree if the binary tree is empty.
+getRightSubtree( ):BinaryTree
// Returns a copy of the right subtree of a binary trees root without
// detaching the subtree. Returns an empty tree if the binary tree is empty.
+preorderTraverse(in visit:FunctionType)
// Traverses a binary tree in preorder &
// calls function visit() once for each node
+inorderTraverse(in visit:FunctionType)
// Traverses a binary tree in inorder &
// calls function visit() once for each node
+postorderTraverse(in visit:FunctionType)
// Traverses a binary tree in postorder &
// calls function visit() once for each node
26. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 26 Using ADT Building a ADT BT
tree1.setRootData(F)
tree1.attachLeft(G)
tree2.setRootData(D)
tree2.attachLeftSubtree(tree1)
tree3.setRootData(B)
tree3.attachLeftSubtree(tree2)
tree3.attachRight(E)
tree4.setRootData(C)
binTree.createBinaryTree(A, tree3, tree4)
//Check fig 10-6b
27. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 27 BT Traversal Visit every node in a BT exactly once, performing the same operation (e.g. printing node contents, or retrieving some information) on each node
If visit operation includes more than display operation (e.g. copy or modify an item), the traversal will be more difficult
Has recursive nature
Visiting sequence is important
Time of action performed on root depends on the order of traversal
28. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 28 BT Traversal General form of a recursive traversal algorithm
traverse (in binTree:BinaryTree)
if (binTree is not empty) {
traverse (Left subtree of binTrees root)
traverse (Right subtree of binTrees root)
}
Where should we put the root processing?
29. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 29 BT Traversal Natural traversal orders:
Pre-order
Depth-First or Node-Left-Right (NLR)
In-order
Symmetric or Left-Node-Right (LNR)
Post-order
Left-Right-Node (LRN)
A traversal operation can call a function to perform a task on each item in the tree
The client defines and passes this function as an argument
30. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 30 BT Traversal Examples:
31. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 31 Pre-order (NLR) Traversal Each node is processed the first time it is observed. i.e. before any node in either subtree.
Recursive definition:
Visit the root
Traverse the left subtree in preorder
Traverse the right subtree in preorder
32. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 32 Preorder (NLR) Traversal Pseudocode
preorder (in binTree:BinaryTree )
// Traverses the binary tree binTree in preorder.
// Assumes that visit a node means to display the nodes data item
if (binTree is not empty )
{
Display the data in the root of binTree
preorder ( Left subtree of binTree s root )
preorder ( Right subtree of binTree s root )
}
33. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 33 In-order (LNR) Traversal Each node is processed the second time it is observed. i.e. after all the nodes in its left subtree but before any of the nodes in its right subtree.
Recursive definition:
Traverse the left subtree in inorder
Visit the root
Traverse the right subtree in inorder
Inorder traversal of BST will visit the trees nodes in sorted search key order
34. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 34 Inorder (LNR) Traversal Pseudocode
inorder (in binTree:BinaryTree )
// Traverses the binary tree binTree in inorder.
// Assumes that visit a node means to display the nodes data item
if (binTree is not empty )
{
inorder ( Left subtree of binTree s root )
Display the data in the root of binTree
inorder ( Right subtree of binTree s root )
}
35. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 35 Post-order (LRN) Traversal Each node is processed the third time it is observed. i.e. after all nodes in both of its subtrees.
Recursive definition:
Traverse the left subtree in postorder
Traverse the right subtree in postorder
Visit the root
36. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 36 Postorder (LRN) Traversal Pseudocode
postorder (in binTree:BinaryTree )
// Traverses the binary tree binTree in postorder.
// Assumes that visit a node means to display the nodes data item
if (binTree is not empty )
{
postorder ( Left subtree of binTree s root )
postorder ( Right subtree of binTree s root )
Display the data in the root of binTree
}
37. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 37 Implementation of BT Possible Implementations:
Array based
Using an array of tree nodes
Requires the creation of a free list which keeps track of available nodes
Fast access
Difficult to change
Used if elements are not modified often
Pointer based (linked)
Slower
Easy to modify
More flexible when elements are often modified
38. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 38 BT Array-Based Implementation Nodes:
A C++ structure
Each node should contain:
Data portion
Left-child index
Right-child index
Tree:
Array of structures
For empty tree
Root's index = -1
39. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 39 BT Array-Based Implementation Example
40. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 40 BT Array-Based Implementation Structure: TreeNode class
const int MAX_NODES = 100; // maximum # nodes
typedef string TreeItemType;
class TreeNode // node in the tree
{ private:
TreeNode(); // constructors
TreeNode(const TreeItemType& nodeItem, int left, int right);
TreeItemType item; // data portion
int leftChild; // index to left child
int rightChild; // index to right child
// friend class can access private members
friend class BinaryTree;
}; // end class TreeNode
TreeNode[MAX_NODES] tree; // array of tree nodes
int root; // index of root
int free; // index of free list
41. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 41 BT Array-Based Implementation As insertion and/or deletion operations are performed, the elements of the tree might not be in consecutive locations in the array
A list of free nodes should be maintained ;
When a node is deleted, it is added to the free list
When a node is inserted, it is removed from the free list
42. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 42 BT Array-Based Implementation If the BT is complete, simpler array-based implementation can be used that contains no Lchild, or Rchild entries.
Formulas to find the location of any node in the tree
Lchild ( Tree [ i ] ) = Tree [ 2 * i + 1 ]
Rchild ( Tree [ i ] ) = Tree [ 2 * i + 2 ]
Parent (Tree [ i ] ) = Tree [ ( i - 1) / 2 ]
43. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 43 BT Pointer-Based Implementation Typical for implementing BTs
Different constructors to allow defining the BT in a variety of circumstances
Only the pointer to the root of the tree can be accessed by BT class clients
Clients shouldnt have access to node pointers inside the class
The data structure would be private data member of a class of binary trees
44. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 44 BT Pointer-Based Implementation Operations accessing pointers inside the tree should be protected
Traversal of the tree should be carefully designed so that the ADT rules are not violated by accessing pointers of interior nodes in the tree
The copy constructor and the destructor implicitly use traversal
45. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 45 BT Pointer-Based Implementation We can use C++ pointers to link the nodes in the tree
typedef string TreeItemType;
class TreeNode // node in the tree
{
private:
TreeNode() {}; // constructors
TreeNode(const TreeItemType& nodeItem,
TreeNode *left = NULL, TreeNode *right = NULL)
: item(nodeItem), leftChildPtr(left), rightChildPtr(right) { }
TreeItemType item; // data portion
TreeNode *leftChildPtr; // pointer to left child
TreeNode *rightChildPtr; // pointer to right child
friend class BinaryTree;
}; // end TreeNode class
46. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 46 BT Pointer-Based Implementation Example:
The external pointer root points to the trees root
root -> leftChildPtr points to the root of the left subtree
root -> roghtChildPtr points to the root of the right subtree
47. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 47 BT Pointer-Based Implementation Header file TreeException.h
Header file BinaryTree.h
Several constructors, including:
A pointer to a root node is a protected constructor to prohibit client access
Copy constructor calls a private function that traverses the tree and copies each node
Overloaded assignment operator
Protected member functions, such as inorder, enable recursive traversals
void inorder(TreeNode *treeptr,
FunctionType visit);
48. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 48 BT Pointer-Based Implementation Implementation file BinaryTree.cpp
Implement traversals so that visit remains on the clients side of the wall
inorderTraverse is a public function that calls inorder private function
visit is a formal parameter for both functions
49. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 49 Using the BT Class Code to Build & Traverse a BT
50. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 50 Non-Recursive BT Traversal (pp. 531-535)
An iterative method and an explicit stack can mimic actions at a return from a recursive call to inorder
51. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 51 Non-Recursive BT Traversal Pseudocode
traverse (in visit:FunctionType)
// Nonrecursively traverse a binary tree inorder.
//initialize
Create an empty stack s
cur = rootPrt() // start at root
done = false
while (!done) {
if (cur != NULL) {
// place pointer to node on stack before trversing nodes left subtree
s.push(cur)
// traverse the left subtree
cur = cur -> leftChildPtr
}
else { // Backtrack from the empty subtree & visit the node at the top of the stack; however,
// if the stack is empty, you are done
if (!s.isEmpty()) {
s.getTop(cur)
visit (cur -> item)
s.pop()
// traverse the right subtree of the node just visited
cur = cur -> rightChildPtr
}
else done = true
} // end else backtrack
} // end while
52. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 52 Non-Recursive BT Traversal Threaded Trees
A binary tree with n nodes has n+1 NULL pointers; i.e half of the storage that is allocated for pointer is wasted.
Makes use of the empty leaf pointers by making the right leaf pointer point to the inorder successor. The pointer is known as thread. The tree node class can be extended by adding a private Boolean variable rightTread that indicate whether the pointer is a thread or not, and method RightThread that returns the value of the thread.
Some URLs:
NIST
itgo.com
53. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 53 Non-Recursive BT Traversal Threaded Tree Traversal (Printing)
Start at the leftmost node in the tree, print the node contents
Follow the right pointer
If it is a thread, follow the thread, print contents, and continue to the right
If it is a normal link, go to the leftmost node, print it, and continue
Threaded Tree Modification
We are still wasting half of the NULL pointers
We can add threads that point to the inorder predecessor to be able to traverse the tree backwards or to perform postorder traversal
54. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 54 ADT Binary Search Tree (BST) Problem of general BT
Searching for a particular item is difficult
Solution
Use BST
BST
Value-oriented BT
Each node satisfies the following properties:
ns value > all values in its left subtree TL
ns value < all values in its right subtree TR
Both TL and TR are BST
Insertion, deletion, and retrieval operations are by search-key value, not by position
55. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 55 ADT BST Operations Create an empty BST
Destroy a BST
Determine whether a BST is empty
Insert a new item into a BST
Delete the item with a given search key from a BST
Retrieve the item with a given search key from a BST
Traverse the items in a BST in preorder, inorder, or postorder
56. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 56 ADT BST Operations +createSearchTree()
// Creates an empty BST.
+destroySearchTree()
// Destroys a BST.
+isEmpty(): boolean {query}
// Determines if a BST is empty.
+searchTreeInsert(in newItem:TreeItemType) throw TreeException
// Inserts newItem into a BST whose items have distinct keys that differ from
// newItems search key. Throws TreeExceptoion if the insertion is not successful
+searchTreeDelete(in searchKey:KeyType) throw TreeException
// Deletes from BST the item whose search key = searchKey.
// Throws TreeExceptoion if the item does not exist
+searchTreeRetrieve(in SearchKey:KeyType,out treeItem:TreeItemType)
throw TreeException
// Retrieves into treeItem the item in a BST with searchKey.
// Throws TreeExceotion if item does not exist
// The following 3 functions traverse a BST in preorder, inorder, & postorder
// respectively, and call function visit() once for each item
+preorderTraverse(in visit:FunctionType)
+inorderTraverse(in visit: FunctionType)
+PostOrderTravers(in visit: FunctionType)
57. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 57 ADT BST Implementation Implementation of the Structure: Class KeyedItem
The record, or item, in the tree would be an instance of a C++ class of the following form
class keyedItem
{ public:
keyedItem ( ) { }; // Default constructor
// Copy constructor
keyedItem (const KeyType & keyValue) : searchKey(keyValue) { }
KeyType getKey () const // returns search key
{ return searchKey;
} // end getKey
private:
keyType searchKey;
... other data members, if any
};
58. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 58 Using ADT BST Example BST of names
Each node will be a record representing a person
New Class: Person Class
Inherited from keyedItem class
Key = Person name
class Person : public KeyedItem
{public:
Person() {}
Person(const string &name, const string &id, const string &phone)
: KeyedItem(name), idNum(id),phoneNumber(phone) { }
private:
// Key item is the person's name
string idNum;
string phoneNumber;
//... and other data about the person
}; // end class
...
nameTree.searchTreeRetrieve(Nancy, nameRecord)
nameTree.searchTreeInsert(HalRecord)
nameTree.searchTreeDelete(Jane)
nameTree.inorderTraverse(displayName)
. . . etc.
59. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 59 ADT BST Implementation Implementation of BST Operations
Because a BST is recursive in nature, operations are implemented using recursive algorithms
A search algorithm is needed as the basis of some operations: insertion, deletion, retrieval
A recursive search function
Searches the binary search tree binTree for the item whose search key is searchKey
Searches recursively in the right or left subtree (depending on the value of the item) until searchKey matches the search key of the nodes item
search(in binTree:BinarySearchTree, in searchKey:KeyType);
60. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 60 ADT BST Implementation Pseudocode of Search Strategy :
search (in binTree:BinarySearchTree, in seachKey:KeyType)
// Searches the BST binTree for the item whose key is searchKey
if (binTree is empty)
The desired record is not found
else if (searchKey = = search key of roots item)
The desired record is found
else if (searchKey < search key of roots item)
search (Left subtree of binTree, searchKey)
else
search (Right subtree of binTree, searchKey)
61. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 61 ADT BST Implementation The shape of the tree doesnt affect the search algorithm. However, the algorithm works more efficiently on some trees than the others
62. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 62 ADT BST Implementation BST Insertion:
insertItem(inout treePtr:TreeNodePtr,
in newItem:TreeItemType)
Insertion should preserve the tree's properties
Idea
First search for the correct position to insert the given item
If the item doesnt exist in the BST, item will be inserted as a new leaf
The search will always terminate at an empty subtree
63. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 63 ADT BST Implementation BST Insertion:
Insert Frank
64. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 64 ADT BST Implementation BST Insertion Pseudocode:
insertItem ( inout treePtr: TreeNodePtr,
in NewItem: TreeItemType)
{ if (treePtr is Null)
{
Create a new node and let treePtr point to it
Copy newItem into new nodes data portion
Set the left & right pointers in the new node to NULL
}
else if (newItem.getKey( ) < treePtr-> getKey ( ) )
insertItem(treePtr->leftChildPtr, newItem)
else
insertItem(treePtr->rightChildPtr, newItem)
65. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 65 ADT BST Implementation Insertion observations:
The recursive nature of the search algorithm provides an elegant means of setting the pointer, provided that treePtr is passed as a reference argument
insertItem can be used to create a BST
If we traverse a BST in preorder and use insertItem to create a BST out of the traversed items, we obtain a duplicate tree
By inserting the items in different order, we get a different BST
66. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 66 ADT BST Implementation BST Deletion:
More involved than insertion
Use Search to locate the item with the specified search key
If found, remove the item from the tree
Special Cases to delete node N:
N is a leaf
N has only one left child
N has only one right child
N has two children
The two possibilities of having one child are symmetrical, so it is sufficient to consider one case only (left child)
The deletion strategy should preserve the BST property
67. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 67 ADT BST Implementation BST Deletion:
Case 1: N is a Leaf
Set the pointer in the leafs parent to NULL
Case 2: N has only one child
Let Ns parent adopt Ns child
Before deleting node N After deletion
68. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 68 ADT BST Implementation Deletion
Case 2: Another situation
Let Ns parent adopt Ls child
Before deleting node N After deletion
69. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 69 ADT BST Implementation Deletion
Case 3: N has two children
Ns parent has room for only one of Ns children as a replacement for N
Instead of directly deleting node N, do the following:
Find another node, M, that is easier to delete instead of N
Copy the item that is in M to N, thus effectively deleting from the tree the item originally in N
Remove the node M from the tree
The above operation should preserve the BST property
70. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 70 ADT BST Implementation BST Deletion:
Case 3 Example: Delete Jane?
Which node to replace it with & keep the BST properties?
71. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 71 ADT BST Implementation BST Deletion:
Case 3: N has two children
There are two suitable possibilities when choosing node M:
Inorder predecessor:
M has to be with a key immediately before the key of N
Inorder successor:
M has to be with a key immediately after the key of N
72. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 72 ADT BST Implementation BST Deletion:
Case 3: N has two children
73. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 73 ADT BST Implementation BST Deletion Pseudocode:
dleteItem (inout treePtr:TreeNodePtr,in sSearchKey:KeyType)
throw TreeException
if (treePtr = = NULL)
Throw TreeExceoption // item not found
else if ( searchKey == treePtr -> item.getKey ( )) // item at root
deleteNodeItem(treePtr) // remove item from tree
else if ( searchKey < treePtr -> item.getKey ( ))
deleteItem(treePtr->leftChildPtr, searchKey)
else deleteItem(treePtr->rightChildPtr, searchKey)
74. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 74 ADT BST Implementation BST Deletion Pseudocode:
deleteNodeItem (inout nodePtr:TreeNodePtr)
// Deletes item in node N to which nodePtr points.
if (N is a leaf)
{ delete nodePtr
nodePtr=NULL
}
else if ( n has only one child C)
{ delPtr = nodePtr
if (C is the left child of N)
nodePtr = nodePtr-> leftChildPtr
else nodePtr = nodePtr-> rightChildPtr
delete delPtr
}
else // N has 2 children
{ processLeftmost(nodePtr->rightChildPtr, replacementItem)
put replacementItem in node N
}
75. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 75 ADT BST Implementation BST Deletion Pseudocode:
processLeftmost (inout nodePtr:TreeNodePtr,
out treeItem:TreeItemType)
// Retrieves into treeItem the item in leftmost descendent of the node to which nodePtr points, deletes the node that contains the item.
if (nodePtr->leftChildPtr == NULL)
{ treeItem = nodePtr-> item
delPtr = nodePtr
nodePtr = nodePtr->rightChildPtr
delete delPtr
}
else processLeftmost(nodePtr->leftChildPtr, treeItem)
76. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 76 ADT BST Implementation Retrieve:
Returns the item with the desired search key
By refining the search algorithm, we can implement the retrieval operation
Return the item with the desired search key if it exists
Otherwise, throw TreeException
77. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 77 ADT BST Implementation RetrievePseudocode:
retrieveItem (in treePtr:TreeNodePtr, in searchKey:KeyType,
out treeItem:TreeItemType) throw TreeException
if (treePtr == NULL)
throw a tree exception item not found
else if (searchKey == treePtr->item.getKey( ) )
treeItem= treePtr->item
else if (searchKey < treePtr->item.getKey( ) )
retrieveITem(treePtr->leftChildPtr, searchKey, treeItem)
else
retrieveITem(treePtr->rightChildPtr, searchKey, treeItem)
78. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 78 ADT BST Implementation BST Traversal:
We can have:
Different traversal operation for each desired task
preorderTraverseAndDisplay
preorederTraverseAndCopy
A traversal operation could call a function, which the client defines and passes as an argument
preorderTraverse ( visit ) // function definition
. . .
T.preorderTraverse ( display ) // function call
In the second alternative, the wall between the program and the implementation of the ADT is not violated, because display on the clients side can only access the data by using the ADT operations
79. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 79 ADT BST Implementation Traversal:
The implementation should include the following functions:
Public Member Functions
virtual void preorderTraverse (functionType visit);
virtual void inorderTraverse (functionType visit);
virtual void postorderTraverse (functionType visit);
Private (Protected) Member Functions
void preorder (TreePtrType treePtr, functionType visit);
void inorder (TreePtrType treePtr, functionType visit);
void postorder (TreePtrType treePtr, functionType visit);
80. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 80 ADT BST Implementation BST Traversal Public Function Implementation
The same as for ADT binary tree
81. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 81 ADT BST Implementation Pointer-Based Implementation of BST Operations
Header file KeyedItem.h
Header file TreeNode.h
Header file BST.h
Implementation file BST.cpp
82. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 82 Efficiency of BST Operations For the retrieval, insertion, and deletion operations, compare the searchKey to the search keys in the nodes along a path through the tree
The path terminates
At the node that contains the search key or,
If the searchKey isn't present, at an empty subtree
The number of comparisons
= number of nodes along the path
Maximum number of comparisons
= number of nodes on the longest path through the tree (Height)
83. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 83 Efficiency of BST Operations The maximum height of n-Node tree:
When the tree is skewed = n
Complete trees & full trees have minimum height
The height of an n-node BST ranges from ?log2(n+1)? to n
Insertion in search-key order produces a maximum-height BST
Insertion in random order produces a near-minimum-height BST
The height of a particular binary search tree depends on the order in which insertion and deletion operations are performed
84. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 84 Efficiency of BST Operations Theorems:
The inorder traversal of a binary search tree T will visit its nodes in sorted search-key order
A full binary tree of height h ? 0 has 2h - 1 nodes
The maximum number of nodes that a binary tree of height h can have is 2h 1
The minimum height of a binary tree with N nodes is ? log2 ( N + 1 )?
Proof:
Self Study
85. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 85 Efficiency of BST Operations
86. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 86 Applications: Tree-Sort ADT BST can be used to sort array elements efficiently into search-key order
Pseudocode:
treeSort (A, n)
// Sorts the n integers in an array A into ascending order
Insert A's elements into a BST (T)
Traverse T inorder.
As you visit T's nodes, copy their data items into successive locations of A.
Advantage:
Time efficient
Average case: O(n * log n)
Worst case: O(n2 )
Disadvantage:
Memory space needed for the BST
87. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 87 STL Search Algorithms Defined for sorted ranges
binary_search
Returns true if a specified value appears in the sorted range
lower_bound; upper_bound
Returns an iterator to the first occurrence; or to one past the last occurrence of a value
equal_range
Returns a pair of iterators that indicate the first and one past the last occurrence of a value
88. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 88 General Trees Nodes can have more than two children
n-ary tree:
A tree whose nodes can have up to n children
For pointer-based implementation, the node will point to its oldest (first) child, that in return will point to a list of its siblings
89. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 89 General Trees If the maximum number of children is reasonable and known, direct pointers from the parent nodes can be used
90. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 90 Saving / Restoring BST in & from Files Two different possible algorithms
Save a BST & then restore it to its original shape
Use preorder traversal & searchTreeInsert to save & then restore a BST into its original shape
Save a BST & then restore it to a balanced shape
Uses inorder traversal to save the tree to a file
Can be used if the data is sorted and the number of nodes in the tree is known
Balanced BST increases the efficiency of the ADT operation
91. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 91 Saving / Restoring BST in & from Files You can construct the tree directly by reading the sorted data sequentially from the file
The tree need not be complete
It doesn't matter where the nodes on the last level go
If n is even, one of the subtrees will have more nodes than the other. Arbitrarily choose the extra nodes to be on the left subtree
92. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 92 Saving / Restoring BST in & from Files Building Full BST
readFull(out treePtr:TreeNodePtr,in n:integer)
// Builds a full BST from n sorted values in a file
// treePtr will point to the root
if (n>0)
{ // Construct the left subtree
treePtr = pointer to new node with NULL child pointers
readFull ( treePtr -> leftChildPtr, n/2)
// Get the root
Read item from file into treePtr -> item
// Construct the right subtree
readFull ( treePtr -> rightChildPtr, n/2)
} // End If
93. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 93 Saving/Restoring BST In & From Files Building Minimum-Height BST
readTree (out treePtr: TreeNodePtr, in n:integer)
// Builds a minimum-heigt BST from n sorted values in a file
// treePtr will point to the tree's root
if ( n > 0)
{ / / Construct the left subtree
treePtr = pointer to new node with NULL child pointers
readTree (treePtr -> leftChildPtr, n/2)
/ / Get the root
Read item from file into treePtr -> item
/ / Construct the right Subtree
readTree ( treePtr -> rightChildPtr, (n-1)/2)
} / / End If
94. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 94 Building Trees You can easily restore a tree as a balanced BST if the data is sorted
You need n to determine the middle item & number of nodes in each subtree
You can get n by counting the number of nodes as you traverse the tree
readTree should be a protected member function of BinarySearchTree class
95. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 95 Summary Binary trees provide a hierarchical organization of data
The implementation of a binary tree is usually pointer-based
A client-defined visit function to the traversal operation can customize the operations on the items of the tree
The binary search tree allows you to use a binary search-like algorithm to search for an item with a specified value
96. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 96 Summary Binary search trees come in many shapes
The height of a binary search tree with n nodes can range from a minimum of ?log2(n + 1)? to a maximum of n
The shape of a binary search tree determines the efficiency of its operations
An inorder traversal of a binary search tree visits the trees nodes in sorted search-key order
The treesort algorithm efficiently sorts an array by using the binary search trees insertion and traversal operations
97. M. Malaty - OCC - CS/CIS - CS200 - Data Structures Chapter 10: Trees 97 Summary Saving a binary search tree to a file
To restore the tree as a binary search tree of minimum height
Perform inorder traversal while saving the tree to a file
To restore the tree to its original form
Perform preorder traversal while saving the tree to a file