270 likes | 511 Views
Tree. Node Edge Path Root Parent Child Leaf Subtree Level. What is a TREE ?. Tree. Tree: Tree generally implemented in the computer using pointers Unbalanced Trees means that : Most of the nodes are on one side of the root. Individual subtrees may also be unbalanced.
E N D
Tree • Node • Edge • Path • Root • Parent • Child • Leaf • Subtree • Level What is a TREE ?
Tree • Tree: • Tree generally implemented in the computer using pointers Unbalanced Trees means that : • Most of the nodes are on one side of the root. • Individual subtrees may also be unbalanced.
Binary Tree • Binary Tree: • It is a tree whose nodes have two children (possibly empty), and each child is designed as either a left child or a right child.
Operations on a Binary Tree: • Finding node(Binary Search Tree) • Inserting node • Deleting Node • Traversing • Finding Minimum and Maximum Values
Operations on a Binary Tree: 57 1.Finding node :
Operations on a Binary Tree: 45 2.Inserting node :
Operations on a Binary Tree: 3.Deleting Node : A- The node to be deleted has no children
Operations on a Binary Tree: 3.Deleting Node : B- The node to be deleted has one child
Operations on a Binary Tree: 3.Deleting Node : C- The node to be deleted has two children
Operations on a Binary Tree: 4.Traversing the Tree: • Visiting each node in a specified order. • Three simple ways to traverse a tree: • Inorder • Preorder • Postorder
Operations on a Binary Tree: Inorder traversal : the left child is recursively visited, the node is visited, and the right child is recursively visited.Steps involved in Inorder traversal (recursion) are:1. Call itself to traverse the node’s left subtree2. Visit the node (e.g. display a key)3 Call itself to traverse the node’s right subtree. Void inOrder(Node* pRoot) { If (pRoot!= null) { inOrder(pRoot->leftChild); cout<< pRoot->Data<<“ “; inOrder(pRoot->rightChild); } }
Operations on a Binary Tree: Preorder traversal: a node is visited and then its children are visited recursively. Sequence of preorder traversal: -- Visit the node-- Call itself to traverse the node’s left subtree-- Call itself to traverse the node’s right subtree. Void preorder (Node* pRoot) { If (pRoot!= null) { cout<< pRoot->Data<<“ “; preorder (pRoot->leftChild); preorder (pRoot->rightChild); } }
Operations on a Binary Tree: Postorder traversal : a node is visited after both children are visited.Sequence of postorder traversal:-- Call itself to traverse the node’s left subtree-- Call itself to traverse the node’s right subtree-- Visit the node. Void Postorder (Node* pRoot) { If (pRoot!= null) { Postorder (pRoot->leftChild); Postorder (pRoot->rightChild); cout<< pRoot->Data<<“ “; } }
Binary Search Tree 5.Finding Minimum Values :
Binary Search Tree 6.Finding Maximum Values :
Representing the Tree in C++ Code The Node Class class Node { public: int day; float temp; Node* pLeftchild; Node* pRightchild; //constructor Node(int d,float t) {day = d; temp = t; pLeftchild = NULL; pRightchild = NULL;} //display the data as:{1, 5.76} void displaynode() {cout<<'{'<<day<<','<<temp<<'}';} };//end class node
Representing the Tree in C++ Code The Tree Class class Tree { private: Node* pRoot; public: //constructor Tree(){pRoot=NULL;}
Inserting node in C++ code //insert node void insert(int d, float t) {Node* pNewnode=new Node(d,t); if (pRoot==NULL) pRoot=pNewnode; else { Node* pCurrent=pRoot; Node* pParent; while (true) {pParent=pCurrent; if(d<pCurrent->day) { pCurrent=pCurrent->pLeftchild; if(pCurrent==NULL) {pParent->pLeftchild=pNewnode; return;} } else {pCurrent=pCurrent->pRightchild; if(pCurrent==NULL) {pParent->pRightchild=pNewnode; return; } } } } }
Finding node in C++ Node* find(int key) { Node* pCurrent=pRoot; while(pCurrent->day!=key) { if(key<pCurrent->day) pCurrent=pCurrent->pLeftchild; else pCurrent=pCurrent->pRightchild; if(pCurrent==NULL) return NULL; } return pCurrent; }
Finding Minimum Value in c++ : Node* minimum() { Node* pCurrent=pRoot; Node* pLast; while(pCurrent!=NULL) { pLast=pCurrent; pCurrent=pCurrent->pLeftchild; } return pLast; }
Finding Maximum Value in c++ : Node* maximum() { Node* pCurrent=pRoot; Node* pLast; while(pCurrent!=NULL) { pLast=pCurrent; pCurrent=pCurrent->pRightchild; } return pLast; }
Finding sum in c++ : void sum1(float &s ) {sum(s,pRoot);} void sum(float &s,Node* plocatRoot) { if(plocatRoot!=NULL) {s=s+plocatRoot->temp; sum(s,plocatRoot->pLeftchild); sum(s,plocatRoot->pRightchild); } }
Finding count in c++ : void count (int &c ) {count1(c,pRoot);} void count1(int &conut,Node* plocatRoot) { if(plocatRoot!=NULL) {conut++; count1(conut,plocatRoot->pLeftchild); count1(conut,plocatRoot->pRightchild); } } };
void main(){ Tree tree1; int n; cin>>n; int day1; float temp1; for(int i=1;i<=n;i++) { cin>>day1>>temp1; tree1.insert(day1,temp1); } //Finding a node with a given key cout<<"\n Enter day number to search about:"; int findkey; cin>>findkey; Node* pfind=tree1.find(findkey); if(pfind!=NULL) { cout<<"\n found node with key"<< findkey<<""; pfind->displaynode(); } else cout<<"can not find node"<<endl;
//minimum & maximum value Node*min=tree1.minimum(); Node*max=tree1.maximum(); cout<<"\nthe Minimum value int the tree is:"<<min->day; cout<<"\nthe maximum value int the tree is:"<<max->day; cout<<endl; float sum=0; tree1.sum_temp(sum); cout<<"the sum of tempretures:"<<sum<<endl; int count=0; tree1.count_day(count); cout<<"the count of day:"<<count<<endl; }
Evolution questions Answer the following questions for the tree shown below. • What is the path length of the path from node 20 to node 12? • Which node is the parent of node 35? • Draw the sub-tree rooted at node 43. • Traverse the tree in Preorder, Inorder and postorder. • Show what would this tree look like after: • Deleting 11 • Deleting 18