Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Trees Lecture 36 Wed, Apr 21, 2004 9/21/2018 Binary Trees.

Similar presentations


Presentation on theme: "Binary Trees Lecture 36 Wed, Apr 21, 2004 9/21/2018 Binary Trees."— Presentation transcript:

1 Binary Trees Lecture 36 Wed, Apr 21, 2004 9/21/2018 Binary Trees

2 Topics Binary trees Array implementation Linked implementation
Binary tree nodes 9/21/2018 Binary Trees

3 Binary Trees A binary tree is a data structure with the following properties. It is either empty or it has a root node. Each node in the binary tree may have up to two children (called left and right). Each node, except the root node, has exactly one parent. The root node has no parent. 9/21/2018 Binary Trees

4 A Binary Tree 10 20 30 40 50 60 70 9/21/2018 Binary Trees

5 Terminology The tree metaphor The family metaphor
tree, root, branch, leaf. The family metaphor parent, child, sibling, ancestor, descendant. 9/21/2018 Binary Trees

6 Tree Terminology 10 20 30 40 50 60 70 9/21/2018 Binary Trees

7 Tree Terminology Root 10 20 30 40 50 60 70 9/21/2018 Binary Trees

8 Tree Terminology 10 20 30 40 50 60 70 Parent 9/21/2018 Binary Trees

9 Tree Terminology 10 20 30 40 50 60 70 Leaves 9/21/2018 Binary Trees

10 Tree Terminology 10 20 30 40 50 60 70 Right Child 9/21/2018
Binary Trees

11 Tree Terminology Subtree 10 20 30 40 50 60 70 9/21/2018 Binary Trees

12 Array Implementation of a Binary Tree
In an array binary tree, the nodes of the tree are stored in an array. Position 0 is left empty. The root is stored in position 1. For the element in position n, The left child is in position 2n. The right child is in position 2n + 1. The parent is in position n/2. 9/21/2018 Binary Trees

13 Array Implementation 10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9 9/21/2018
9/21/2018 Binary Trees

14 Array Implementation Unused 10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9
9/21/2018 Binary Trees

15 Array Implementation Root 10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9
9/21/2018 Binary Trees

16 Array Implementation Parent 10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9
9/21/2018 Binary Trees

17 Array Implementation Parents, do you know where your children are?
10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9 Parents, do you know where your children are? 9/21/2018 Binary Trees

18 Array Implementation Parents, do you know where your children are?
10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9 Parents, do you know where your children are? 9/21/2018 Binary Trees

19 Advantages of the Array Implementation
This representation is very efficient when The tree is full, and The structure of the tree will not be modified. 9/21/2018 Binary Trees

20 Linked Implementation of a Binary Tree
In a linked binary tree, each node contains pointers to its left and right children. A linked binary tree object has one data member: BinaryTreeNode* root - A pointer to the root node. 9/21/2018 Binary Trees

21 Binary Tree Nodes A binary tree node has three components.
A value (data) that contains the tree element. A left pointer (left) that points to the root node of the left subtree. A right pointer (right) that points to the root node of the right subtree. 9/21/2018 Binary Trees

22 Binary Tree Nodes Nodes are allocated and deallocated dynamically.
The binary tree always has allocated the exact number of nodes necessary to store the tree elements. Example binarytreenode.h 9/21/2018 Binary Trees

23 Binary Tree Implementation
Lecture 37 Thu, Apr 22, 2004 9/21/2018 Binary Trees

24 Topics Binary tree interface Implementation of Search()
Implementation of Draw() 9/21/2018 Binary Trees

25 Binary Tree Constructors
BinaryTree(const T& value); BinaryTree(const BinaryTree& lft, const BinaryTree& rgt); BinaryTree(const T& value, const BinaryTree& lft, BinaryTree(const BinaryTree& tree); 9/21/2018 Binary Trees

26 Binary Tree Destructor
9/21/2018 Binary Trees

27 Binary Tree Inspectors
int Size() const; int Height() const; bool Empty() const; T& Root() const; BinaryTree LeftSubtree() const; BinaryTree RightSubtree() const; bool IsCountBalanced() const; bool IsHeightBalanced() const; 9/21/2018 Binary Trees

28 Binary Tree Mutators void MakeEmpty(); void SetRoot(const T& value);
9/21/2018 Binary Trees

29 Binary Tree Facilitators
void Input(istream& in); void Output(ostream& out); bool Equal(BinaryTree tree) const; 9/21/2018 Binary Trees

30 Binary Tree Operators BinaryTree& operator=(const BinaryTree&);
istream& operator>>(istream&, BinaryTree&); ostream& operator<<(ostream&, const BinaryTree&); bool operator==(BinaryTree&); 9/21/2018 Binary Trees

31 Binary Tree Traversal Functions
void PreorderTraversal(void (* Visit) (BinaryTreeNode*)) const; void InorderTraversal(void (* Visit) void PostorderTraversal(void (* Visit) void LevelorderTraversal(void (* Visit) 9/21/2018 Binary Trees

32 Other Binary Tree Functions
T* Search(const T& value) const; void Draw() const; 9/21/2018 Binary Trees

33 Linked Binary Tree Implementation
Example binarytree.h BinaryTreeTest.cpp 9/21/2018 Binary Trees

34 Binary Tree Traversals
Lecture 38 Mon, Apr 26, 2004 9/21/2018 Binary Trees

35 Topics Binary tree traversals Binary tree iterators
Pre-order traversals In-order traversals Post-order traversals Level-order traversals Binary tree iterators Pre-order iterators In-order iterators Post-order iterators Level-order iterators 9/21/2018 Binary Trees

36 Binary Tree Traversals
Four Standard Traversals Pre-order Traversal. In-order Traversal. Post-order Traversal. Level-order Traversal. 9/21/2018 Binary Trees

37 Pre-order Traversal At each node, Also called an NLR traversal.
First visit the node, Then perform a pre-order traversal of the left subtree, Then perform a pre-order traversal of the right subtree. Also called an NLR traversal. Node - Left - Right. 9/21/2018 Binary Trees

38 Pre-Order Traversal 10 20 30 40 50 60 70 9/21/2018 Binary Trees

39 Pre-Order Traversal 10 20 30 40 50 60 70 10 9/21/2018 Binary Trees

40 Pre-Order Traversal 10 20 30 40 50 60 70 10 20 9/21/2018 Binary Trees

41 Pre-Order Traversal 10 20 30 40 50 60 70 10 20 40 9/21/2018
Binary Trees

42 Pre-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 9/21/2018
Binary Trees

43 Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 9/21/2018
Binary Trees

44 Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 9/21/2018
Binary Trees

45 Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 9/21/2018 Binary Trees

46 Pre-Order Traversal Order: 10, 20, 40, 50, 30, 60, 70 10 20 30 40 50
9/21/2018 Binary Trees

47 In-order Traversal At each node, Also called an LNR traversal.
First, perform an in-order traversal of the left subtree, Then visit the node, Then perform an in-order traversal of the right subtree. Also called an LNR traversal. Left - Node - Right. 9/21/2018 Binary Trees

48 In-Order Traversal 10 20 30 40 50 60 70 9/21/2018 Binary Trees

49 In-Order Traversal 10 20 30 40 50 60 70 40 9/21/2018 Binary Trees

50 In-Order Traversal 10 20 30 40 50 60 70 20 40 9/21/2018 Binary Trees

51 In-Order Traversal 10 20 30 40 50 60 70 20 40 50 9/21/2018 Binary Trees

52 In-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 9/21/2018 Binary Trees

53 In-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 60 9/21/2018 Binary Trees

54 In-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 9/21/2018 Binary Trees

55 In-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 9/21/2018 Binary Trees

56 In-Order Traversal Order: 40, 20, 50, 10, 60, 30, 70 10 20 30 40 50 60
9/21/2018 Binary Trees

57 Post-order Traversal At each node, Also called an LRN traversal.
First perform a post-order traversal of the left subtree, Then perform a post-order traversal of the right subtree, Then visit the node. Also called an LRN traversal. Left - Right - Node. 9/21/2018 Binary Trees

58 Post-Order Traversal 10 20 30 40 50 60 70 9/21/2018 Binary Trees

59 Post-Order Traversal 10 20 30 40 50 60 70 40 9/21/2018 Binary Trees

60 Post-Order Traversal 10 20 30 40 50 60 70 40 50 9/21/2018 Binary Trees

61 Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 9/21/2018
Binary Trees

62 Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 60 9/21/2018
Binary Trees

63 Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 60 70 9/21/2018
Binary Trees

64 Post-Order Traversal 10 20 30 40 50 60 70 20 30 40 50 60 70 9/21/2018
Binary Trees

65 Post-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 9/21/2018 Binary Trees

66 Post-Order Traversal Order: 40, 50, 20, 60, 70, 30, 10 10 20 30 40 50
9/21/2018 Binary Trees

67 Level-order Traversal
Traverse the levels of the tree from top to bottom. Within each level, visit the nodes from left to right. 9/21/2018 Binary Trees

68 Level-Order Traversal
10 20 30 40 50 60 70 9/21/2018 Binary Trees

69 Level-Order Traversal
10 20 30 40 50 60 70 10 9/21/2018 Binary Trees

70 Level-Order Traversal
10 20 30 40 50 60 70 10 20 9/21/2018 Binary Trees

71 Level-Order Traversal
10 20 30 40 50 60 70 10 20 30 9/21/2018 Binary Trees

72 Level-Order Traversal
10 20 30 40 50 60 70 10 20 30 40 9/21/2018 Binary Trees

73 Level-Order Traversal
10 20 30 40 50 60 70 10 20 30 40 50 9/21/2018 Binary Trees

74 Level-Order Traversal
10 20 30 40 50 60 70 10 20 30 40 50 60 9/21/2018 Binary Trees

75 Level-Order Traversal
10 20 30 40 50 60 70 10 20 30 40 50 60 70 9/21/2018 Binary Trees

76 Level-Order Traversal
10 20 30 40 50 60 70 10 20 30 40 50 60 70 Order: 10, 20, 30, 40, 50, 60, 70 9/21/2018 Binary Trees

77 Binary Tree Iterators A binary tree iterator systematically visits each node of a binary tree. The four standard iterators correspond to the four standard traversal methods. Pre-order iterator In-order iterator Post-order iterator Level-order iterator 9/21/2018 Binary Trees

78 Pre-order Iterator Visit the nodes in a pre-order traversal.
Use a stack to store unvisited nodes. Example binarytreeiterator.h binarytreepreorderiterator.h 9/21/2018 Binary Trees

79 Pre-order Iterator 10 20 30 40 50 60 70 10 10 10 20 20 20 30 30 30 40 50 60 70 9/21/2018 Binary Trees

80 In-order Iterator Visit the nodes in an in-order traversal.
Use a stack to store unvisited nodes. Example binarytreeiterator.h binarytreeinorderiterator.h 9/21/2018 Binary Trees

81 In-order Iterator 10 20 30 40 50 60 70 10 10 20 20 30 30 40 50 60 70 9/21/2018 Binary Trees

82 Post-order Iterator Visit the nodes in a post-order traversal.
Use a stack to store unvisited nodes. Example binarytreeiterator.h binarytreepostorderiterator.h 9/21/2018 Binary Trees

83 Post-order Iterator 10 20 30 40 50 60 70 10 10 20 20 30 30 40 50 60 70 9/21/2018 Binary Trees

84 Level-order Iterator Visit the nodes in a level-order traversal.
Use a queue to store unvisited nodes. 9/21/2018 Binary Trees

85 Level-order Iterator 10 20 30 40 50 60 70 10 20 20 30 30 40 40 50 50 60 60 70 70 9/21/2018 Binary Trees

86 Traversals and Expression Trees
Perform an in-order traversal of the expression tree and print the nodes. * + - 4 5 6 3 9/21/2018 Binary Trees

87 Traversals and Expression Trees
Perform a post-order traversal to evaluate the expression tree. * + - 4 5 6 3 9/21/2018 Binary Trees

88 Binary Search Trees Lecture 39 9/21/2018 Binary Trees

89 Topics Binary search trees Inserting a node Deleting a node
Balancing a binary search tree 9/21/2018 Binary Trees

90 Binary Search Trees A binary search tree is a binary tree with the following properties. There is a total order relation on the members in the tree. At every node, every member of the left subtree is less than or equal to the node value. At every node, every member of the right subtree is greater than or equal to the node value. 9/21/2018 Binary Trees

91 BinarySearchTree Implementation
The BinarySearchTree class is implemented as a subclass of the BinaryTree class. 9/21/2018 Binary Trees

92 Binary Search Tree Interface
Mutators void Insert(const T& value); void Delete(const T& value); Other member functions T* Search(const T& value) const; void CountBalance(); 9/21/2018 Binary Trees

93 Searching a BinarySearchTree
Begin at the root node. Apply the following algorithm recursively. Compare the value to the node data. If it is equal, you are done. If it is less, search the left subtree. If it is greater, search the right subtree. If the subtree is empty, the value is not in the tree. 9/21/2018 Binary Trees

94 Inserting a Value into a BinarySearchTree
Begin at the root node. Apply the following algorithm recursively. Compare the value to the node data. If it is less (or equal), continue with the left subtree. If is is greater, continue with the right subtree. When the subtree is empty, attach the node as a subtree. 9/21/2018 Binary Trees

95 Deleting a Value from a BinarySearchTree
Perform a search to locate the value. This node has Two children. One child. No child. 9/21/2018 Binary Trees

96 Deleting a Value from a BinarySearchTree
Case 1: The node has no child. Delete the node. Case 2: The node has one child. Replace the node with the subtree of which the child is the root. 9/21/2018 Binary Trees

97 Deleting a Value from a BinarySearchTree
Case 3: The node has two children. Locate the next smaller value in the tree. This value is the rightmost value of the left subtree. Move left one step. Move right as far as possible. Swap this value with the value to be deleted. The node to be deleted now has at most one child. 9/21/2018 Binary Trees

98 Count-Balancing a BinarySearchTree
Write a function MoveNodeRight() that will move the largest value of the left subtree to the right subtree. Locate the largest value in the left subtree. Delete it (but save the value). Place it at the root. Insert the old root value into the right subtree. 9/21/2018 Binary Trees

99 Count-Balancing a BinarySearchTree
Write a similar function MoveNodeLeft(). Apply either MoveNodeRight() or MoveNodeLeft() repeatedly at the root node until the tree is balanced at the root. Then apply these functions recursively, down to the leaves. 9/21/2018 Binary Trees

100 BinarySearchTree Implementation
Example binarysearchtree.h BinarySearchTreeTest.cpp 9/21/2018 Binary Trees


Download ppt "Binary Trees Lecture 36 Wed, Apr 21, 2004 9/21/2018 Binary Trees."

Similar presentations


Ads by Google