Advertisement
Guest User

Листинг 32

a guest
Apr 24th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.58 KB | None | 0 0
  1. internal  class Node{
  2.     var iData: Int = 0
  3.     var dData: Double = 0.toDouble()
  4.     var leftChild: Node? = null
  5.     var rightChild: Node? = null
  6.  
  7.     fun displayNode(){
  8.         print("{$iData, $dData} ")
  9.     }
  10.  
  11.     internal class Tree{
  12.         private var root: Node?
  13.         init { root = null }
  14.  
  15.         fun find(key:Int):Node?{
  16.             var current = root
  17.             while (current!!.iData != key){
  18.                 if (key < current.iData) current = current.leftChild
  19.                 else current = current.rightChild
  20.                 if (current == null) return null
  21.             }
  22.             return current
  23.         }
  24.  
  25.         fun insert(id:Int, dd: Double){
  26.             val newNode = Node()
  27.             newNode.iData = id
  28.             newNode.dData = dd
  29.             if (root == null) root = newNode
  30.             else{
  31.                 var current = root
  32.                 var parent: Node
  33.                 while (true){
  34.                     parent = current
  35.                     if (id < current!!.iData){
  36.                         current = current.leftChild
  37.                         if (current == null){
  38.                             parent.leftChild = newNode
  39.                             return
  40.                         }
  41.                     }else{
  42.                         current = current.rightChild
  43.                         if (current == null){
  44.                             parent.rightChild = newNode
  45.                             return
  46.                         }
  47.                     }
  48.                 }
  49.             }
  50.         }
  51.  
  52.         fun delete(key:Int):Boolean{
  53.             var current = root
  54.             var parent: Node = root
  55.             var isLeftChild = true
  56.             while (current!!.iData != key){
  57.                 parent = current
  58.                 if (key < current.iData){
  59.                     isLeftChild = true
  60.                     current = current.leftChild
  61.                 }else{
  62.                     isLeftChild = false
  63.                     current = current.rightChild
  64.                 }
  65.                 if (current == null) return false
  66.             }
  67.            
  68.             if (current.leftChild == null && current.rightChild == null){
  69.                 if (current == root) root = null
  70.                 else if (isLeftChild) parent.leftChild = null
  71.                 else parent.rightChild = null
  72.             }
  73.            
  74.             else if(current.leftChild == null)
  75.                 if (current == root) root = current.leftChild
  76.                 else if (isLeftChild) parent.leftChild = current.leftChild
  77.                 else parent.rightChild = current.leftChild
  78.            
  79.             else if (current.leftChild == null)
  80.                 if (current == root) root = current.rightChild
  81.                 else if (isLeftChild) parent.leftChild = current.rightChild
  82.                 else parent.rightChild = current.rightChild
  83.            
  84.             else{
  85.                 var successor = getSuccessor(current)
  86.                 if (current == root) root = successor
  87.                 else if (isLeftChild) parent.leftChild = successor
  88.                 else parent.rightChild = successor
  89.                 successor.leftChild = current.leftChild
  90.             }
  91.            
  92.             return true
  93.         }
  94.  
  95.         private fun getSuccessor(delNode:Node): Node{
  96.             var successorParent = delNode
  97.             var successor = delNode
  98.  
  99.             var current = delNode.rightChild
  100.             while (current != null) {
  101.                 successorParent = successor
  102.                 successor = current
  103.                 current = current.leftChild
  104.             }
  105.             if (successor !== delNode.rightChild){
  106.                 successorParent.leftChild = successor.rightChild
  107.                 successor.rightChild = delNode.rightChild
  108.             }
  109.             return  successor
  110.         }
  111.  
  112.         private  fun preOrder(localRoot: Node?){
  113.             if (localRoot != null){
  114.                 print("${localRoot.iData} ")
  115.                 preOrder(localRoot.leftChild)
  116.                 preOrder(localRoot.rightChild)
  117.             }
  118.         }
  119.  
  120.         private  fun inOrder(localRoot: Node?){
  121.             if (localRoot != null){
  122.                 inOrder(localRoot.leftChild)
  123.                 print("${localRoot.iData} ")
  124.                 inOrder(localRoot.rightChild)
  125.             }
  126.         }
  127.  
  128.         private fun postOrder(localRoot: Node?){
  129.             if (localRoot != null){
  130.                 postOrder(localRoot.leftChild)
  131.                 postOrder(localRoot.rightChild)
  132.                 rint("${localRoot.iData} ")
  133.             }
  134.         }
  135.  
  136.     }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement