1 / 25

ADT Tree

ADT Tree. Tree : nonempty collection of vertices and edges that satisfies certain requirements. Vertex ( node ): Have a name and carry other associated information Edge : A connection between two vertices.

duncan
Télécharger la présentation

ADT 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. ADT Tree Tree: nonempty collection of vertices and edges that satisfies certain requirements. Vertex (node): Have a name and carry other associated information Edge: A connection between two vertices. We usually draw trees with the root (node A)at the top and leaves at the bottom!

  2. ADT Tree • Any node (except the root) has exactly one edge running upward to another node. The node above it is called theparent of the node. For example, D is the parent of H and I. G is the parent of J and K. • The nodes below a given node are called its children. For example, B, C and D are children of A. L is a child of H. • Nodes with no children are known as leaves. For example, E,F,J,L,M and N are leaves. • A node that has children is an internal node. For example, C is an internal node. • The height of a tree is the length (i.e. number of edges) of a longest path from the root to a leaf. For example, the height of the above tree is 3.

  3. ADT Tree Subtree Subtree

  4. ADT Binary Tree Binary tree : no node can have more than two children (left child and right child).

  5.  + C D _ A B E Expression Tree • Expression tree for (A + B)  ((C / D) – E). • Leaves are operands (constants or variable names). Internal nodes are operators • If we traverse the above tree in postorder we will get AB+ CD/E -  which is the postfix of (A + B)  ((C / D) – E).

  6. Complete Binary Tree • All internal nodes have (exactly) two children • All leaves are at the same level • Height of an n-node complete binary tree is log n

  7. Implementation : ADT Binary Tree Data Structure: typedef struct Node *Node_ptr; struct Node { ElementType element; Node_ptr left; Node_ptr right; } ; typedef Node_ptr tree;

  8. ADT Binary Tree - Operations • create anempty tree • add a new node to the tree • remove a node from the tree. • tree traversal :preorder, inorder and post order

  9. Tree traversals Inorder: • Traverse the left subtree in inorder • Visit the root (and print the value ) • Traverse the right subtree in inorder

  10. Tree traversals Preorder: • Visit the root (and print the value ) • Traverse the left subtree in preorder • Traverse the right subtree in preorder

  11. Tree traversals Postorder: • Traverse the left subtree in postorder • Traverse the right subtree in postorder • Visit the root (and print the value )

  12. Tree traversals Inorder : B A C Preorder : A B C Postorder : B C A A B C

  13. Tree traversals Inorder: D B E A F C G Preorder: A B D EC F G Postorder: D E BF G C A A C B G F D E

  14. Tree traversals Inorder : G D H B A E I C J F K Preorder: A B D G HC E I F J K Postorder: G H D BI E J K F C A A C B E F D I J K G H

  15. ADT Binary Search Tree (BST) • Binary search tree is a binary tree such that for every node, X, in the tree, the values of the all the nodes in the left subtree are less the value stored at X, and the values of all the nodes in the right subtree are equal or larger than the value stored at X ( BST Property). • That is, a node’s left child must have a value less than its parent, and a node’s right child must have a value equal or greater than its parent .

  16. ADT Binary Search Tree (BST) Operations: • create a BST • insert an element • delete an element • find an element (search) • display all elements in a sorted order

  17. ADT Binary Search Tree (BST) Insert: • Start at the root • If value we want to insert is less than value of current node, we have to go to the left subtree • Otherwise we have to go to the right subtree • If the current node is empty (not existing) create a node with the value we are inserting and place it here.

  18. 87 87 27 67 54 54 Building a BST 54, 17, 87, 11, 19, 18, 93 54 54 54 54 87 17 17 87 17 11 54 54 17 87 17 87 11 93 19 11 19 18

  19. ADT Binary Search Tree (BST) Delete: Similar to the insert function, after deletion of a node, the BST property must be maintained. There are 3 possible cases: • Node to be deleted has no children We just delete the node. • Node to be deleted has only one child  The node can be deleted by replacing the node by that child. • Node to be deleted has two children

  20. ADT Binary Search Tree (BST) – Delete (contd.) Node to be deleted has two children

  21. ADT Binary Search Tree (BST) – Delete (contd.) Node to be deleted has two children Steps - A: • Find minimum value of right subtree • Delete minimum node of right subtree but keep its value • Replace the value of the node to be deleted by this minimum value OR Steps - B: • Find maximum value of left subtree • Delete maximum node of left subtree but keep its value • Replace the value of the node to be deleted by this maximum value

  22. ADT Binary Search Tree (BST) – Delete (contd.)Steps-A

  23. ADT Binary Search Tree (BST) Search- for element X in Tree T (r is the value stored at root) if T is empty "X is not in the tree"; else { if X = r "X is found" else { if X < r search the left tree else search the right tree } }

  24. ADT Binary Search Tree (BST) Using the data structure described in page 7, the above algorithm can be implemented in C as follows: tree Binarysearch (ElementType X, tree T) {if (T = NULL) ERROR("X is not in the tree") ; else { if (X = T->element) return T; else { if (X < T->element) return Binarysearch (X, T->left); else return Binarysearch (X, T->right); } } }

  25. An AVL (Adelson-Velskii and Landis) tree is a balanced BST with the height O(log n)

More Related