Data Structures and Algorithms Introduction to Binary and Binary Search Trees
-
Upload
keith-norris -
Category
Documents
-
view
15 -
download
0
description
Transcript of Data Structures and Algorithms Introduction to Binary and Binary Search Trees
![Page 1: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/1.jpg)
Data Structures and Algorithms
Introduction to Binary and Binary Search Trees
Prepared by: S. Kondakci
![Page 2: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/2.jpg)
Tree
A tree is a recursively structured set of branches, where each branch consists of a collection of N nodes, one of which is the root and N-1 edges. The collection can be empty; otherwise, a tree consists of a distinguished node r, called the root, and zero or more non-empty (sub) trees T1, T2, …, Tk each of whose roots are connected by a directed edge from r.
![Page 3: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/3.jpg)
Tree terminology
• The root of each subtree is said to be a child of r and r is said to be the parent of each subtree root.
• Leaves: nodes with no children (also known as external nodes)
• Internal Nodes: nodes with children
• Siblings: nodes with the same parent
![Page 4: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/4.jpg)
Tree terminology (continued)
• A path from node n1 to nk is defined as a sequence of nodes n1, n2, …, nk such that ni is the parent of ni+1 for 1<= i <= k.
• The length of this path is the number of edges on the path namely k-1.
• The length of the path from a node to itself is 0.• There is exactly one path from the root to each
node.
![Page 5: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/5.jpg)
Tree terminology (continued)
• Depth: the length of the unique path from the root to a node.– The depth of a tree is equal to the depth of its
deepest leaf.
• Height: the length of the longest path from a node to a leaf.– All leaves have a height of 0
![Page 6: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/6.jpg)
Implementation of Binary trees
• A binary tree is a tree in which no node can have more than two children.
• Each node has an element, a reference to a left child and a reference to a right child.
struct TreeNode{ int element; TreeNode *left_child; TreeNode *right_child;};
![Page 7: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/7.jpg)
Picture of a binary tree
a
b c
d e
g h i
l
f
j k
![Page 8: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/8.jpg)
General Tree traversals
• A binary tree is defined recursively: it consists of a root, a left subtree, and a right subtree
• To traverse (or walk) the binary tree is to visit each node in the binary tree exactly once
• Tree traversals are naturally recursive• Since a binary tree has three “parts,” there are six
possible ways to traverse the binary tree:
– root, left, right– left, root, right– left, right, root
– root, right, left– right, root, left– right, left, root
![Page 9: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/9.jpg)
Traversing the Tree
• There are three common methods for traversing a binary tree and processing the value of each node: – inorder– preorder– postorder
• Each of these methods is best implemented as a recursive function.
![Page 10: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/10.jpg)
Inorder Traversal (LVR)
1. The node’s left subtree is traversed.
2. The node’s data is processed.
3. The node’s right subtree is traversed.
![Page 11: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/11.jpg)
Preorder Traversal (VLR)
1. The node’s data is processed.
2. The node’s left subtree is traversed.
3. The node’s right subtree is traversed.
![Page 12: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/12.jpg)
Postorder Traversal (LRV)
1. The node’s left subtree is traversed.
2. The node’s right subtree is traversed.
3. The node’s data is processed.
![Page 13: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/13.jpg)
Inorder traversal (LVR)
• In inorder, the root is visited in the middle• Here’s an inorder traversal to print out all
the elements in the binary tree:
void inorderPrint(BinaryTree *bt) { if (bt == null) return; inorderPrint(bt -> leftChild); printf(“%d \n”,bt -> element); inorderPrint(bt -> rightChild);}
![Page 14: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/14.jpg)
Preorder traversal (VLR)
• In preorder, the root is visited first• Here’s a C code implementing preorder
traversal to print out all the elements in a binary tree:
void preorderPrint(BinaryTree *bt) { if (bt == null) return; printf(“%d \n”,bt -> element); preorderPrint(bt -> leftChild); preorderPrint(bt -> rightChild);}
![Page 15: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/15.jpg)
Postorder traversal (LRV)
• In postorder, the root is visited last• Here’s a postorder traversal to print out all
the elements in the binary tree:
void postorderPrint(BinaryTree bt) { if (bt == null) return; postorderPrint(bt.leftChild); postorderPrint(bt.rightChild); printf(“%d \n”,bt -> element);
}
![Page 16: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/16.jpg)
Tree traversals Illustration using “flags”
• The order in which the nodes are visited during a tree traversal can be easily determined by imagining there is a “flag” attached to each node, as follows:
• To traverse the tree, collect the flags:
Preorder (VLR)
Inorder (LVR)
Postorder (LRV)
A
B C
D E F G
A
B C
D E F G
A
B C
D E F G
A B D E C F G D B E A F C G D E B F G C A
![Page 17: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/17.jpg)
Inorder Search Through Binary Tree (LVR)
14 34
33 23 35 55
89
12
75
![Page 18: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/18.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 19: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/19.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 20: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/20.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 21: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/21.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 22: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/22.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
89
![Page 23: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/23.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
89 33
![Page 24: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/24.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
89 33 14
![Page 25: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/25.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
89 33 14
![Page 26: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/26.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
89 33 14
![Page 27: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/27.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
89 33 14 75
![Page 28: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/28.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
89 33 14 75 23
![Page 29: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/29.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
89 33 14 75 23 12
![Page 30: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/30.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
89 33 14 75 23 12
![Page 31: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/31.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
89 33 14 75 23 12
![Page 32: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/32.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
89 33 14 75 23 12 35
![Page 33: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/33.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
89 33 14 75 23 12 35 34
![Page 34: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/34.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
89 33 14 75 23 12 35 34
![Page 35: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/35.jpg)
Inorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
89 33 14 75 23 12 35 34 55
![Page 36: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/36.jpg)
Preorder Search Through Binary Tree (VLR)
14 34
33 23 35 55
89
12
75
12
![Page 37: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/37.jpg)
Preorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
12 14
![Page 38: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/38.jpg)
Preorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
12 14 33
![Page 39: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/39.jpg)
Preorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
12 14 33 89
![Page 40: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/40.jpg)
Preorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
12 14 33 89 23
![Page 41: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/41.jpg)
Preorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
12 14 33 89 23 75
![Page 42: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/42.jpg)
Preorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
12 14 33 89 23 75 34 35
![Page 43: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/43.jpg)
Preorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
12 14 33 89 23 75 34 35 55
![Page 44: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/44.jpg)
Postorder Search Through Binary Tree (LRV)
14 34
33 23 35 55
89
12
75
![Page 45: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/45.jpg)
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 46: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/46.jpg)
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 47: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/47.jpg)
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 48: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/48.jpg)
89
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 49: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/49.jpg)
89 33
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 50: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/50.jpg)
89 33
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 51: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/51.jpg)
89 33
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 52: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/52.jpg)
89 33
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 53: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/53.jpg)
89 33 75
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 54: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/54.jpg)
89 33 75 23
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 55: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/55.jpg)
89 33 75 23 14
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 56: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/56.jpg)
89 33 75 23 14
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 57: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/57.jpg)
89 33 75 23 14
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 58: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/58.jpg)
89 33 75 23 14
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 59: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/59.jpg)
89 33 75 23 14 35
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 60: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/60.jpg)
89 33 75 23 14 35
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 61: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/61.jpg)
89 33 75 23 14 35
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 62: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/62.jpg)
89 33 75 23 14 35
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 63: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/63.jpg)
89 33 75 23 14 35 55
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 64: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/64.jpg)
89 33 75 23 14 35 55 34
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 65: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/65.jpg)
89 33 75 23 14 35 55 34 12
Postorder Search Through Binary Tree
14 34
33 23 35 55
89
12
75
![Page 66: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/66.jpg)
Other traversals
• The other traversals are the reverse of these three standard ones– That is, the right subtree is traversed before the left
subtree is traversed
• Reverse preorder: root, right subtree, left subtree• Reverse inorder: right subtree, root, left subtree• Reverse postorder: right subtree, left subtree, root
![Page 67: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/67.jpg)
Arithmetic Expression Tree
• Binary tree for an arithmetic expression– internal nodes: operators– leaves: operands
• Example: arithmetic expression tree for the expression (2 (5 - 1) + (3 2))
2
5 1
3 2
![Page 68: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/68.jpg)
Print Arithmetic Expressions
• inorder traversal:– print “(“ before traversing
left subtree– print operand or operator
when visiting node– print “)“ after traversing right
subtree
void printTree(t) //binary operands only
if (t.left != null)print("(");
printTree (t.left);print(t.element );if (t.right != null)
printTree (t.right);print (")");
2
5 1
3 2((2 (5 - 1)) + (3 2))
![Page 69: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/69.jpg)
Evaluate Arithmetic Expressions
• postorder traversal– Recursively evaluate
subtrees– Apply the operator
after subtrees are evaluated
int evaluate (t)//binary operators only
if (t.left == null) //external node return t.element;else //internal node x = evaluate (t.left); y = evaluate (t.right); let o be the operator t.element z = apply o to x and y return z;
2
5 1
3 2
![Page 70: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/70.jpg)
Binary Trees: Recursive Definition
ROOT OF TREE T
T1 T2
SUBTREES
*left_child *right_child
![Page 71: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/71.jpg)
Binary Search Trees
6
92
41 8
![Page 72: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/72.jpg)
Binary Search Tree
• A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying the following property:– Let u, v, and w be three
nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u) key(v) key(w)
• External nodes do not store items
• An inorder traversal of a binary search trees visits the keys in increasing order
6
92
41 8
Samller keys
Greater keys
![Page 73: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/73.jpg)
Binary Search Tree Operations
• insertElement(k)
• findElement(k)
• removeElement(k)
![Page 74: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/74.jpg)
Searching in a Binary Search Tree
• To search for a key k, we trace a downward path starting at the root
• The next node visited depends on the outcome of the comparison of k with the key of the current node
• If we reach a leaf, the key is not found and we return a null position
• Example: find(4)
Algorithm find (k, v)if T.isExternal (v)
return Position(null)if k key(v)
return find(k, T.leftChild(v))else if k key(v)
return Position(v)else { k key(v) }
return find(k, T.rightChild(v))
6
92
41 8
![Page 75: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/75.jpg)
Insertion
• To perform operation insertElement(k, o), we search for key k
• Assume k is not already in the tree, and let w be the leaf reached by the search
• We insert k at node w and expand w into an internal node
• Example: insert 5
6
92
41 8
6
92
41 8
5
w
w
![Page 76: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/76.jpg)
Deletion of External Nodes• To perform operation
removeElement(k), we search for key k
• Assume key k is in the tree, and let let v be the node storing k
• If node v has a leaf child w, we remove v and w from the tree with operation removeAboveExternal(w): also reconnects the broken tree!
• Example: remove 4
6
92
41 8
5
vw
6
92
51 8
![Page 77: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/77.jpg)
Deletion of Internal Nodes
• We consider the case where the key k to be removed is stored at a node v whose children are both internal– we find the internal node w that
follows v in an inorder traversal
– we copy key(w) into node v
– we remove node w and its left child z (which must be a leaf) by means of operation removeAboveExternal(z)
• Example: remove 3
3
1
8
6 9
5
v
w
z
2
5
1
8
6 9
v
2
![Page 78: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/78.jpg)
Binary Search Trees in C
• We will use two struct definitions as ADT:– The BinaryNode simply defines individual
nodes in the tree.– The BinarySearchTree maintains a pointer
to the root of the binary search tree.
![Page 79: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/79.jpg)
Binary Search Trees in C
element, *left, *right
Functions
The Binary Tree Node struct BinaryNode
{
int element;
BinaryNode *left;
BinaryNode *right;
};
*left *right
el
![Page 80: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/80.jpg)
Binary Search Trees in C
*root
find/insert/remove functions
The Binary Search Tree ADT
struct BinarySearchTree
{
BinaryNode *root;
const int NOT_FOUND;
};void insert (const int x);void remove (const int x);
*left *right
el*root
![Page 81: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/81.jpg)
Binary Search Tree Methods
• bool find(): Search operation returns true if a value is found in the tree.
• The Find operation returns a pointer to the node in a tree T that has item X, or NULL if there is no such node.
• void insert(int X): The Insert operation inserts a new node (with item X) into the tree T.
• void remove(int X):The Remove operation removes the node (with item X) from the tree T.
![Page 82: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/82.jpg)
Searching a Binary TreeSearchNode returns true if a value is found in the tree, or false otherwise. bool searchNode(int num){
TreeNode *nodePtr = root;
while (nodePtr){
if (nodePtr->value == num)return true;
else if (num < nodePtr->value)nodePtr = nodePtr->left;
elsenodePtr = nodePtr->right;
}return false;
}
![Page 83: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/83.jpg)
The Find Operation
// Returns the element at the node
const int &getElement (BinaryNode *t)
{
return (t == NULL ? NOT_FOUND : t element);
}
// Start the search at the root node
const int &find (const int & x)
{
return elementAt (find (x, root));
}
element
*t
*root
![Page 84: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/84.jpg)
The Find Operation…
BinaryNode *find (const int &x, BinaryNode *t)
{
if ( t == NULL )
return NULL;
else if ( x < t element )
return find ( x, t left );
else if ( x > t element )
return find ( x, t right );
else
return t;
/* Match; return a poointer to the node */
}
*root
6
2
5
9
7 10
Suppose I try to find thenode with 7 in it. First godown the right subtree, thengo down the left subtree.
![Page 85: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/85.jpg)
Search Left: The FindMin Operation…
BinaryNode *findMin (BinaryNode *t)
{
if ( t == NULL )
return NULL;
else if ( t left == NULL )
return t;
return findMin (t left);
}
*root
6
2
5
9
7 10This function returns a pointer to the node
containing the element with smallest value in the tree. It does so by following the leftbranch of the tree.
![Page 86: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/86.jpg)
Search Right:The FindMax Operation…
BinaryNode *findMax (BinaryNode *t)
{
if ( t == NULL )
return NULL;
else if ( t right == NULL )
return t;
return findMax (t right);
}
*root
6
2
5
9
7 10
This function returns a pointer to the node containing thelargest element in the tree. It does so by following the rightside of the tree.
![Page 87: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/87.jpg)
The Insert Operation• Insertion is conceptually simple. To insert X into a
tree T, proceed down the tree as you would with a find. If X is found, do nothing. Otherwise insert X at the last spot on the path that has been traversed.
suppose I want toinsert a 1 into this tree…
*root
6
2
5
9
7 101
![Page 88: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/88.jpg)
The Insert Operationvoid insert (const int &x, BinaryNode *&t)
{
if (t == NULL)
t = new BinaryNode (x, NULL, NULL);
else if (x < t element)
insert(x, t left);
else if( x > t element )
insert(x, t right);
else
; // Duplicate entry; do nothing
}
*root
6
2
5
9
7 10
t left
1
NULLNULL
t
Note the pointer t is passedusing call by reference. In this casethis means tleft will be changed to t.
t=malloc(sizeof(BinaryNode));
t->x=val;t->left=t->right =NULL
![Page 89: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/89.jpg)
The Removal Operation
• If the node to be removed is a leaf, it can be deleted immediately.
• If the node has one child, the node can be deleted after its parent adjusts a link to bypass the deleted node. *root
6
2
5
9
7 10
What if the node with 2 is deleted?
![Page 90: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/90.jpg)
Removal…
*root
6
2
5
9
7 10
*root
6
2
5
9
7 10
t
t right
Set t = t right
![Page 91: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/91.jpg)
Removal…
• If the node to be removed has two children, the general strategy is to replace the data of this node with the smallest data of the right subtree.
• Then the node with the smallest data is now removed (this case is easy since this node cannot have two children).
![Page 92: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/92.jpg)
Removal…
*root
6
2
5
9
7 101
3
4
Remove the node with 2 again…
*root
6
3
5
9
7 101
3
4
![Page 93: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/93.jpg)
The Removal Operation void remove (const int &x, BinaryNode *&t) {
if ( t == NULL ) return; // Item doesn’t exist; do nothing
if ( x < t element ) remove( x, t left );
else if( x > t element ) remove( x, t right );
else if( t left != NULL && t right != NULL ) // Two kids
{
t element = findMin( t right ) element;
remove( t element, t right );
}
else // One kid7
{
BinaryNode *oldNode = t;
t = ( t left != NULL ) ? t left : t right;
free (oldNode);
}
}
![Page 94: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/94.jpg)
Binary Search Tree Operations
• Creating a Node: We will demonstrate binary tree operations
• The basis of our binary tree node is the following struct declaration:
struct TreeNode{
int value;TreeNode *left;TreeNode *right;
};
![Page 95: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/95.jpg)
BinaryTree.hstruct BinaryTree { struct TreeNode *root;
const int NOT_FOUND;
};
struct TreeNode {
int value;TreeNode *left;TreeNode *right;
};
TreeNode *root;void destroySubTree(TreeNode *);void deleteNode(int, TreeNode *&);void makeDeletion(TreeNode *&);void displayInOrder(TreeNode *);void displayPreOrder(TreeNode *);void displayPostOrder(TreeNode *);
![Page 96: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/96.jpg)
BinaryTree.h (continued)
/* Initialize BinaryTree and make root = NULL; */void insertNode(int);bool searchNode(int);void remove(int);void showNodesInOrder(void)
{ displayInOrder(root); }void showNodesPreOrder()
{ displayPreOrder(root); }void showNodesPostOrder()
{ displayPostOrder(root); }
![Page 97: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/97.jpg)
Binary Search Tree Operations
• Inserting a Node:First, a new node is allocated and its value member is initialized with the new value.
• The left and right child pointers are set to NULL, because all nodes must be inserted as leaf nodes.
• Next, we determine if the tree is empty. If so, we simply make root point to it, and there is nothing else to be done. But, if there are nodes in the tree, we must find the new node's proper insertion point.
![Page 98: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/98.jpg)
Binary Search Tree Operations
• If the new value is less than the root node's value, we know it will be inserted somewhere in the left subtree. Otherwise, the value will be inserted into the right subtree.
• We simply traverse the subtree, comparing each node along the way with the new node's value, and deciding if we should continue to the left or the right.
• When we reach a child pointer that is set to NULL, we have found out insertion point.
![Page 99: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/99.jpg)
The insertNode() Function
void insertNode(int num){
TreeNode *newNode,// Pointer to a new node *nodePtr;// Pointer to traverse the tree
// Create a new nodenewNode = new TreeNode; // Watch out C++ notation!newNode->value = num;newNode->left = newNode->right = NULL;
if (!root) // Is the tree empty?root = newNode;
else{
nodePtr = root;
![Page 100: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/100.jpg)
The insertNode() Function
while (nodePtr != NULL){
if (num < nodePtr->value){
if (nodePtr->left)nodePtr = nodePtr->left;
else{
nodePtr->left = newNode;break;
}}
![Page 101: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/101.jpg)
The insertNode() Function
else if (num > nodePtr->value){
if (nodePtr->right)nodePtr = nodePtr->right;
else{
nodePtr->right = newNode;break;
}} else{
printf("Duplicate value found in tree.\n“);break;
}}
}}
![Page 102: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/102.jpg)
Let’s Build A Bin Search-Ttree
// This program builds a binary tree with 5 nodes.
#include <stdio.h>#include "BinaryTree.h“
void main(void){
IntBinaryTree tree;
printf("Inserting nodes... “);tree.insertNode(5);tree.insertNode(8);tree.insertNode(3);tree.insertNode(12);tree.insertNode(9);printf("Done.\n“);
}
![Page 103: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/103.jpg)
How the Binary Tree Looks Like
Note: The shape of the tree is determined by the order in which the values are inserted. The root node in the diagram above holds the value 5 because that was the first value inserted.
![Page 104: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/104.jpg)
The displayInOrder() Function
void displayInOrder(TreeNode *nodePtr){
if (nodePtr){
displayInOrder(nodePtr->left);printf(“%d \n”, nodePtr->value);displayInOrder(nodePtr->right);
}}
![Page 105: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/105.jpg)
The displayPreOrder() Function
void IntBinaryTree::displayPreOrder(TreeNode *nodePtr){
if (nodePtr){
printf(“%d \n”, nodePtr->value); displayPreOrder(nodePtr->left);displayPreOrder(nodePtr->right);
}}
![Page 106: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/106.jpg)
The displayPostOrder() Function
void displayPostOrder(TreeNode *nodePtr){
if (nodePtr){
displayPostOrder(nodePtr->left);displayPostOrder(nodePtr->right);printf(“%d \n”, nodePtr->value);
}}
![Page 107: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/107.jpg)
Buil A B-Tree with 5 Nodes#include <stdio.h>#include "BinaryTree.h“void main(void){
IntBinaryTree tree;printf("Inserting nodes.\n“);tree.insertNode(5);tree.insertNode(8);tree.insertNode(3);tree.insertNode(12);tree.insertNode(9);
printf("Inorder traversal:\n“);showNodesInOrder(tree);printf("\nPreorder traversal:\n“);showNodesPreOrder(tree);printf("\nPostorder traversal:\n“);showNodesPostOrder(tree);
}
![Page 108: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/108.jpg)
5 Node B-Tree Output Inserting nodes.Inorder traversal:358912 Preorder traversal:538129
Postorder traversal:3
91285
![Page 109: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/109.jpg)
Build A B-Tree & Search 3 in it// This program builds a binary tree with 5 nodes.// The SearchNode function determines if the value 3 is in the tree.#include <stdio.h>#include "BinaryTree.h“void main(void){
IntBinaryTree tree;printf("Inserting nodes.\n“);tree.insertNode(5);tree.insertNode(8);tree.insertNode(3);tree.insertNode(12);tree.insertNode(9);
if (tree.searchNode(3))pritnf( "3 is found in the tree.\n“);
elseprinyf( "3 was not found in the tree.\n“);
}
if (tree.searchNode(3))printf( "3 is found in the tree.\n“);
elseprintf("3 was not found in the tree.\n“);
}
![Page 110: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/110.jpg)
Deleting a Node
• We simply find its parent and set the child pointer that links to it to NULL, and then free the node's memory.
• But what if we want to delete a node that has child nodes? We must delete the node while at the same time preserving the subtrees that the node links to.
![Page 111: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/111.jpg)
Deleting a Node
• There are two possible situations when we are deleting a non-leaf node:– A) the node has one child, or– B) the node has two children.
![Page 112: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/112.jpg)
Deleting a NodeA tree in which we are about to delete a node with one subtree.
![Page 113: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/113.jpg)
Deleting a NodeHow to will link the node's subtree with its parent after the delete.
![Page 114: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/114.jpg)
Deleting a NodeThe problem is not as easily solved, however, when the node we are about to delete has two subtrees.
![Page 115: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/115.jpg)
Deleting a Node
• We cannot attach both of the node's subtrees to its parent, so there must be an alternative solution.
• One way is to attach the node's right subtree to the parent, and then find a position in the right subtree to attach the left subtree. The result is shown in the next slide.
![Page 116: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/116.jpg)
Deleting a Node
![Page 117: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/117.jpg)
Deleting a Node
To delete a node from the BinaryTree, call function remove(). The argument is the value of the node that is to be deleted.
void remove(int num){
deleteNode(num, root);}
The remove function calls the deleteNode function. It passes the value of the node to delete, and the root pointer.
![Page 118: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/118.jpg)
The deleteNode()
void deleteNode(int num, TreeNode *&nodePtr){
if (num < nodePtr->value)deleteNode(num, nodePtr->left);
else if (num > nodePtr->value)deleteNode(num, nodePtr->right);
elsemakeDeletion(nodePtr);
} elsemakeDeletion(nodePtr);
Notice the declaration of the nodePtr parameter:
TreeNode *&nodePtr;
nodePtr is not simply a pointer to a TreeNode structure, but a reference to a pointer to a TreeNode structure. Any action performed on nodePtr is actually performed on the argument passed into nodePtr.
![Page 119: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/119.jpg)
The deleteNode() Function
The trailing else statement calls the makeDeletion function, passing nodePtr as its argument.
•The makeDeletion function actually deletes the node from the tree, and must reattach the deleted node’s subtrees.
•Therefore, it must have access to the actual pointer in the binary tree to the node that is being deleted.
•This is why the nodePtr parameter in the deleteNode function is a reference.
![Page 120: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/120.jpg)
The makeDeletion()
void makeDeletion(TreeNode *&nodePtr){
TreeNode *tempNodePtr; // Temporary pointer, used in // reattaching the left subtree.
if (nodePtr == NULL)cout << "Cannot delete empty node.\n";
else if (nodePtr->right == NULL){
tempNodePtr = nodePtr;nodePtr = nodePtr->left; // Reattach the left childfree (tempNodePtr);
}else if (nodePtr->left == NULL){
tempNodePtr = nodePtr;nodePtr = nodePtr->right; // Reattach the right childfree (tempNodePtr);
}
![Page 121: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/121.jpg)
The makeDeletion() (continued)
// If the node has two children.else{
// Move one node the right.tempNodePtr = nodePtr->right;// Go to the end left node.while (tempNodePtr->left)
tempNodePtr = tempNodePtr->left;// Reattach the left subtree.tempNodePtr->left = nodePtr->left;tempNodePtr = nodePtr;// Reattach the right subtree.nodePtr = nodePtr->right;free (tempNodePtr);
}}
![Page 122: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/122.jpg)
Build a binary tree with 5 nodes// The DeleteNode function is used to remove two of them.#include <stdio.h>#include "BinaryTree.h“void main(void){
BinaryTree tree;printf("Inserting nodes.\n“);tree.insertNode(5);tree.insertNode(8);tree.insertNode(3);tree.insertNode(12);tree.insertNode(9);printf("Here are the values in the tree:\n“);tree.showNodesInOrder();
printf("Deleting 8...\n";tree.remove(8);printf("Deleting 12...\n";tree.remove(12);printf("Now, here are the nodes:\n";tree.showNodesInOrder();
}
![Page 123: Data Structures and Algorithms Introduction to Binary and Binary Search Trees](https://reader035.fdocuments.net/reader035/viewer/2022081603/5681368c550346895d9e14d8/html5/thumbnails/123.jpg)
Program Output
Inserting nodes.Here are the values in the tree:358912Deleting 8...Deleting 12...Now, here are the nodes:359