1 / 27

Tree

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.

kamal
Download Presentation

Tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tree • Node • Edge • Path • Root • Parent • Child • Leaf • Subtree • Level What is a TREE ?

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

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

  4. Operations on a Binary Tree: • Finding node(Binary Search Tree) • Inserting node • Deleting Node • Traversing • Finding Minimum and Maximum Values

  5. Operations on a Binary Tree: 57 1.Finding node :

  6. Operations on a Binary Tree: 45 2.Inserting node :

  7. Operations on a Binary Tree: 3.Deleting Node : A- The node to be deleted has no children

  8. Operations on a Binary Tree: 3.Deleting Node : B- The node to be deleted has one child

  9. Operations on a Binary Tree: 3.Deleting Node : C- The node to be deleted has two children

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

  11. 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); } }

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

  13. 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<<“ “; } }

  14. Binary Search Tree 5.Finding Minimum Values :

  15. Binary Search Tree 6.Finding Maximum Values :

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

  17. Representing the Tree in C++ Code The Tree Class class Tree { private: Node* pRoot; public: //constructor Tree(){pRoot=NULL;}

  18. 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; } } } } }

  19. 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; }

  20. Finding Minimum Value in c++ : Node* minimum() { Node* pCurrent=pRoot; Node* pLast; while(pCurrent!=NULL) { pLast=pCurrent; pCurrent=pCurrent->pLeftchild; } return pLast; }

  21. Finding Maximum Value in c++ : Node* maximum() { Node* pCurrent=pRoot; Node* pLast; while(pCurrent!=NULL) { pLast=pCurrent; pCurrent=pCurrent->pRightchild; } return pLast; }

  22. 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); } }

  23. 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); } } };

  24. 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;

  25. //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; }

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

More Related