Download presentation
Presentation is loading. Please wait.
Published byMorris Hoover Modified over 9 years ago
1
Trees Namiq Sultan
2
Trees Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing hierarchical relationship between the grand father and his children and grand children. A tree is a nonempty collection of vertices and edges that satisfy certain requirements. A vertex is a simple object (also referred as a node) that can have a name and can carry other associated information. An edge is a connection between two vertices. A path in a tree is a list of distinct vertices in which successive vertices are connected by edges in the tree. Note that there is exactly one path between the root and each of other nodes in the tree. Each node (except the root) has exactly one node above it, which is called its parent. The nodes directly below a node are called its children. Nodes with no children are sometimes called leaves, or terminal nodes
3
Binary Trees A binary tree is a finite set of elements that is either empty or is partitioned into three disjoint subset. The first subset contains a single element called the root of the tree. The other two subsets are themselves binary trees, called the left and right subtree. The subtree can be empty.
4
There is no natural order for sorting the data in a tree such as the order used in a linked list (addfirst, addlast, …). So, we need a specified method to store the data. To store a list of numbers in a binary tree, we perform the following: The first number in the list is placed in a node that is established the root of a binary tree. Each successive number in the list is then compared to the number in the root. If it is smaller, examine the left subtree. If the subtree is empty, the number is placed into a new node at that position in the tree. If the subtree is not empty, compare the number with the contents of the root of the subtree and the entire process is repeated with the subtree. Binary Trees
5
The following figure illustrates the tree constructed from the inputs 14, 15, 4, 9, 7, 18, 3, 16, 20, 9, 14 14 39 18 14 15 4 97 1620 Binary Trees
6
Binary Tree Representation A node may be defined as: struct node{ int info; node *right; node *left; // father field may exist };
7
The makeNode function, which allocates a node and sets it as the root of a single-node binary tree, may be written as: node * makeNode(int x) { node * p; p = new node; p->info = x; p->left = NULL; p->right = NULL; return p; } Binary Tree Representation
8
Creating Binary Tree int main() { node *root, *p, *q; int num; cin >> num; root= makeNode(num); while (1){ cin >> num; if (num == 0) break; p = q = root; while(q != NULL){ p = q; if (num info) q = q -> left; else q = q -> right; } if (num info) p->left = makeNode(num); else p->right= makeNode(num); } return 0; }
9
Binary Tree Traversal To traverse a binary tree is to pass through the tree. Three of traversal methods will be defined. In each of these method, nothing need be done to traverse an empty binary tree. The methods are all defined recursively, so that traversing a binary tree involves visiting the root and traversing its left and right subtrees. The only difference among the methods is the order in which these trees operations are performed.
10
Binary Tree Traversal - Preorder:1. Visit the root 2. Traverse the left subtree in preorder 3. Traverse the right subtree in preorder - Inorder:1. Traverse the left subtree in inorder 2. Visit the root 3. Traverse the right subtree in inorder - Postorder:1. Traverse the left subtree in postorder 2. Traverse the right subtree in postorder 3. Visit the root
11
Binary Tree Traversal Preorder A B D G C E H I F Inorder D G B A H E I C F Postorder G D B H I E F C A A D B E C G F HI
12
Binary Tree Traversal void preorder(node* root) { if (root != NULL){ cout info; preorder(root->left); // left subtree preorder(root->right); //right subtree } void inorder (node* root) { if (root != NULL){ inorder(root ->left); cout info; inorder(root ->right); } void postorder(node* root) { if (root != NULL){ preorder(root->left); // left subtree preorder(root->right); //right subtree cout info; }
13
Binary Search Tree Binary search tree is a binary tree with a property that all elements in the left subtree of a node n are less than the contents of n, and all elements in the right subtree of n are greater than or equal to the contents of n. If a binary search tree is traversed in inorder (left, root, right) and the contents of each node are printed as a node visited, the numbers are printed in ascending order. void inorder(nodePtr root) { if (root != NULL) { inorder(root->left); cout info; inorder(root->right); }
14
Binary Tree Traversal Inorder Output: 4, 5, 6, 7, 8, 9 6 4 7 8 5 9 Input: 6, 4, 8, 5, 7, 9 6 5 7 8 4 9 Input: 6, 5, 4, 8, 7, 9
15
Delete a node from a binary tree 1.If the node to be deleted has a left branch only, the left son is selected to replace the deleted element. 2.If the node to be deleted has a right branch only, the right son is selected to replace the deleted element. 3.If the node has two branches, the right branch is selected and the smallest element in that branch is used to replace that element (i.e. the left most element of the right branch). 4.If the node to be deleted is a leaf, it is simply deleted.
16
Delete a node from a binary tree // Non-recursive for deleting a node void Delete(node* &root, int x) { node* p = root; // p points to the node to be deleted node* q, rp, s, f; // q father of p, rp points to the node that // will replace p // f father of rp, s left son of rp if(p==NULL){ cout<<“The tree is empty.”; return; } q = NULL; while (p != NULL && p->info != x){ q = p; p = (x info) ? p->left : p->right; }
17
ifNULL if (p == NULL) // x does not exist return return ; ifNULL if (p->left == NULL) // no left branch rp = p->right; elseifNULL else if (p->right == NULL) // no right branch rp = p->left; else else{ // find the replacement node f = p; // father of the replacement node rp = p->right; s = rp->left; // son of the replacement node while(sNULL while(s != NULL){ f = rp; rp = s; s = rp->left; }
18
if(f == p) // rp in next level rp->left = p->left; // no left node for rp else { // rp not in next level f->left = rp->right; rp->right = p->right; rp->left = p->left; } } // end of else if if (q == NULL) // node(p) is the root of the tree root = rp;else (p == q->left) ? q->left = rp : q->right = rp; delete delete p; } p f rp q
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.