1 / 19

Topics

Topics. Definition and Application of Binary Trees Binary Search Tree Operations. Definition and Application of Binary Trees. Binary tree : a nonlinear data structure in which each node may point to 0, 1, or two other nodes The nodes that a node N points to are the children of N.

Download Presentation

Topics

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. Topics Definition and Application of Binary Trees Binary Search Tree Operations

  2. Definition and Application of Binary Trees • Binary tree: a nonlinear data structure in which each node may point to 0, 1, or two other nodes • The nodes that a node N points to are the children of N NULL NULL NULL NULL NULL NULL

  3. Terminology • If a node N is a child of another node P, then P is called the parent of N • A node that has no children is called a leaf • In a binary tree there is a unique node with no parent, it is called the root of the tree

  4. Binary Tree Terminology • Tree pointer: like a head pointer for a linked list, it points to the first node in the binary tree • Root node: the node with no parent NULL NULL NULL NULL NULL NULL

  5. Binary Tree Terminology • Leaf nodes: nodes that have no children The nodes containing 7 and 43 are leaf nodes 31 19 59 7 NULL 43 NULL NULL NULL NULL NULL

  6. Binary Tree Terminology • Child nodes, children: The children of the node containing 31 are the nodes containing 19 and 59 31 19 59 7 43 NULL NULL NULL NULL NULL NULL

  7. Binary Tree Terminology The parent of the node containing 43 is the node containing 59 31 19 59 7 43 NULL NULL NULL NULL NULL NULL

  8. Binary Tree Terminology • A descendant of a node is defined as follows: (1) A node is considered its own descendant (2) A child of a descendant of a node N is a descendant of N So the set of all descendants of a node includes the node itself, its children, the children of the children, etc.

  9. Binary Tree Terminology • A subtree of a binary tree is a part of the tree consisting of all descendants of a given node N • Such a subtree is said to be rooted at N, and N is called the root of the subtree

  10. Subtrees of Binary Trees • A subtree of a binary tree is itself a binary tree • A nonempty binary tree consists of a root node, with the rest of its nodes forming two subtrees, called the left and right subtree

  11. Binary Tree Terminology • The node containing 31 is the root • The nodes containing 19 and 7 form the left subtree • The nodes containing 59 and 43 form the right subtree 31 19 59 7 43 NULL NULL NULL NULL NULL NULL

  12. Binary Tree Node • A node in a binary tree is like a node in a linked list, except it has two node pointer fields: class TreeNode { public: int value; TreeNode *left; TreeNode *right; }; It is convenient to use a constructor to assist in creation of nodes

  13. Binary Tree Operations • Create a binary tree • Insert a node into a binary tree at some position • Delete a node from a binary tree – remove a node and adjust links to preserve the binary tree • Update a node in a binary tree • Retrieve value of a node in a binary tree

  14. Inserting an item into a Binary Tree • If tree is empty, create a new node as root, with empty left and right subtrees • Otherwise, insert the item as a leaf (as either the right or left child), update the links

  15. Since the right subtree is NULL, insert 23 here Inserting an item into a Binary Tree root 65 87 59 7 43 NULL NULL NULL NULL NULL NULL

  16. Traversing a Binary Tree Three traversal methods: • Inorder: • Traverse left subtree of node • Process data in node • Traverse right subtree of node • Preorder: • Process data in node • Traverse left subtree of node • Traverse right subtree of node • Postorder: • Traverse left subtree of node • Traverse right subtree of node • Process data in node

  17. Traversing a Binary Tree 65 87 59 7 43 NULL NULL NULL NULL NULL NULL

  18. Uses of Binary Trees • Binary search tree: a binary tree whose data is organized to simplify searches • Left subtree at each node contains data values less than the data in the node • Right subtree at each node contains values greater than the data in the node 31 19 59 7 NULL 43 NULL NULL NULL NULL NULL

  19. Uses of Binary Trees • Decision Tree: a binary tree whose data is organized to answer yes/no question • Left subtree at each node contains yes responses • Right subtree at each node contains no responses Is it an animal? Does it fly? Flower NULL NULL Dog Bird NULL NULL NULL NULL NULL

More Related