Professional Documents
Culture Documents
Binary Trees: Data Structures Using C++ 1
Binary Trees: Data Structures Using C++ 1
Binary Trees
Chapter Objectives
Learn about binary trees Explore various binary tree traversal algorithms Learn how to organize data in a binary search tree Discover how to insert and delete items in a binary search tree Explore nonrecursive binary tree traversal algorithms Learn about AVL (height-balanced) trees
Data Structures Using C++ 2
Binary Trees
Definition: A binary tree, T, is either empty or such that:
T has a special node called the root node; T has two sets of nodes, LT and RT, called the left subtree and right subtree of T, respectively; LT and RT are binary trees
Binary Tree
LA = empty
RA = empty
Binary Trees
Following struct defines the node of a binary tree:
template<class elemType> struct nodeType { elemType info; nodeType<elemType> *llink; nodeType<elemType> *rlink; };
Nodes
For each node:
Data is stored in info The pointer to the left child is stored in llink The pointer to the right child is stored in rlink
10
11
14
Copy Tree
Useful operation on binary trees is to make identical copy of binary tree Use function copyTree when we overload assignment operator and implement copy constructor
15
Copy Tree
template<class elemType> void copyTree(nodeType<elemType>* &copiedTreeRoot, nodeType<elemType>* otherTreeRoot) { if(otherTreeRoot == NULL) copiedTreeRoot = NULL; else { copiedTreeRoot = new nodeType<elemType>; copiedTreeRoot->info = otherTreeRoot->info; copyTree(copiedTreeRoot->llink, otherTreeRoot->llink); copyTree(copiedTreeRoot->rlink, otherTreeRoot->rlink); } }//end copyTree
16
Traversals
Inorder
Traverse the left subtree Visit the node Traverse the right subtree
Preorder
Visit the node Traverse the left subtree Traverse the right subtree
Data Structures Using C++ 18
Traversals
Postorder
Traverse the left subtree Traverse the right subtree Visit the node
19
20
{
inorder(p->llink); cout<<p->info<< ; inorder(p->rlink); } }
Data Structures Using C++ 21
}1
22
Private
copyTree Destroy Inorder, preorder, postorder Height Max nodeCount leavesCount
23
25
26
28
30
31
//start traversing the binary tree at // the root node while(current is not NULL or stack is nonempty) if(current is not NULL) { push current onto stack; current = current->llink; } else { pop stack into current; visit current; //visit the node current = current->rlink; //move to the //right child }
Data Structures Using C++ 32
current = root;
33
current = root; //start traversal at root node v = 0; if(current is NULL) the binary tree is empty if(current is not NULL) a. push current into stack; b. push 1 onto stack; c. current = current->llink; d. while(stack is not empty) if(current is not NULL and v is 0)
{
35
36
37
38
AVL Trees
39
Non-AVL Trees
40
41
42
43
44
45
Left rotation: certain nodes from the right subtree of x move to its left subtree; the root of the right subtree of x becomes the new root of the reconstructed subtree Right rotation at x: certain nodes from the left subtree of x move to its right subtree; the root of the left subtree of x becomes the new root of the reconstructed subtree
Data Structures Using C++ 46
47
48
49
50
51
52
54
55
Chapter Summary
Binary trees Binary search trees Recursive traversal algorithms Nonrecursive traversal algorithms AVL trees
57