Download presentation
Presentation is loading. Please wait.
Published byErnest Sims Modified over 9 years ago
1
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 T 1, T 2, …, T k 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 n 1 to n k is defined as a sequence of nodes n 1, n 2, …, n k such that n i is the parent of n i+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
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; };
7
Picture of a binary tree a bc d e ghi l f jk
8
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
9
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.
10
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.
11
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.
12
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.
13
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); }
14
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); }
15
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); }
16
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 BC DEFG A BC DEFG A BC DEFG A B D E C F G D B E A F C G D E B F G C A
17
Inorder Search Through Binary Tree (LVR) 14 34 33 2335 55 89 12 75
18
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
19
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
20
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
21
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
22
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 89
23
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 89 33
24
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 89 33 14
25
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 89 33 14
26
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 89 33 14
27
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 89 33 14 75
28
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 89 33 14 75 23
29
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 89 33 14 75 23 12
30
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 89 33 14 75 23 12
31
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 89 33 14 75 23 12
32
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 89 33 14 75 23 12 35
33
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 89 33 14 75 23 12 35 34
34
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 89 33 14 75 23 12 35 34
35
Inorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 89 33 14 75 23 12 35 34 55
36
Preorder Search Through Binary Tree (VLR) 14 34 33 2335 55 89 12 75 12
37
Preorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 12 14
38
Preorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 12 14 33
39
Preorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 12 14 33 89
40
Preorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 12 14 33 89 23
41
Preorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 12 14 33 89 23 75
42
Preorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 12 14 33 89 23 75 34 35
43
Preorder Search Through Binary Tree 14 34 33 2335 55 89 12 75 12 14 33 89 23 75 34 35 55
44
Postorder Search Through Binary Tree (LRV) 14 34 33 2335 55 89 12 75
45
Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
46
Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
47
Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
48
89 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
49
89 33 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
50
89 33 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
51
89 33 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
52
89 33 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
53
89 33 75 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
54
89 33 75 23 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
55
89 33 75 23 14 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
56
89 33 75 23 14 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
57
89 33 75 23 14 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
58
89 33 75 23 14 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
59
89 33 75 23 14 35 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
60
89 33 75 23 14 35 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
61
89 33 75 23 14 35 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
62
89 33 75 23 14 35 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
63
89 33 75 23 14 35 55 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
64
89 33 75 23 14 35 55 34 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
65
89 33 75 23 14 35 55 34 12 Postorder Search Through Binary Tree 14 34 33 2335 55 89 12 75
66
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
67
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 51 32
68
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 51 32 ((2 (5 - 1)) + (3 2))
69
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 51 32
70
Binary Trees: Recursive Definition ROOT OF TREE T T1 T2 SUBTREES *left_child *right_child
71
Binary Search Trees 6 9 2 4 1 8
72
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 418 Samller keysGreater keys
73
Binary Search Tree Operations insertElement(k) findElement(k) removeElement(k)
74
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 9 2 4 1 8
75
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 418 6 9 2 4 18 5 w w
76
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 9 2 4 18 5 v w 6 9 2 5 18
77
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
78
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.
79
Binary Search Trees in C element, *left, *right Functions The Binary Tree Node struct BinaryNode { int element; BinaryNode *left; BinaryNode *right; }; *left*right el
80
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
81
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.
82
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 value) nodePtr = nodePtr->left; else nodePtr = nodePtr->right; } return false; }
83
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
84
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 710 Suppose I try to find the node with 7 in it. First go down the right subtree, then go down the left subtree.
85
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 710 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.
86
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 710 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.
87
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 to insert a 1 into this tree… *root 6 2 5 9 710 1
88
The Insert Operation 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 5 9 710 t left 1 NULL t Note the pointer t is passed using call by reference. In this case this means t left will be changed to t. t=malloc(sizeof(BinaryNode)); t->x=val;t->left=t->right =NULL
89
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 710 What if the node with 2 is deleted?
90
Removal… *root 6 2 5 9 710 *root 6 2 5 9 710 t t right Set t = t right
91
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).
92
Removal… *root 6 2 5 9 7101 3 4 Remove the node with 2 again… *root 6 3 5 9 7101 3 4
93
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); }
94
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; };
95
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 *);
96
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); }
97
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.
98
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.
99
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;
100
The insertNode() Function while (nodePtr != NULL) { if (num value) { if (nodePtr->left) nodePtr = nodePtr->left; else { nodePtr->left = newNode; break; } }
101
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; } } } }
102
Let’s Build A Bin Search-Ttree // This program builds a binary tree with 5 nodes. #include #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“); }
103
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.
104
The displayInOrder() Function void displayInOrder(TreeNode *nodePtr) { if (nodePtr) { displayInOrder(nodePtr->left); printf(“%d \n”, nodePtr->value); displayInOrder(nodePtr->right); } }
105
The displayPreOrder() Function void IntBinaryTree::displayPreOrder(TreeNode *nodePtr) { if (nodePtr) { printf(“%d \n”, nodePtr->value); displayPreOrder(nodePtr->left); displayPreOrder(nodePtr->right); } }
106
The displayPostOrder() Function void displayPostOrder(TreeNode *nodePtr) { if (nodePtr) { displayPostOrder(nodePtr->left); displayPostOrder(nodePtr->right); printf(“%d \n”, nodePtr->value); } }
107
Buil A B-Tree with 5 Nodes #include #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); }
108
5 Node B-Tree Output Inserting nodes. Inorder traversal: 3 5 8 9 12 Preorder traversal: 5 3 8 12 9 Postorder traversal: 3 9 12 8 5
109
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 #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“); }
110
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.
111
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.
112
Deleting a Node A tree in which we are about to delete a node with one subtree.
113
Deleting a Node How to will link the node's subtree with its parent after the delete.
114
Deleting a Node The problem is not as easily solved, however, when the node we are about to delete has two subtrees.
115
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.
116
Deleting a Node
117
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.
118
The deleteNode() void deleteNode(int num, TreeNode *&nodePtr) { if (num 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.
119
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.
120
The makeDeletion() void makeDeletion(TreeNode *&nodePtr) { TreeNode *tempNodePtr;// Temporary pointer, used in // reattaching the left subtree. if (nodePtr == NULL) cout 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); }
121
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); } }
122
Build a binary tree with 5 nodes // The DeleteNode function is used to remove two of them. #include #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(); }
123
Program Output Inserting nodes. Here are the values in the tree: 3 5 8 9 12 Deleting 8... Deleting 12... Now, here are the nodes: 3 5 9
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.