Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prepared by: S. Kondakci

Similar presentations


Presentation on theme: "Prepared by: S. Kondakci"— Presentation transcript:

1 Prepared by: S. Kondakci
Data Structures and Algorithms Introduction to Binary and Binary Search Trees Prepared by: S. Kondakci

2 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.

3 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

4 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.

5 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

6 Picture of a binary tree
d e g h i l f j k

7 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

8 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.

9 Inorder Traversal (LVR)
The node’s left subtree is traversed. The node’s data is processed. The node’s right subtree is traversed.

10 Preorder Traversal (VLR)
The node’s data is processed. The node’s left subtree is traversed. The node’s right subtree is traversed.

11 Postorder Traversal (LRV)
The node’s left subtree is traversed. The node’s right subtree is traversed. The node’s data is processed.

12 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); }

13 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); }

14 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); }

15 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: Preorder (VLR) Inorder (LVR) Postorder (LRV) To traverse the tree, collect the flags: A B D E C F G D E B F G C A A B C D E F G D B E A F C G A B C D E F G A B C D E F G

16 Inorder Search Through Binary Tree (LVR)
12 14 34 33 23 35 55 89 75

17 Inorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

18 Inorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

19 Inorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

20 Inorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

21 Inorder Search Through Binary Tree
12 89 14 34 33 23 35 55 89 75

22 Inorder Search Through Binary Tree
12 89 33 14 34 33 23 35 55 89 75

23 Inorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

24 Inorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

25 Inorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

26 Inorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

27 Inorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

28 Inorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

29 Inorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

30 Inorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

31 Inorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

32 Inorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

33 Inorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

34 Inorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

35 Preorder Search Through Binary Tree (VLR)
12 12 14 34 33 23 35 55 89 75

36 Preorder Search Through Binary Tree
12 12 14 14 34 33 23 35 55 89 75

37 Preorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

38 Preorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

39 Preorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

40 Preorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

41 Preorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

42 Preorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

43 Postorder Search Through Binary Tree (LRV)
12 14 34 33 23 35 55 89 75

44 Postorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

45 Postorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

46 Postorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

47 Postorder Search Through Binary Tree
12 89 14 34 33 23 35 55 89 75

48 Postorder Search Through Binary Tree
12 89 33 14 34 33 23 35 55 89 75

49 Postorder Search Through Binary Tree
12 89 33 14 34 33 23 35 55 89 75

50 Postorder Search Through Binary Tree
12 89 33 14 34 33 23 35 55 89 75

51 Postorder Search Through Binary Tree
12 89 33 14 34 33 23 35 55 89 75

52 Postorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

53 Postorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

54 Postorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

55 Postorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

56 Postorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

57 Postorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

58 Postorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

59 Postorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

60 Postorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

61 Postorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

62 Postorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

63 Postorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

64 Postorder Search Through Binary Tree
12 14 34 33 23 35 55 89 75

65 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

66 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

67 Print Arithmetic Expressions
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 (")"); inorder traversal: print “(“ before traversing left subtree print operand or operator when visiting node print “)“ after traversing right subtree + - 2 5 1 3 ((2  (5 - 1)) + (3  2))

68 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

69 Binary Trees: Recursive Definition
ROOT OF TREE T T1 T2 SUBTREES *left_child *right_child

70 Binary Search Trees < 6 2 > 9 1 4 = 8

71 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 9 2 4 1 8 Samller keys Greater keys

72 Binary Search Tree Operations
insertElement(k) findElement(k) removeElement(k)

73 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 2 > 9 1 4 = 8

74 Insertion < 6 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 2 9 > 1 4 8 > w 6 2 9 1 4 8 w 5

75 Deletion of External Nodes
6 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 < 2 9 > v 1 4 8 w 5 6 2 9 1 5 8

76 Deletion of Internal Nodes
1 v 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 2 8 6 9 w 5 z 1 v 5 2 8 6 9

77 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.

78 Binary Search Trees in C
The Binary Tree Node struct BinaryNode { int element; BinaryNode *left; BinaryNode *right; }; element, *left, *right Functions el *left *right

79 Binary Search Trees in C
struct BinarySearchTree { BinaryNode *root; const int NOT_FOUND; }; void insert (const int x); void remove (const int x); The Binary Search Tree ADT *root find/insert/remove functions *root el *left *right

80 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.

81 Searching a Binary Tree
SearchNode 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; else nodePtr = nodePtr->right; } return false; }

82 The Find Operation *t element *root // 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 *root

83 The Find Operation… *root 6 2 9 5 7 10 Suppose I try to find the
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 9 5 7 10 Suppose I try to find the node with 7 in it. First go down the right subtree, then go down the left subtree.

84 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 10 This function returns a pointer to the node containing the element with smallest value in the tree. It does so by following the left branch of the tree.

85 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 9 5 7 10 This function returns a pointer to the node containing the largest element in the tree. It does so by following the right side of the tree.

86 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. *root 6 2 5 9 7 10 1 suppose I want to insert a 1 into this tree…

87 The Insert Operation *root 6 2 9 5 7 10 Note the pointer t is passed
t=malloc(sizeof(BinaryNode)); t->x=val;t->left=t->right =NULL void 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 9 5 7 10 Note the pointer t is passed using call by reference. In this case this means tleft will be changed to t. t  left t 1 NULL NULL

88 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?

89 Removal… *root *root 6 6 t 2 2 9 9 t  right 5 7 10 5 7 10
Set t = t  right

90 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).

91 Removal… 3 Remove the node with 2 again… *root *root 6 6 2 9 9 1 5 7
10 1 5 7 10 3 3 4 4

92 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);

93 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; };

94 BinaryTree.h struct 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 *);

95 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); }

96 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.

97 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.

98 The insertNode() Function
void insertNode(int num) { TreeNode *newNode,// Pointer to a new node *nodePtr;// Pointer to traverse the tree // Create a new node newNode = 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;

99 The insertNode() Function
while (nodePtr != NULL) { if (num < nodePtr->value) { if (nodePtr->left) nodePtr = nodePtr->left; else { nodePtr->left = newNode; break; } }

100 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; } } } }

101 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“); }

102 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.

103 The displayInOrder() Function
void displayInOrder(TreeNode *nodePtr) { if (nodePtr) { displayInOrder(nodePtr->left); printf(“%d \n”, nodePtr->value); displayInOrder(nodePtr->right); } }

104 The displayPreOrder() Function
void IntBinaryTree::displayPreOrder(TreeNode *nodePtr) { if (nodePtr) { printf(“%d \n”, nodePtr->value); displayPreOrder(nodePtr->left); displayPreOrder(nodePtr->right); } }

105 The displayPostOrder() Function
void displayPostOrder(TreeNode *nodePtr) { if (nodePtr) { displayPostOrder(nodePtr->left); displayPostOrder(nodePtr->right); printf(“%d \n”, nodePtr->value); } }

106 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); }

107 5 Node B-Tree Output   Inserting nodes. Inorder traversal:   Preorder traversal: Postorder traversal: 3

108 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“); else prinyf( "3 was not found in the tree.\n“); } if (tree.searchNode(3)) printf( "3 is found in the tree.\n“); else printf("3 was not found in the tree.\n“); }

109 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.

110 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.

111 Deleting a Node A tree in which we are about to delete a node with one subtree.

112 Deleting a Node How to will link the node's subtree with its parent after the delete.

113 Deleting a Node The problem is not as easily solved, however, when the node we are about to delete has two subtrees.

114 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.

115 Deleting a Node

116 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.

117 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); else makeDeletion(nodePtr); } else makeDeletion(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.

118 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.

119 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 child free (tempNodePtr); } else if (nodePtr->left == NULL) { tempNodePtr = nodePtr; nodePtr = nodePtr->right; // Reattach the right child free (tempNodePtr); }

120 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); } }

121 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(); }

122 Program Output Inserting nodes. Here are the values in the tree: Deleting 8... Deleting Now, here are the nodes: 3 5 9


Download ppt "Prepared by: S. Kondakci"

Similar presentations


Ads by Google