日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

算法与数据结构 -- 二叉树(六)

發布時間:2025/1/21 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 算法与数据结构 -- 二叉树(六) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、二叉樹

  • 滿二叉樹:除最后一層無任何子節點外,每一層上的所有結點都有兩個子結點的二叉樹
  • 完全二叉樹:完全二叉樹是效率很高的數據結構,完全二叉樹是由滿二叉樹而引出來的。對于深度為K的,有n個結點的二叉樹,當且僅當其每一個結點都與深度為K的滿二叉樹中編號從1至n的結點一一對應時稱之為完全二叉樹

  • 排序二叉樹
  • 二叉搜索樹 :二叉查找樹(Binary Search Tree),(又:二叉搜索樹,二叉排序樹)它或者是一棵空樹,或者是具有下列性質的二叉樹: 若它的左子樹不空,則左子樹上所有結點的值均小于它的根結點的值; 若它的右子樹不空,則右子樹上所有結點的值均大于它的根結點的值; 它的左、右子樹也分別為二叉排序樹
  • 平衡二叉樹
  • 二、完全二叉樹的構建

    二叉樹的每個結點包含三個信息:左子結點、右子結點的位置以及自身的值
    基本元素:在建二叉樹前,構建結點類。
    存儲:用隊列存儲二叉樹。隊列尾部進,頭部出。根結點從頭部出時,相應的子結點從尾部進入

    結點定義

    class Node():def __init__(self,item):self.val = itemself.left = Noneself.right = None

    樹定義
    給樹添加元素的方法。

  • 新建一個隊列,存儲待訪問的結點,結點訪問完成后,彈出結點
  • 從根結點開始遍歷樹,如果它的左子樹存在,則將子樹壓入隊列,否則將待插入結點掛在左子樹上。同樣的方法處理右子樹。
  • 然后彈出隊列最先進去的結點,左右子樹使用上面同樣的處理方法
  • class Binary_tree():def __init__(self):self.root = None#根結點def add_item(self,item):node = Node(item)if self.root == None:self.root = nodereturnqueue = Queue()current_node = self.rootwhile current_node is not None:if current_node.left is not None:queue.en_queue(current_node.left)else:current_node.left = nodereturnif current_node.right is not None:queue.en_queue(current_node.right)else:current_node.right = nodereturncurrent_node = queue.de_queue()

    三、任意二叉樹的構建

    三、廣度遍歷二叉樹

    廣度遍歷,即層級遍歷二叉樹。一層一層的訪問結點,打印相應的值。
    下圖廣度遍歷的結果:1 2 3 4 5 6 7 8。
    廣度遍歷與完全二叉樹添加元素的思想相似。
    步驟:

  • 訪問樹的根結點,如果不為空,打印根結點的值,否則退出
  • 訪問根結點的左子樹,如果左子樹不為空,將左結點存入隊列。相同的方法處理右子樹
  • 彈出隊列首部的元素,重復步驟1~3
  • def travel(self):if self.root == None:returncurrent_node = self.rootqueue = Queue()#新建隊列,存儲待處理的結點。# 需要處理的彈出來,如果左右子樹存在,將左右子樹存儲在隊列中while current_node is not None:# 判斷當前結點是否為空if current_node.left is not None:queue.en_queue(current_node.left)if current_node.right is not None:queue.en_queue(current_node.right)print(current_node.val,end=' ')current_node = queue.de_queue()

    四、深度遍歷二叉樹(遞歸方法實現)

  • 先序遍歷
    先序遍歷訪問順序是:根 ——左 ——右
  • def pre_order(self,current_root):if current_root == None :returnprint(current_root.val,end=' ')self.pre_order(current_root.left)self.pre_order(current_root.right)
  • 中序遍歷
    中序遍歷訪問順序是:左 ——根 ——右
  • def in_order(self,current_root):if current_root == None :returnself.in_order(current_root.left)print(current_root.val, end=' ')self.in_order(current_root.right)
  • 后序遍歷
    后序遍歷訪問順序是:左 ——右——根
  • def post_order(self,current_root):if current_root == None :returnself.post_order(current_root.left)self.post_order(current_root.right)print(current_root.val, end=' ')

    五、深度遍歷二叉樹(非遞歸方法實現)

  • 先序遍歷
  • def pre_order(self):if self.root == None:returncurrent_root = self.roots = Stack()s.push(current_root)while not s.is_empty():while current_root is not None:print(current_root.val,end=' ')if current_root.right is not None:s.push(current_root.right)current_root = current_root.leftcurrent_root = s.pop()
  • 中序遍歷
  • def in_order(self):#多輸出一個根結點if self.root == None:returncurrent_root = self.roots = Stack()s.push(current_root)while not s.is_empty():while current_root is not None:# s.push(current_root)if current_root.left is not None:current_root = current_root.lefts.push(current_root)else:print(current_root.val,end=' ')s.pop()breakcurrent_root = s.pop()if current_root is None:returnprint(current_root.val,end=' ')current_root = current_root.rights.push(current_root)

    1

    總結

    以上是生活随笔為你收集整理的算法与数据结构 -- 二叉树(六)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。